| import { get } from 'lodash-es'; |
|
|
| |
| const recursionColumn = (result: any, columnObj: any, columnConfig?: any, extraParams?: any) => { |
| for (const key of Object.keys(columnObj)) { |
| const item = columnObj[key]; |
| let column: any = { |
| key, |
| align: 'center', |
| dataIndex: key, |
| ...columnConfig, |
| }; |
|
|
| |
| if (typeof item === 'string') { |
| column['title'] = item; |
| result.push(column); |
| continue; |
| } |
|
|
| |
| if (item.column) { |
| column = { |
| title: item.title, |
| children: [], |
| }; |
| recursionColumn(column.children, item.column, columnConfig, extraParams); |
| } else { |
| |
| column = { |
| ...column, |
| ...item, |
| }; |
| } |
|
|
| if (column.title.children) { |
| const { fRender } = extraParams || {}; |
| column.title = fRender({ name: item.title.name }, column.title.children); |
| } |
|
|
| result.push(column); |
| } |
| }; |
|
|
| |
| export const getColumns = (columnObj: any, columnConfig?: any, extraParams?: any) => { |
| const result: any = []; |
| |
| recursionColumn(result, columnObj, columnConfig, extraParams); |
| return result; |
| }; |
|
|
| |
| export const getDataSource = ( |
| columnObj: object, |
| dataList: any[] = [], |
| extra: { key?: string; that?: any } = {}, |
| ) => { |
| const dataSource: any[] = []; |
| const { key, that } = extra; |
|
|
| const dealData = (column: any) => { |
| for (const code in column) { |
| const item = column[code]; |
|
|
| |
| if (item.column) { |
| dealData(item.column); |
| continue; |
| } |
|
|
| (dataList || []).forEach((data, index) => { |
| let dataItem = dataSource[index]; |
|
|
| if (!dataItem) { |
| dataItem = { ...data, key: `${key ? data[key] : index}` }; |
| dataSource.push(dataItem); |
| } |
|
|
| if (code.includes('x-operate')) { |
| dataItem[code] = { ...data, that, dataIndex: index }; |
| } else { |
| dataItem[code] = get(data, code, ''); |
| } |
| }); |
| } |
| }; |
|
|
| |
| dealData(columnObj); |
| return dataSource; |
| }; |
|
|
| |
| export const combineDataSource = (data: any[], field: any) => { |
| let count = 0; |
| let index = 1; |
| while (index < data.length) { |
| const item = data.slice(count, count + 1)[0]; |
| if (!item.rowSpan) { |
| item.rowSpan = 1; |
| } |
| if (item[field] === data[index][field]) { |
| |
| item.rowSpan++; |
| data[index].rowSpan = 0; |
| } else { |
| count = index; |
| } |
| index++; |
| } |
| }; |
|
|