【发布时间】:2017-11-06 14:36:24
【问题描述】:
我正在 ReactJS 中构建一个简单的应用程序,它通过调用某个 API 来处理 JSON 数组。然后我将数组的结果填充到一个表中。我现在想让表格的列可排序。有人可以帮我吗。这是我的代码。
class ParentComponent extends Component {
constructor(props) {
super(props);
this.state = { data: [] };
}
componentDidMount() {
fetch("http://hostname:xxxx/yyyy/zzzz")
.then(function(response) {
return response.json();
})
.then(items => this.setState({ data: items }));
}
render() {
var newdata = this.state.data;
return (
<table className="m-table">
<thead>
<tr>
<th>AccountName</th>
<th>ContractValue</th>
</tr>
</thead>
<tbody>
{newdata.map(function(account, index) {
return (
<tr key={index} data-item={account}>
<td data-title="Account">{account.accountname}</td>
<td data-title="Value">{account.negotiatedcontractvalue}</td>
</tr>
);
})}
</tbody>
</table>
);
}
}
export default ParentComponent;
【问题讨论】:
-
我建议你使用
react-bootstrap-table,它默认提供了包括排序在内的所有功能,请检查:allenfang.github.io/react-bootstrap-table/start.html 列排序示例:allenfang.github.io/react-bootstrap-table/example.html#sort -
我也在使用自定义功能,例如(单击表格行以提取 rowID 并执行某些操作)。所以我不能使用
react-bootstrap-table。我正在寻找要添加的代码。 -
由于您是从数据中渲染表格,您只需对数据进行排序并重新渲染。这有意义吗?
-
我想从表头排序。我也想在其他表格中使用该功能
标签: javascript jquery reactjs