UNPKG

@schema-render/search-table-react

Version:
79 lines (78 loc) 2.43 kB
import { jsx as _jsx } from "react/jsx-runtime"; import { utils } from "@schema-render/core-react"; import { Table } from "antd"; import { EColumnsKeys } from "../../constants"; import { forEach } from "../../utils/common"; const { isArray, isNil } = utils; /** * 获取默认文案 */ function getSummaryDefaultText(dataKey, summaryText) { // 序号栏显示“合计”文案 if (dataKey === EColumnsKeys.rowNumber) { return summaryText; } // 操作栏默认为空 if (dataKey === EColumnsKeys.actions) { return ''; } // 其他列数据不存在显示中横线 return '-'; } /** * 获取平铺的 columns */ export function getFlattenedColumns(columns) { const flattened = []; function traverse(list) { forEach(list, (item)=>{ if (isArray(item.children)) { traverse(item.children); } else { flattened.push(item); } }); } traverse(columns); return flattened; } /** * 创建 Antd Table 总结栏列表数据 */ function createTableSummaryItems({ flattenedColumns, summaryData, summaryText = '合计', hasRowNumber = false, hasRowSelection = false }) { const resultList = []; let index = -1; if (hasRowSelection) { index++; resultList.push({ index, text: hasRowNumber ? '' : summaryText }); } forEach(flattenedColumns, ({ key, dataIndex })=>{ const strDataIndex = isArray(dataIndex) ? dataIndex.join('.') : dataIndex; const dataKey = String(key || strDataIndex); const content = summaryData[dataKey]; index++; resultList.push({ index, text: isNil(content) ? getSummaryDefaultText(dataKey, summaryText) : content }); }); return resultList; } /** * 创建 antd table 总结栏 */ export function createTableSummary(p) { const items = createTableSummaryItems(p); return /*#__PURE__*/ _jsx(Table.Summary, { fixed: "bottom", children: /*#__PURE__*/ _jsx(Table.Summary.Row, { children: items.map((item)=>{ return /*#__PURE__*/ _jsx(Table.Summary.Cell, { align: "center", index: item.index, colSpan: item.colSpan, children: item.text }, item.index); }) }) }); }