提交 854642ec 作者: 郁骅焌
import React, { useEffect, useState, useImperativeHandle, useRef } from 'react'; import React, { useEffect, useState, useImperativeHandle, useRef, useMemo } from 'react';
import { ConfigProvider, Table, Card } from 'antd'; import { ConfigProvider, Table, Card } from 'antd';
import { useFullscreen } from '@umijs/hooks'; import { useFullscreen } from '@umijs/hooks';
import TableSearchForm from '@/components/Common/TableSearchForm'; import TableSearchForm from '@/components/Common/TableSearchForm';
...@@ -30,6 +30,7 @@ const initState = { ...@@ -30,6 +30,7 @@ const initState = {
* 8、onMouseLeaveRow * 8、onMouseLeaveRow
* 9、onContextMenuRow * 9、onContextMenuRow
* 10、toolOptionConfig * 10、toolOptionConfig
* 11、hiddeTool 隐藏工具栏
* 表格工具['reload', 'hiddensearch', 'density', 'fullScreen'] * 表格工具['reload', 'hiddensearch', 'density', 'fullScreen']
*/ */
const SageTable = React.forwardRef((props, ref) => { const SageTable = React.forwardRef((props, ref) => {
...@@ -41,7 +42,7 @@ const SageTable = React.forwardRef((props, ref) => { ...@@ -41,7 +42,7 @@ const SageTable = React.forwardRef((props, ref) => {
const [selectedRows, setSelectedRows] = useState([]); const [selectedRows, setSelectedRows] = useState([]);
// 展开keys // 展开keys
const [expandedRowKeys, setExpandedRowKeys] = useState([]) const [expandedRowKeys, setExpandedRowKeys] = useState([]);
const { const {
request, request,
...@@ -69,6 +70,7 @@ const SageTable = React.forwardRef((props, ref) => { ...@@ -69,6 +70,7 @@ const SageTable = React.forwardRef((props, ref) => {
// 工具相关 // 工具相关
toolBarRender: toolBarRenderProps, toolBarRender: toolBarRenderProps,
toolOptionConfig: toolOptionConfigProps, toolOptionConfig: toolOptionConfigProps,
hiddeTool = false,
// 其余表格属性 // 其余表格属性
...tableProps ...tableProps
} = props; } = props;
...@@ -138,9 +140,9 @@ const SageTable = React.forwardRef((props, ref) => { ...@@ -138,9 +140,9 @@ const SageTable = React.forwardRef((props, ref) => {
delete newSearchParams.pageSize; delete newSearchParams.pageSize;
} }
let lastDataSource = res.data ? res.data.slice() : [] let lastDataSource = res.data ? res.data.slice() : [];
if (renderData) { if (renderData) {
lastDataSource = renderData(lastDataSource) lastDataSource = renderData(lastDataSource);
} }
setTableState({ setTableState({
...@@ -157,18 +159,18 @@ const SageTable = React.forwardRef((props, ref) => { ...@@ -157,18 +159,18 @@ const SageTable = React.forwardRef((props, ref) => {
}); });
if (defaultExpandAllRowsProps) { if (defaultExpandAllRowsProps) {
const allKeys = [] const allKeys = [];
const allData = lastDataSource.slice() const allData = lastDataSource.slice();
const filterExpandRowKeys = (list) => { const filterExpandRowKeys = (list) => {
list.forEach(item => { list.forEach((item) => {
if (item.children && item.children.length !== 0) { if (item.children && item.children.length !== 0) {
allKeys.push(item[props.rowKey]) allKeys.push(item[props.rowKey]);
filterExpandRowKeys(item.children) filterExpandRowKeys(item.children);
} }
}) });
} };
filterExpandRowKeys(allData) filterExpandRowKeys(allData);
setExpandedRowKeys(allKeys) setExpandedRowKeys(allKeys);
} }
} }
} }
...@@ -239,16 +241,34 @@ const SageTable = React.forwardRef((props, ref) => { ...@@ -239,16 +241,34 @@ const SageTable = React.forwardRef((props, ref) => {
const paginationState = paginationProps !== undefined ? paginationProps : tableState.pagination; const paginationState = paginationProps !== undefined ? paginationProps : tableState.pagination;
const loadingState = loadingProps !== undefined ? loadingProps : loading; const loadingState = loadingProps !== undefined ? loadingProps : loading;
// 是否有序号 // 是否有序号
const columnsState = columnsProps !== undefined ? columnsProps.slice() : []; // const columnsState = columnsProps !== undefined ? columnsProps.slice() : [];
const getLatestColumnsState = () => {
let columnsState = columnsProps !== undefined ? columnsProps.slice() : [];
// 处理列配置
if (hasNumber) { if (hasNumber) {
columnsState.unshift({ columnsState.unshift({
title: '序号', title: '序号',
render: (text, record, index) => render: (text, record, index) => {
`${(paginationState.current - 1) * paginationState.pageSize + index + 1}`, let serialNumber = 0;
if (paginationState) {
serialNumber = `${
(paginationState.current - 1) * paginationState.pageSize + index + 1
}`;
} else {
serialNumber = index + 1;
}
return serialNumber;
},
align: 'center', align: 'center',
width: 60, width: 60,
}); });
} }
// 隐藏hidden属性列
columnsState = columnsState.filter((item) => !item.hidden);
return columnsState;
};
const latestColumnsState = useMemo(() => getLatestColumnsState(), [props.columns, tableState]);
const tableSearchFormProps = { const tableSearchFormProps = {
searchFields: searchFieldsProps, searchFields: searchFieldsProps,
...@@ -280,56 +300,84 @@ const SageTable = React.forwardRef((props, ref) => { ...@@ -280,56 +300,84 @@ const SageTable = React.forwardRef((props, ref) => {
const getExpandedRowKeys = () => { const getExpandedRowKeys = () => {
return expandedRowKeys; return expandedRowKeys;
} };
const getAllExpandedRowKeys = () => { const getAllExpandedRowKeys = () => {
const allKeys = [] const allKeys = [];
const allData = dataSourceState.slice() const allData = dataSourceState.slice();
const filterExpandRowKeys = (list) => { const filterExpandRowKeys = (list) => {
list.forEach(item => { list.forEach((item) => {
if (item.children && item.children.length !== 0) { if (item.children && item.children.length !== 0) {
allKeys.push(item[props.rowKey]) allKeys.push(item[props.rowKey]);
filterExpandRowKeys(item.children) filterExpandRowKeys(item.children);
} }
}) });
} };
filterExpandRowKeys(allData) filterExpandRowKeys(allData);
return allKeys; return allKeys;
} };
if (expandableProps) { if (expandableProps) {
lastTableState.expandable = { lastTableState.expandable = {
expandedRowKeys, expandedRowKeys,
onExpand: (expanded, record) => { onExpand: (expanded, record) => {
const lastExpandedRowKeys = expandedRowKeys.slice() const lastExpandedRowKeys = expandedRowKeys.slice();
if (expanded) { if (expanded) {
lastExpandedRowKeys.push(record[props.rowKey]) lastExpandedRowKeys.push(record[props.rowKey]);
setExpandedRowKeys(lastExpandedRowKeys) setExpandedRowKeys(lastExpandedRowKeys);
} else { } else {
const deleteIndex = lastExpandedRowKeys.findIndex(item => item === record[props.rowKey]) const deleteIndex = lastExpandedRowKeys.findIndex(
lastExpandedRowKeys.splice(deleteIndex, 1) (item) => item === record[props.rowKey],
} );
setExpandedRowKeys(lastExpandedRowKeys) lastExpandedRowKeys.splice(deleteIndex, 1);
}
} }
setExpandedRowKeys(lastExpandedRowKeys);
},
};
} }
// 获取当前列表的数据 // 获取当前列表的数据
const getDataSource = () => { const getDataSource = () => {
return dataSourceState return dataSourceState;
} };
// 给列表赋值 // 给列表赋值
const setDataSource = (data) => { const setDataSource = (data) => {
const tableStateTemp = Object.assign({}, tableState) const tableStateTemp = Object.assign({}, tableState);
let lastDataSource = data.slice() let lastDataSource = data.slice();
if (renderData) { if (renderData) {
lastDataSource = renderData(lastDataSource) lastDataSource = renderData(lastDataSource);
}
tableStateTemp.dataSource = lastDataSource;
setTableState(tableStateTemp);
};
// 根据某个属性修改值
const setDataSourceByMapKey = (
fieldKeyName,
fieldKeyValue,
changeFieldKeyName,
changeFieldKeyValue,
) => {
const dataSourceTemp = tableState.dataSource ? tableState.dataSource.slice() : [];
const loopDataSource = (list) => {
for (let i = 0; i < list.length; i++) {
if (list[i].children && list[i].children.length !== 0) {
loopDataSource(list[i].children);
}
if (list[i][fieldKeyName] === fieldKeyValue) {
list[i][changeFieldKeyName] = changeFieldKeyValue;
break;
} }
tableStateTemp.dataSource = lastDataSource
setTableState(tableStateTemp)
} }
};
loopDataSource(dataSourceTemp);
const tableStateTemp = Object.assign({}, tableState);
tableStateTemp.dataSource = dataSourceTemp;
setTableState(tableStateTemp);
};
// 暴露给外部的方法 // 暴露给外部的方法
useImperativeHandle(ref, () => ({ useImperativeHandle(ref, () => ({
...@@ -345,6 +393,7 @@ const SageTable = React.forwardRef((props, ref) => { ...@@ -345,6 +393,7 @@ const SageTable = React.forwardRef((props, ref) => {
getAllExpandedRowKeys, getAllExpandedRowKeys,
getExpandedRowKeys, getExpandedRowKeys,
setExpandedRowKeys, setExpandedRowKeys,
setDataSourceByMapKey,
})); }));
return ( return (
...@@ -363,11 +412,13 @@ const SageTable = React.forwardRef((props, ref) => { ...@@ -363,11 +412,13 @@ const SageTable = React.forwardRef((props, ref) => {
<TableSearchForm ref={tableSearchFormRef} {...tableSearchFormProps} /> <TableSearchForm ref={tableSearchFormRef} {...tableSearchFormProps} />
)} )}
{!hiddeTool ? (
<TableTool tableSize={tableSizeState} isFullscreen={isFullscreen} {...tableToolProps} /> <TableTool tableSize={tableSizeState} isFullscreen={isFullscreen} {...tableToolProps} />
) : null}
<Table <Table
size={tableSizeState} size={tableSizeState}
columns={columnsState} columns={latestColumnsState}
dataSource={dataSourceState} dataSource={dataSourceState}
pagination={paginationState} pagination={paginationState}
loading={loadingState} loading={loadingState}
......
...@@ -120,7 +120,7 @@ const GlobalModel = { ...@@ -120,7 +120,7 @@ const GlobalModel = {
*returnTab({ payload }, { put, select }) { *returnTab({ payload }, { put, select }) {
const globalState = yield select(state => state.global) const globalState = yield select(state => state.global)
dropByCacheKey(payload.closePath) // dropByCacheKey(payload.closePath)
const tabPanes = globalState.tabPanes.slice() const tabPanes = globalState.tabPanes.slice()
let removeIndex; let removeIndex;
......
This source diff could not be displayed because it is too large. You can view the blob instead.
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论