是否有一种方法可以使语义UI表排序在多个组件的重呈现中保持持久。例如,如果我按列“名称”对升序进行排序,即使在表父组件重新呈现时,是否也有一种方法可以使此排序暂停应用?
有没有一种在不影响表实现的情况下做到这一点的方法?
发布于 2018-12-15 11:49:44
根据您的需求,我为you.And创建了一个示例--输出如图中所示。

底部的输入和复选框是模拟用户添加data.After的按钮,点击“提交”按钮,数据将添加到表中,以便automatically.The方法处理数据添加,如下所示:
addDate1 = () => {
const { column, data, direction} = this.state
let addData = {
name: this.state.inputName,
age: this.state.inputAge,
gender: this.state.gender
}
let newData = [...data,addData]
if (!column){
console.log('Please select a colume')
} else if (column === 'name'){
this.setState({
column: 'name',
data: _.sortBy(newData, ['name']),
direction: 'ascending',
})
} else if (column === 'age'){
this.setState({
column: 'age',
data: _.sortBy(newData, ['age']),
direction: 'ascending',
})
} else if (column === 'gender'){
this.setState({
column: 'gender',
data: _.sortBy(newData, ['gender']),
direction: 'ascending',
})
} else {
console.log('error')
}
}工作示例在这里:https://codesandbox.io/s/github/stackOverflow166/setable
发布于 2018-12-14 13:23:43
当然,您可以在本地状态/redux状态下将列名设置为排序(IE:" name ")和方向("ASC"/"DESC"),然后根据每个重新加载的状态应用排序。
这一点在DOCS中得到了很好的证明:
import _ from 'lodash'
import React, { Component } from 'react'
import { Table } from 'semantic-ui-react'
const tableData = [
{ name: 'John', age: 15, gender: 'Male' },
{ name: 'Amber', age: 40, gender: 'Female' },
{ name: 'Leslie', age: 25, gender: 'Female' },
{ name: 'Ben', age: 70, gender: 'Male' },
]
export default class TableExampleSortable extends Component {
state = {
column: null,
data: tableData,
direction: null,
}
handleSort = clickedColumn => {
const { column, data, direction } = this.state
if (column !== clickedColumn) {
this.setState({
column: clickedColumn,
data: _.sortBy(data, [clickedColumn]),
direction: 'ascending',
})
return
}
this.setState({
data: data.reverse(),
direction: direction === 'ascending' ? 'descending' : 'ascending',
})
}
render() {
const { column, data, direction } = this.state
return (
<Table sortable celled fixed>
<Table.Header>
<Table.Row>
<Table.HeaderCell
sorted={column === 'name' ? direction : null}
onClick={this.handleSort('name')}
>
Name
</Table.HeaderCell>
<Table.HeaderCell
sorted={column === 'age' ? direction : null}
onClick={this.handleSort('age')}
>
Age
</Table.HeaderCell>
<Table.HeaderCell
sorted={column === 'gender' ? direction : null}
onClick={this.handleSort('gender')}
>
Gender
</Table.HeaderCell>
</Table.Row>
</Table.Header>
<Table.Body>
{_.map(data, ({ age, gender, name }) => (
<Table.Row key={name}>
<Table.Cell>{name}</Table.Cell>
<Table.Cell>{age}</Table.Cell>
<Table.Cell>{gender}</Table.Cell>
</Table.Row>
))}
</Table.Body>
</Table>
)
}
}参考文献:https://react.semantic-ui.com/collections/table/#variations-sortable
https://stackoverflow.com/questions/53741503
复制相似问题