cell.jsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import { VxeUI } from 'vxe-pc-ui';
  2. import dayjs from 'dayjs';
  3. import { get, has, isEmpty, isFunction } from 'lodash';
  4. import { formatMoney, formatPercentage } from '/@/components/BsUi/uitl.js';
  5. import { DISPLAY_STATE, FILE_TYPE_ICON_MAP } from '/@/components/BsUi/constant.js';
  6. import { DeleteOutlined, EditOutlined, EyeOutlined, PlusOutlined } from '@ant-design/icons-vue';
  7. import { h } from 'vue';
  8. const getValue = (params) => {
  9. const { column, row } = params;
  10. return get(row, column.field, '');
  11. };
  12. // 格式化人民币
  13. VxeUI.renderer.add('CellRmb', {
  14. // 默认显示模板
  15. renderTableDefault(renderOpts, params) {
  16. return <>¥{formatMoney(getValue(params))}</>;
  17. },
  18. });
  19. // 格式化百分比
  20. VxeUI.renderer.add('CellPercent', {
  21. // 默认显示模板
  22. renderTableDefault(renderOpts, params) {
  23. return <>{formatPercentage(getValue(params))}</>;
  24. },
  25. });
  26. // 格式化日期
  27. VxeUI.renderer.add('CellDate', {
  28. // 默认显示模板
  29. renderTableDefault(renderOpts, params) {
  30. if (isEmpty(getValue(params))) return <></>;
  31. return <>{dayjs(getValue(params)).format('YYYY-MM-DD')}</>;
  32. },
  33. });
  34. // 格式化时间
  35. VxeUI.renderer.add('CellDateTime', {
  36. // 默认显示模板
  37. renderTableDefault(renderOpts, params) {
  38. if (isEmpty(getValue(params))) return <></>;
  39. return <>{dayjs(getValue(params)).format('YYYY-MM-DD HH:mm:ss')}</>;
  40. },
  41. });
  42. // 字典渲染
  43. VxeUI.renderer.add('CellDict', {
  44. // 默认显示模板
  45. renderTableDefault(renderOpts, params) {
  46. if (isEmpty(getValue(params))) return <></>;
  47. return <bs-dic-tag dicts={getValue(params)}></bs-dic-tag>;
  48. },
  49. });
  50. // 字典渲染
  51. VxeUI.renderer.add('CellOption', {
  52. // 默认显示模板
  53. renderTableDefault(renderOpts, params) {
  54. const { extraProps } = renderOpts;
  55. const { row, column } = params;
  56. const { buttons } = extraProps;
  57. const visibleBtn = buttons.filter(
  58. (v) => !has(v, 'display') || (isFunction(v.display) && v.display(params) === DISPLAY_STATE.VISIBLE) || v.display === DISPLAY_STATE.VISIBLE
  59. );
  60. column.title = '操作';
  61. column.width = 'auto';
  62. const iconMap = {
  63. add: h(PlusOutlined),
  64. edit: h(EditOutlined),
  65. delete: h(DeleteOutlined),
  66. view: h(EyeOutlined),
  67. };
  68. return (
  69. <a-space>
  70. {visibleBtn.map((v) => {
  71. const disabled = has(v, 'disabled') && isFunction(v.disabled) ? v.disabled(params) : v.props?.disabled;
  72. return (
  73. <a-button
  74. style={{ padding: '0' }}
  75. type='link'
  76. onClick={() => {
  77. v?.onClick(params);
  78. }}
  79. disabled={disabled}
  80. icon={iconMap[v?.code]}
  81. {...v.extraProps}
  82. >
  83. {v.title}
  84. </a-button>
  85. );
  86. })}
  87. </a-space>
  88. );
  89. },
  90. });
  91. // 文件类型
  92. VxeUI.renderer.add('CellFileType', {
  93. // 默认显示模板
  94. renderTableDefault(renderOpts, params) {
  95. const { extraProps } = renderOpts;
  96. const { row, column } = params;
  97. const fileTypeName = row[column.field]?.toUpperCase();
  98. const iconName = FILE_TYPE_ICON_MAP[fileTypeName] || 'undefined_icon.svg';
  99. column.title = '文件类型';
  100. column.width = '80';
  101. column.align = 'center';
  102. return <a-image preview={false} width={30} height={30} src={`/src/components/BsUi/images/${iconName}`}></a-image>;
  103. },
  104. });