Skip to content

Commit a52ef36

Browse files
committed
✨ [CTable] 表格提供默认列模板
1 parent 3a49cde commit a52ef36

10 files changed

Lines changed: 343 additions & 64 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import React from 'react';
2+
import PropTypes from 'prop-types';
3+
import NumberTpl from './number';
4+
import TimeTpl from './time';
5+
import { isVoid } from '../util';
6+
import { NUMBER, TIME } from '../constant';
7+
8+
const components = {
9+
[NUMBER]: NumberTpl,
10+
[TIME]: TimeTpl,
11+
};
12+
13+
export default function ColumnTpl({ value, type, typeConfig = {} }) {
14+
const Com = components[type];
15+
if (Com) {
16+
return <Com value={value} {...typeConfig} />;
17+
}
18+
return isVoid(value) ? '-' : value;
19+
}
20+
21+
ColumnTpl.propTypes = {
22+
numberConfig: PropTypes.object,
23+
timeConfig: PropTypes.object,
24+
};
25+
26+
ColumnTpl.defaultProps = {
27+
numberConfig: {},
28+
};
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import PropTypes from 'prop-types';
2+
import { formatThousands, isVoid } from '../util';
3+
4+
export default function NumberTpl({
5+
value,
6+
precision = 0,
7+
isThousands = true,
8+
prefix = '',
9+
suffix = '',
10+
}) {
11+
let number = value;
12+
if (isVoid(number)) {
13+
return '-';
14+
}
15+
if (precision > 0) {
16+
number = Number(number).toFixed(precision);
17+
}
18+
if (isThousands) {
19+
number = formatThousands(number);
20+
}
21+
return `${prefix || ''}${number}${suffix || ''}`;
22+
}
23+
24+
NumberTpl.propTypes = {
25+
value: PropTypes.oneOfType([PropTypes.number, PropTypes.string]).isRequired,
26+
precision: PropTypes.number, // 小数位数
27+
isThousands: PropTypes.bool, // 是否使用千分位格式
28+
prefix: PropTypes.string, // 前缀
29+
suffix: PropTypes.string, // 后缀
30+
};
31+
32+
NumberTpl.defaultProps = {
33+
precision: 0,
34+
isThousands: true,
35+
prefix: '',
36+
suffix: '',
37+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import PropTypes from 'prop-types';
2+
import moment from 'moment';
3+
import { isVoid } from '../util';
4+
5+
export default function TimeTpl({ value, format }) {
6+
if (isVoid(value)) {
7+
return '-';
8+
}
9+
if (format) {
10+
return moment(value).format(format);
11+
}
12+
return `${value}`.replaceAll('/', '-');
13+
}
14+
15+
TimeTpl.propTypes = {
16+
value: PropTypes.string.isRequired,
17+
format: PropTypes.string,
18+
};
19+
20+
TimeTpl.defaultProps = {
21+
format: '',
22+
};
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import React from 'react';
2+
import PropTypes from 'prop-types';
3+
import moment from 'moment';
4+
import { isVoid } from '../util';
5+
6+
export default function TimeRangeTpl({ value, format }) {
7+
if (isVoid(value)) {
8+
return '-';
9+
}
10+
if (format) {
11+
return (
12+
<div>
13+
<p>
14+
起:
15+
{moment(value).format(format)}
16+
</p>
17+
<p>
18+
止:
19+
{moment(value).format(format)}
20+
</p>
21+
</div>
22+
);
23+
}
24+
return `${value}`.replaceAll('/', '-');
25+
}
26+
27+
TimeRangeTpl.propTypes = {
28+
value: PropTypes.string.isRequired,
29+
format: PropTypes.string,
30+
};
31+
32+
TimeRangeTpl.defaultProps = {
33+
format: '',
34+
};

src/components/c-table/constant.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@ export const tablePrefixCls = `${prefixCls}-table`;
55
export const DRAG_SELECTOR = `.${tablePrefixCls}-row`;
66

77
export const DRAG_ICON_SELECTOR = `.${tablePrefixCls}-drag-column`;
8+
9+
export const NUMBER = 'NUMBER'; // 数字类型
10+
export const TIME = 'TIME'; // 时间类型
11+
export const TIME_RANGE = 'TIME_RANGE'; // 时间范围类型
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
---
2+
order: 1
3+
title: CTable
4+
desc: 默认表格
5+
---
6+
7+
```jsx
8+
import React from 'react';
9+
import { CTable } from 'cloud-react';
10+
11+
const data = [
12+
{ number: '1234455', name: '手机号优先继续发送1', createTime: '2021/12/14 10:19:02', creator: 'liyuan.meng', num: '12,222' },
13+
{ number: 122, name: 'ouid疲劳度3', createTime: '2021/12/13 15:47:33 ', creator: 'jiaojiao.diao', num: '198' },
14+
{ number: 12243, name: '继续发送手机1', createTime: '2021/12/13 15:36:42', creator: 'nan.run', num: '1,232' },
15+
{ number: 2345, name: '继续发送手机2', createTime: '2021/12/13 11:14:40', creator: 'xiaotong.fan', num: '12,122,112' },
16+
{ number: 1231322, name: '继续发送手机3', createTime: '2021/12/13 11:03:05', creator: 'zhenxiao.guo', num: '1000,000' },
17+
];
18+
19+
const columns = [
20+
{
21+
title: '客户数',
22+
dataIndex: 'number',
23+
width: 120,
24+
type: 'NUMBER'
25+
},
26+
{
27+
title: '金额',
28+
dataIndex: 'number',
29+
width: 130,
30+
type: 'NUMBER',
31+
typeConfig: {
32+
prefix: '¥ ',
33+
precision: 2,
34+
}
35+
},
36+
{ title: '人数', dataIndex: 'num', align: 'right', width: 120 },
37+
{ title: '创建人', dataIndex: 'creator', width: 130 }
38+
];
39+
40+
export default function CTableDemo() {
41+
return (
42+
<CTable
43+
style={{ height: 400 }}
44+
columnData={columns}
45+
ajaxData={{ totals: data.length, data }}
46+
/>
47+
);
48+
}
49+
```
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
---
2+
order: 1
3+
title: CTable
4+
desc: 默认表格
5+
---
6+
7+
```jsx
8+
import React from 'react';
9+
import { CTable } from 'cloud-react';
10+
11+
const data = [
12+
{ number: '1234455', name: '手机号优先继续发送1', createTime: '2021/12/14 10:19:02', creator: 'liyuan.meng', num: '12,222' },
13+
{ number: 122, name: 'ouid疲劳度3', createTime: '2021/12/13 15:47:33 ', creator: 'jiaojiao.diao', num: '198' },
14+
{ number: 12243, name: '继续发送手机1', createTime: '2021/12/13 15:36:42', creator: 'nan.run', num: '1,232' },
15+
{ number: 2345, name: '继续发送手机2', createTime: '2021/12/13 11:14:40', creator: 'xiaotong.fan', num: '12,122,112' },
16+
{ number: 1231322, name: '继续发送手机3', createTime: '2021/12/13 11:03:05', creator: 'zhenxiao.guo', num: '1000,000' },
17+
];
18+
19+
const columns = [
20+
{
21+
title: '年月日时分秒',
22+
dataIndex: 'createTime',
23+
width: 190,
24+
type: 'TIME',
25+
},
26+
{
27+
title: '年月日',
28+
dataIndex: 'createTime',
29+
width: 130,
30+
type: 'TIME',
31+
typeConfig: {
32+
format: 'YYYY-MM-DD',
33+
}
34+
},
35+
{
36+
title: '年月',
37+
dataIndex: 'createTime',
38+
width: 90,
39+
type: 'TIME',
40+
typeConfig: {
41+
format: 'YYYY-MM',
42+
}
43+
},
44+
{
45+
title: '月日',
46+
dataIndex: 'createTime',
47+
width: 90,
48+
type: 'TIME',
49+
typeConfig: {
50+
format: 'MM-DD',
51+
}
52+
},
53+
{
54+
title: '时分秒',
55+
dataIndex: 'createTime',
56+
width: 100,
57+
type: 'TIME',
58+
typeConfig: {
59+
format: 'HH:MM:SS',
60+
}
61+
},
62+
];
63+
64+
export default function CTableDemo() {
65+
return (
66+
<CTable
67+
style={{ height: 400 }}
68+
columnData={columns}
69+
ajaxData={{ totals: data.length, data }}
70+
/>
71+
);
72+
}
73+
```

src/components/c-table/index.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ import {
1717
debounce,
1818
hasCustomScroll,
1919
} from './util';
20-
import { DRAG_ICON_SELECTOR, DRAG_SELECTOR, tablePrefixCls } from './constant';
20+
import {
21+
DRAG_ICON_SELECTOR,
22+
DRAG_SELECTOR,
23+
NUMBER,
24+
tablePrefixCls,
25+
} from './constant';
2126
import getExpandableConfig from './js/expend';
2227
import ResizableTitle from './js/resizableTitle';
2328
import Pagination from '../pagination';
@@ -29,6 +34,7 @@ import './css/business.less';
2934
import Column from './js/column';
3035
import RowTooltip from './js/rowTooltip';
3136
import { defaultProps, propTypes } from './js/propType';
37+
import ColumnTpl from './columnTpl';
3238

3339
class CTable extends Component {
3440
ref = createRef();
@@ -148,13 +154,19 @@ class CTable extends Component {
148154
};
149155

150156
resolveColumn = (columnData) => {
151-
return columnData.map((item) => ({ ...item, show: true }));
157+
return columnData.map((item) => ({
158+
render: (val, row) => <ColumnTpl value={val} row={row} {...item} />,
159+
...item,
160+
show: true,
161+
align: item.type === NUMBER ? 'right' : 'left',
162+
}));
152163
};
153164

154165
resolveOriginColumn = (columnData) => {
155166
return (
156167
(this.props.supportMemory && getConfig(this.props.tableId)) ||
157168
columnData.map((item) => ({
169+
render: (val, row) => <ColumnTpl value={val} row={row} {...item} />,
158170
...item,
159171
show: true,
160172
}))

0 commit comments

Comments
 (0)