From 96c9c11b5570fc12fa8dbaa580a630c2df279e24 Mon Sep 17 00:00:00 2001 From: afc163 Date: Fri, 26 Aug 2016 15:04:19 +0800 Subject: [PATCH] Make children sort recursively --- components/table/Table.jsx | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/components/table/Table.jsx b/components/table/Table.jsx index c143fc6455..542f3b8a83 100644 --- a/components/table/Table.jsx +++ b/components/table/Table.jsx @@ -59,6 +59,7 @@ export default class Table extends React.Component { onChange: noop, locale: {}, rowKey: 'key', + childrenColumnName: 'children', } static contextTypes = { @@ -717,18 +718,23 @@ export default class Table extends React.Component { return flatArray(this.getCurrentPageData()); } + recursiveSort(data, sorterFn) { + const { childrenColumnName } = this.props; + return data.sort(sorterFn).map(item => (item[childrenColumnName] ? { + ...item, + [childrenColumnName]: this.recursiveSort(item[childrenColumnName], sorterFn), + } : item)); + } + getLocalData() { const state = this.state; - const { dataSource, childrenColumnName } = this.props; + const { dataSource } = this.props; let data = dataSource || []; // 优化本地排序 data = data.slice(0); const sorterFn = this.getSorterFn(); if (sorterFn) { - data = data.sort(sorterFn).map(dataItem => (dataItem[childrenColumnName] ? { - ...dataItem, - [childrenColumnName]: dataItem[childrenColumnName].sort(sorterFn), - } : dataItem)); + data = this.recursiveSort(data, sorterFn); } // 筛选 if (state.filters) {