antd Table自定义列的显示
image.png

  1. const filterColumn = [0,1,2,3].sort()
  2. const _columns = filterColumn.map(it => columns[it])
  3. 隐藏列
  4. <tr
  5. draggable
  6. onDragStart={onDrag(e,row, index)}
  7. onDrop={onDrop(e, row, index)}
  8. onDragOver={allowDrop(ev)}
  9. >
  10. funciton onDrag(e, row, index) {e.dataTransfer.setData('index',index)}
  11. funciton onDrop(ev, row, index) {
  12. e.preventDefault();
  13. const dragIndex = e.dataTransfer.getData(index)
  14. dragAndDrop(dragIndex, index);
  15. }
  16. funciton allowDrop(e) { e.preventDefalut() }
  17. // 组件抛出 index,用户自己排序
  18. function dragAndDrop(dragIndex, index) {
  19. array.move(dragIndex, index)
  20. const item = arr.splice(dragIndex, 1, arr[index])
  21. arr.splice(index, 1, item) //数组替换
  22. }
  23. 显示序号
  24. const current = 1;
  25. const pageSize = 10;
  26. (rowIndex + 1) + (current - 1) * pageSize
  27. // (rowIndex + 1) + (current* pageSize) * pageSize
  28. 初始化进行排序,后端初始化排序
  29. 排序 asc 升序,排序是个互斥的,这一列排序,下一列就要取消排序
  30. arr.sort((a,b) => a.value > b.value ? 1 : -1)
  31. desc 降序
  32. arr.sort((a,b) => a.value < b.value ? 1 : -1)

ColumnsTable.jsx

  1. import { useState } from 'react';
  2. import { array } from 'prop-types';
  3. import CustomColumn from './CustomColumns';
  4. import TableList from '../TableList';
  5. ColumnsTable.propTypes = {
  6. columns: array.isRequired,
  7. dataSource: array.isRequired,
  8. };
  9. function ColumnsTable({ columns, ...rest }) {
  10. const [state, setState] = useState(columns);
  11. function checkChange(checked) {
  12. const _state = columns.filter(it => checked.includes(it.dataIndex));
  13. setState(_state);
  14. }
  15. return (
  16. <>
  17. <div className='text-right mb8'>
  18. <CustomColumn
  19. columns={columns}
  20. onChange={checkChange}
  21. />
  22. </div>
  23. <TableList
  24. {...rest}
  25. columns={state}
  26. />
  27. </>
  28. );
  29. }
  30. export default ColumnsTable;

组件使用 ColumnsTable

  1. import ColumnsTable from '@/components';
  2. const columns = [
  3. {dataIndex: 'user', title: '用户'},
  4. ];
  5. function App() {
  6. return <ColumnsTable columns={columns}/>
  7. }

CustomColumn.jsx 自定义表头

  1. import React, { memo, useState } from 'react';
  2. import { array, func, number } from 'prop-types';
  3. import { Popover, Checkbox } from 'antd';
  4. import { SettingOutlined } from '@ant-design/icons';
  5. import equal from 'fast-deep-equal';
  6. const { Group } = Checkbox;
  7. const iconStyle = { padding: 8, fontSize: 16 };
  8. CustomColumn.propTypes = {
  9. columns: array.isRequired,
  10. onChange: func.isRequired,
  11. width: number,
  12. };
  13. CustomColumn.defaultProps = {
  14. width: 100, // 下拉的宽度
  15. };
  16. function CustomColumn({ columns, onChange, width }) {
  17. const [checked, setChecked] = useState(initState);
  18. function initState() {
  19. return (columns || []).map(it => it.dataIndex);
  20. }
  21. const options = columns.map(({ title, dataIndex }) => ({ label: title, value: dataIndex }));
  22. const menu = (
  23. <Group
  24. style={{ width }}
  25. options={options}
  26. value={checked}
  27. onChange={onCheck}
  28. />
  29. );
  30. function onCheck(values) {
  31. if (values.length < 1) return;
  32. setChecked(values);
  33. onChange(values);
  34. }
  35. return (
  36. <Popover
  37. trigger='click'
  38. content={menu}
  39. placement='bottomRight'
  40. >
  41. <SettingOutlined style={iconStyle} />
  42. </Popover>
  43. );
  44. }
  45. function propsEqual({ columns }, props) {
  46. return equal(columns, props.columns);
  47. }
  48. export default memo(CustomColumn, propsEqual);

表格组件参考 https://procomponents.ant.design/components/table?current=1&pageSize=5