|
| 1 | +--- |
| 2 | +order: 1 title: CTable desc: 自定义表格行背景色 |
| 3 | +--- |
| 4 | + |
| 5 | +```jsx |
| 6 | +import React, { useRef, useEffect, useState } from 'react'; |
| 7 | +import { CTable } from 'cloud-react'; |
| 8 | + |
| 9 | +const data = [ |
| 10 | + { id: '121410327', name: '手机号优先继续发送1', createTime: '2021/12/14 10:19:02', creator: 'liyuan.meng' }, |
| 11 | + { id: '121410328', name: 'ouid疲劳度3', createTime: '2021/12/13 15:47:33 ', creator: 'jiaojiao.diao' }, |
| 12 | + { id: '121410329', name: '继续发送手机1', createTime: '2021/12/13 15:36:42', creator: 'nan.run' }, |
| 13 | + { id: '121408294', name: '继续发送手机2', createTime: '2021/12/13 11:14:40', creator: 'xiaotong.fan' }, |
| 14 | + { id: '121407191', name: '继续发送手机3', createTime: '2021/12/13 11:03:05', creator: 'zhenxiao.guo' }, |
| 15 | +]; |
| 16 | + |
| 17 | +const columns = [ |
| 18 | + { title: '活动ID', dataIndex: 'id' }, |
| 19 | + { title: '活动名称', dataIndex: 'name' }, |
| 20 | + { |
| 21 | + title: '创建时间', dataIndex: 'createTime', render: val => { |
| 22 | + return <CTable.TimeTpl value={val} /> |
| 23 | + } |
| 24 | + }, |
| 25 | + { title: '创建人', dataIndex: 'creator' } |
| 26 | +]; |
| 27 | + |
| 28 | +export default function CTableDemo() { |
| 29 | + const ref = useRef(); |
| 30 | + const [activeKey, setActiveKey] = useState(null); |
| 31 | + |
| 32 | + const onClickRow = evt => { |
| 33 | + if (evt.target?.parentNode?.dataset?.rowKey) { |
| 34 | + setActiveKey(evt.target?.parentNode?.dataset?.rowKey); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + useEffect(() => { |
| 39 | + ref.current.addEventListener('click', onClickRow); |
| 40 | + |
| 41 | + return () => { |
| 42 | + ref.current.removeEventListener('click', onClickRow); |
| 43 | + } |
| 44 | + }, []) |
| 45 | + |
| 46 | + return ( |
| 47 | + <div id="demo" ref={ref} className="cloud-table-demo"> |
| 48 | + <CTable |
| 49 | + rowClassName={(row) => { |
| 50 | + if (row.id === activeKey) { |
| 51 | + // 可以自定义类,在业务中自定义色值 |
| 52 | + return "cloud-table-row-lighted"; |
| 53 | + } |
| 54 | + return row.id; |
| 55 | + }} |
| 56 | + columnData={columns} |
| 57 | + ajaxData={{ totals: data.length, data }} |
| 58 | + /> |
| 59 | + </div> |
| 60 | + ); |
| 61 | +} |
| 62 | +``` |
0 commit comments