function 树形表格() {
const { dataSource4, columns4 } = assets.biz
const pipeline = useTablePipeline({ components: fusion })
.input({ dataSource: dataSource4, columns: columns4 })
.primaryKey('id')
.use(
features.treeMode({
defaultOpenKeys: ['4', '4-2'],
clickArea: 'cell',
}),
)
return <BaseTable {...pipeline.getProps()} />
}
function 异步树状表格() {
const { amount, lfl, ratio } = assets.format
const [state, setState] = useState({ isLoading: true, data: [] })
const dataServiceRef = useRef()
useEffect(() => {
dataServiceRef.current = new assets.MockTreeDataService()
dataServiceRef.current.loadRootNodeData().then((rootNodeData) => {
setState({ data: [rootNodeData], isLoading: false })
})
}, [])
function loadNodeDataByParentKey(parentKey) {
setState((prev) => ({ isLoading: true, data: prev.data }))
dataServiceRef.current.loadNodeDataByParentKey(parentKey).then((newData) => {
setState((prev) => {
return {
isLoading: false,
data: prev.data.concat(newData),
}
})
})
}
const [openKeys, onChangeOpenKeys] = useState([])
const pipeline = useTablePipeline({ components: fusion })
.input({
dataSource: state.data,
columns: [
{ name: '名称', code: 'name', width: 180, lock: true },
{ code: 'a', name: '目标收入', render: amount, align: 'right' },
{ code: 'b', name: '实际收入', render: amount, align: 'right' },
{ code: 'd', name: '重点商品收入', render: amount, align: 'right' },
{ code: 'lfl', name: '收入月环比', render: lfl, align: 'right' },
{ code: 'rate', name: '重点商品收入占比', render: ratio, align: 'right' },
],
})
.primaryKey('key')
.mapColumns(([firstCol, ...rest]) => [
firstCol,
...rest,
...rest,
...rest,
...rest,
])
.use(features.buildTree('key', 'parentKey'))
.use(
features.treeMode({
openKeys,
onChangeOpenKeys(nextKeys, key, action) {
if (state.isLoading) {
return
}
onChangeOpenKeys(nextKeys)
const needLoadData = state.data.every((d) => d.parentKey !== key)
if (action === 'expand' && needLoadData) {
loadNodeDataByParentKey(key)
}
},
isLeafNode(node) {
return node.childCount === 0
},
}),
)
return <BaseTable defaultColumnWidth={120} isLoading={state.isLoading} {...pipeline.getProps()} />
}
function 树状表格中的最近展开标记() {
const { ratio } = assets.format
const columns = [
{ code: 'name', name: '数据维度', width: 200 },
{ code: 'shop_name', name: '门店' },
{ code: 'imp_uv_dau_pct', name: '曝光UV占DAU比例', render: ratio, align: 'right' },
{ code: 'app_qty_pbt', name: 'APP件单价', align: 'right' },
{ code: 'all_app_trd_amt_1d', name: 'APP成交金额汇总', align: 'right' },
]
const [state, setState] = useState({
isLoading: true,
data: [],
lastOpenKey: '',
lastCollapseKey: '',
})
useEffect(() => {
assets.cdnData.getAppTrafficData().then((data) => {
setState({ isLoading: false, data, lastOpenKey: 'B2C', lastCollapseKey: '' })
})
}, [])
const [openKeys, onChangeOpenKeys] = useState(['B2C'])
const pipeline = useTablePipeline({ components: fusion })
.input({ columns: columns, dataSource: state.data })
.primaryKey('id')
.use(features.buildTree('id', 'parent_id'))
.use(
features.treeMode({
openKeys,
onChangeOpenKeys(nextKeys, key, action) {
onChangeOpenKeys(nextKeys)
if (action === 'expand') {
setState({ ...state, lastOpenKey: key, lastCollapseKey: '' })
} else {
setState({ ...state, lastOpenKey: '', lastCollapseKey: key })
}
},
}),
)
.appendRowPropsGetter((record) => {
if (record.id === state.lastOpenKey) {
return { className: 'last-open' }
} else if (record.id === state.lastCollapseKey) {
return { className: 'last-collapse' }
}
})
return (
<docHelpers.StyledWebsiteBaseTable defaultColumnWidth={150} isLoading={state.isLoading} {...pipeline.getProps()} />
)
}