SetColWidComponent.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import React, { useState, useEffect } from 'react';
  2. import { Modal,Button } from 'antd';
  3. import { SortableContainer, SortableElement, SortableHandle } from 'react-sortable-hoc';
  4. import ProFormDigit from '@ant-design/pro-form/lib/components/Digit';
  5. import { getClolumnTableData } from '@/pages/setting/reportSet/reportSetting/service';
  6. import { arrayMoveImmutable } from 'array-move';
  7. import { ProColumns } from '@ant-design/pro-components';
  8. import { BMSTable } from '@/components/BMSTable';
  9. const SetColWidComponent = ({reportCode,onOkHandle}:{reportCode:string,onOkHandle?:(data:any[])=>void}) => {
  10. const [dataSource, setDataSource] = useState<any[]>([]);
  11. const [isModalVisible, setIsModalVisible] = useState(false);
  12. const urlReportCode = reportCode; // Replace with actual report code
  13. useEffect(() => {
  14. const fetchData = async () => {
  15. const resp = await getClolumnTableData({ reportCode: urlReportCode });
  16. if (resp) {
  17. const dataSorted = resp.sort((prev: any, cur: any) => prev.sort - cur.sort);
  18. setDataSource(dataSorted);
  19. }
  20. };
  21. fetchData();
  22. }, [urlReportCode]);
  23. const SortableItem = SortableElement((props: any) => <tr className="sortable-item" {...props} />);
  24. const SortContainer = SortableContainer((props: any) => <tbody {...props} />);
  25. const DragHandle = SortableHandle(() => <img width={16} style={{ cursor: 'pointer' }} src={require('../../../../static/tuozhuai_icon.png')} alt="" />);
  26. const onSortEnd = ({ oldIndex, newIndex }: { oldIndex: number; newIndex: number }) => {
  27. if (oldIndex !== newIndex) {
  28. const newData = arrayMoveImmutable([...dataSource], oldIndex, newIndex).filter((el) => !!el);
  29. const updatedSortArr = newData.map((item: any, index: number) => ({ ...item, sort: index }));
  30. setDataSource(updatedSortArr);
  31. }
  32. };
  33. const DraggableContainer = (props: any) => (
  34. <SortContainer
  35. useDragHandle
  36. disableAutoscroll
  37. helperClass="row-dragging"
  38. onSortEnd={onSortEnd}
  39. {...props}
  40. />
  41. );
  42. const DraggableBodyRow = (props: any) => {
  43. const { className, style, ...restProps } = props;
  44. const index = dataSource.findIndex((x) => x.id === restProps['data-row-key']);
  45. return <SortableItem index={index} {...restProps} />;
  46. };
  47. const onChangeHandle = (value:any, record: any) => {
  48. const newDataSource = [...dataSource].map((a) => {
  49. if (a.id === record.id) {
  50. return {
  51. ...a,
  52. width: value
  53. };
  54. } else {
  55. return a;
  56. }
  57. });
  58. setDataSource(newDataSource);
  59. };
  60. const modalTableColumns: ProColumns[] = [
  61. {
  62. title: '排序',
  63. dataIndex: 'sort',
  64. width: 60,
  65. className: 'drag-visible',
  66. render: () => <DragHandle />
  67. },
  68. {
  69. title: '列标题',
  70. ellipsis: true,
  71. dataIndex: 'columnHeaderText',
  72. },
  73. {
  74. title: '列名称',
  75. ellipsis: true,
  76. dataIndex: 'columnName',
  77. },
  78. {
  79. title: '列宽',
  80. dataIndex: 'width',
  81. render: (num: any, record: any) => {
  82. return (
  83. <div style={{ display: 'flex', flexDirection: 'row', justifyContent: 'flex-start', alignItems: 'center' }}>
  84. <ProFormDigit
  85. noStyle
  86. width={70}
  87. fieldProps={{
  88. defaultValue: num,
  89. onBlur: (value) => onChangeHandle(value, record)
  90. }}
  91. />
  92. </div>
  93. );
  94. }
  95. }
  96. ];
  97. const showModal = () => {
  98. setIsModalVisible(true);
  99. };
  100. const handleOk = () => {
  101. setIsModalVisible(false);
  102. onOkHandle&&onOkHandle(dataSource)
  103. };
  104. const handleCancel = () => {
  105. setIsModalVisible(false);
  106. };
  107. return (
  108. <div>
  109. <div onClick={showModal}>设置列宽</div>
  110. <Modal
  111. title="设置列宽"
  112. open={isModalVisible}
  113. onOk={handleOk}
  114. onCancel={handleCancel}
  115. width={500}
  116. >
  117. <BMSTable
  118. rowKey='id'
  119. scroll={{ y: 450 }}
  120. pagination={false}
  121. columns={modalTableColumns}
  122. dataSource={dataSource}
  123. components={{
  124. body: {
  125. wrapper: DraggableContainer,
  126. row: DraggableBodyRow,
  127. },
  128. }}
  129. />
  130. </Modal>
  131. </div>
  132. );
  133. };
  134. export default SetColWidComponent;