我在ReactJS中構建了一個簡單的應用程序,它通過調用某個API與JSON數組一起工作。然後我在表中填充數組的結果。我現在想讓表格的列可以排序。有人可以幫助我。這是我的代碼。如何在ReactJS中對錶進行排序?
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;
我會建議你使用'反應 - 引導 - table'它提供的所有功能,包括通過排序默認選中:http://allenfang.github.io/react-bootstrap-table/start.html列排序示例:http://allenfang.github.io/react-bootstrap-table/example.html#sort –
我也使用自定義功能,例如(單擊表格行來拉rowID並執行某些操作)。所以我不能使用'react-bootstrap-table'。我正在尋找要添加的代碼。 –
由於您是根據數據渲染表格,因此只需對數據進行排序並重新渲染即可。那有意義嗎? – Ted