|
| 1 | +--- |
| 2 | +order: 20 |
| 3 | +title: CTable |
| 4 | +desc: 表格排序 |
| 5 | +--- |
| 6 | + |
| 7 | +```jsx |
| 8 | + |
| 9 | +/** |
| 10 | + * title: 表格排序 |
| 11 | + * desc: 表格排序(指定列默认排序规则:给 columnData 配置 sortBy 参数即可) |
| 12 | + */ |
| 13 | +import React from 'react'; |
| 14 | +import { CTable, Tooltip, Icon } from 'cloud-react'; |
| 15 | + |
| 16 | +const data = new Array(50).fill(1).map((item, index) => ( |
| 17 | + { id: 121410327 + index, name: `手机号优先继续发送${index}`, createTime: '2021/12/14 10:19:02', creator: 'liyuan.meng', num: '12,222', orderNum: '33,342' } |
| 18 | +)) |
| 19 | + |
| 20 | +const columns = [ |
| 21 | + { |
| 22 | + title: '活动ID', |
| 23 | + dataIndex: 'id', |
| 24 | + sortable: true, |
| 25 | + fixed: 'left', |
| 26 | + width: 120, |
| 27 | + sortBy: 'DESC', |
| 28 | + titleTooltipConfig: { |
| 29 | + content: <span>提示信息</span> |
| 30 | + } |
| 31 | + }, |
| 32 | + { |
| 33 | + title: '活动名称', |
| 34 | + dataIndex: 'name', |
| 35 | + sortable: true, |
| 36 | + width: 300 |
| 37 | + }, |
| 38 | + { title: '创建时间', dataIndex: 'createTime', sortable: true, width: 200 }, |
| 39 | + { |
| 40 | + title: '人数', |
| 41 | + dataIndex: 'num', |
| 42 | + align: 'right', |
| 43 | + sortable: true, |
| 44 | + width: 200, |
| 45 | + titleTooltipConfig: { |
| 46 | + content: '提示信息' |
| 47 | + }, |
| 48 | + }, |
| 49 | + { |
| 50 | + title: '订单数', |
| 51 | + dataIndex: 'orderNum', |
| 52 | + align: 'right', |
| 53 | + sortable: true, |
| 54 | + width: 200, |
| 55 | + titleTooltipConfig: { |
| 56 | + content: '订单数提示信息' |
| 57 | + }, |
| 58 | + titleTooltipAlign: 'left', |
| 59 | + }, |
| 60 | + { title: '创建人', dataIndex: 'creator', width: 120, sortable: true, fixed: 'right' } |
| 61 | +]; |
| 62 | + |
| 63 | +export default function CTableDemo() { |
| 64 | + const sort = (data, { sortParams }) => { |
| 65 | + const { dataIndex, sortBy } = sortParams?.allSortColumns?.find(item => item.sortBy) || {}; |
| 66 | + if (dataIndex === 'id') { |
| 67 | + return data.sort((a, b) => sortBy === 'ASC' ? Number(a.id) - Number(b.id) : Number(b.id) - Number(a.id)); |
| 68 | + } |
| 69 | + if (['name', 'createTime', 'num', 'orderNum', 'creator'].includes(dataIndex)) { |
| 70 | + return data.sort((a, b) => sortBy === 'ASC' ? a[dataIndex].localeCompare(b[dataIndex]) : b[dataIndex].localeCompare(a[dataIndex])); |
| 71 | + } |
| 72 | + return data; |
| 73 | + }; |
| 74 | + |
| 75 | + const page = (data, { pageNum, pageSize }) => { |
| 76 | + return JSON.parse(JSON.stringify(data.slice(pageSize * (pageNum - 1), pageSize * pageNum))) |
| 77 | + } |
| 78 | + |
| 79 | + return ( |
| 80 | + <CTable |
| 81 | + style={{ height: 400 }} |
| 82 | + supportPage |
| 83 | + columnData={columns} |
| 84 | + ajaxData={(params) => { |
| 85 | + console.log('所有列排序配置:'); |
| 86 | + console.table(params.sortParams?.allSortColumns?.map(item => { |
| 87 | + return { |
| 88 | + dataIndex: item.dataIndex, |
| 89 | + sortBy: item.sortBy |
| 90 | + } |
| 91 | + })); |
| 92 | + return new Promise(resolve => { |
| 93 | + const sortedData = sort(data, params); |
| 94 | + setTimeout(() => { |
| 95 | + resolve({ totals: sortedData.length, data: page(sortedData, params) }); |
| 96 | + }, 500) |
| 97 | + }) |
| 98 | + }} |
| 99 | + /> |
| 100 | + ); |
| 101 | +} |
| 102 | +``` |
0 commit comments