ant-design-vue/components/table/util.js

66 lines
1.6 KiB
JavaScript
Raw Normal View History

2019-01-12 11:33:27 +08:00
export function flatArray(data = [], childrenName = 'children') {
const result = [];
const loop = array => {
2018-03-29 22:08:04 +08:00
array.forEach(item => {
if (item[childrenName]) {
2019-01-12 11:33:27 +08:00
const newItem = { ...item };
delete newItem[childrenName];
result.push(newItem);
2018-03-29 22:08:04 +08:00
if (item[childrenName].length > 0) {
2019-01-12 11:33:27 +08:00
loop(item[childrenName]);
2018-03-29 22:08:04 +08:00
}
} else {
2019-01-12 11:33:27 +08:00
result.push(item);
2018-03-29 22:08:04 +08:00
}
2019-01-12 11:33:27 +08:00
});
};
loop(data);
return result;
2018-03-29 22:08:04 +08:00
}
2019-01-12 11:33:27 +08:00
export function treeMap(tree, mapper, childrenName = 'children') {
2018-03-29 22:08:04 +08:00
return tree.map((node, index) => {
2019-01-12 11:33:27 +08:00
const extra = {};
2018-03-29 22:08:04 +08:00
if (node[childrenName]) {
2019-01-12 11:33:27 +08:00
extra[childrenName] = treeMap(node[childrenName], mapper, childrenName);
2018-03-29 22:08:04 +08:00
}
return {
...mapper(node, index),
...extra,
2019-01-12 11:33:27 +08:00
};
});
2018-03-29 22:08:04 +08:00
}
2019-01-12 11:33:27 +08:00
export function flatFilter(tree, callback) {
2018-03-29 22:08:04 +08:00
return tree.reduce((acc, node) => {
if (callback(node)) {
2019-01-12 11:33:27 +08:00
acc.push(node);
2018-03-29 22:08:04 +08:00
}
if (node.children) {
2019-01-12 11:33:27 +08:00
const children = flatFilter(node.children, callback);
acc.push(...children);
2018-03-29 22:08:04 +08:00
}
2019-01-12 11:33:27 +08:00
return acc;
}, []);
2018-03-29 22:08:04 +08:00
}
// export function normalizeColumns (elements) {
// const columns = []
// React.Children.forEach(elements, (element) => {
// if (!React.isValidElement(element)) {
// return
// }
// const column = {
// ...element.props,
// }
// if (element.key) {
// column.key = element.key
// }
// if (element.type && element.type.__ANT_TABLE_COLUMN_GROUP) {
// column.children = normalizeColumns(column.children)
// }
// columns.push(column)
// })
// return columns
// }