【发布时间】:2019-03-07 03:40:52
【问题描述】:
我正在模态窗口中实现表格排序。我写了这段代码,但它不正确。
<ExtendedModalBar
list={this.state.modalList}
onCancel={this.handleExtendedModalClose}
onSave={(...args) => this.handleSaveFromModal(...args)}
selectedItems={this.state.modalItems}
show={this.state.showExtendedModal}
type={this.state.modalType}
//onClick={this.onSort('', )}
sortColumn={this.onSort('isSecure', this.state.modalList); this.onSort('profile', this.state.modalList)}
/>
我的功能:
onSort = (column, dataToSort) => (event) => {
const direction = this.state.sort.column ? (this.state.sort.direction === 'asc' ? 'desc' : 'asc') : 'desc';
dataToSort.sort((a, b) => {
if ( typeof(a[column]) === 'string' && typeof(b[column]) === 'string' ) {
const nameA = a[column].toUpperCase(); // ignore upper and lowercase
const nameB = b[column].toUpperCase(); // ignore upper and lowercase
if (nameA < nameB ) {
return -1;
}
if (nameA > nameB) {
return 1;
}
// names must be equal
return 0;
}
if( typeof(a[column]) === 'number' && typeof(b[column]) === 'number'){
return a[column] - b[column];
}
if(typeof(a[column]) === 'boolean' && typeof(b[column]) === 'boolean'){
const firstBool = +(a[column]);
const secondBool = +(b[column]);
return firstBool - secondBool;
}
}
);
if (direction === 'desc') {
dataToSort.reverse();
}
this.setState({
sort: {
column,
direction,
}
});
};
我的排序函数接受两个参数,columnName 和 data。如果我只在 onClick 中调用该函数一次,它只会对一列进行排序。如何多次调用函数并传递不同的参数,即不同的列名?
【问题讨论】:
-
您尝试过类似
onClick={() => ... the code in sortColumn...}的方法吗?如果您对相同的组数据调用两次onSort,它是否对它进行了适当的排序? -
有点不懂。我需要将每个 columnName 作为字符串传递给我的 onSort 函数,以便对表的每一列进行排序,而不仅仅是一个确切的列。是的,集合是相同的,但函数也接受列名
-
请看我的功能。我已经编辑了问题
标签: javascript reactjs function sorting events