【发布时间】:2020-11-09 02:46:18
【问题描述】:
我使用对象数组创建了表,现在我不明白我们如何通过单击 reactjs 中的列名按升序和降序对表进行排序。我在stackoverflow上尝试了很多概念,但它似乎对这个没有用。这是一个非常愚蠢的问题,但我已经停止了很多天。
class Hello extends Component {
constructor(props) {
super(props)
this.state = {
search: '',
Data:[
{
id: 1,
fullName: 'abc',
email:'example@gmail.com',
},
{
id: 2,
fullName: 'qps',
email:'qps@gmail.com',
},
{
id: 3,
fullName: 'qwe',
email:'qwe@gmail.com',
},
]
}
}
function Sort(){
//what need to write here;
}
render() {
return (
<div>
<h1>welcome to React</h1>
<table className="table table-hover table-dark">
<tbody>
<tr>
<th onChange={this.Sort.bind(this)}>ID</th>
<th>Full Name</th>
<th>Email</th>
</tr>
{
this.state.Data.map((item,index)=>(
<tr key={item.id}>
<td >{item.id}</td>
<td >{item.fullName}</td>
<td>{item.email}</td>
</tr>
))
}
</tbody>
</table>
</div>
)
}
}
export default Hello
【问题讨论】:
-
您想按什么排序,电子邮件、id 还是全名?
标签: javascript arrays reactjs sorting