如何使用 ReactJS 以两种方式在 table sortable 中创建列
How to make columns in table sortable in both ways using ReactJS
我正在 ReactJS 中构建一个简单的应用程序,它通过调用某个 API 来处理 JSON 数组。然后我将数组的结果填充到 table 中。我现在想制作 table sortable 的列。我最理想的是同时进行升序和降序排序。一旦我点击 header,当它按升序排列时,它应该按降序排列 vice-versa。这是我的代码。
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;
您可以使用 column
和 direction
属性向您的州添加 sort
属性:
state = {
data: [],
sort: {
column: null,
direction: 'desc',
},
};
当然你也应该有一个像这样的排序处理程序:
onSort = (column) => (e) => {
const direction = this.state.sort.column ? (this.state.sort.direction === 'asc' ? 'desc' : 'asc') : 'desc';
const sortedData = this.state.data.sort((a, b) => {
if (column === 'accountName') {
const nameA = a.accountName.toUpperCase(); // ignore upper and lowercase
const nameB = b.accountName.toUpperCase(); // ignore upper and lowercase
if (nameA < nameB) {
return -1;
}
if (nameA > nameB) {
return 1;
}
// names must be equal
return 0;
} else {
return a.contractValue - b.contractValue;
}
});
if (direction === 'desc') {
sortedData.reverse();
}
this.setState({
data: sortedData,
sort: {
column,
direction,
}
});
};
我从 MDN 得到排序。
我正在 ReactJS 中构建一个简单的应用程序,它通过调用某个 API 来处理 JSON 数组。然后我将数组的结果填充到 table 中。我现在想制作 table sortable 的列。我最理想的是同时进行升序和降序排序。一旦我点击 header,当它按升序排列时,它应该按降序排列 vice-versa。这是我的代码。
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;
您可以使用 column
和 direction
属性向您的州添加 sort
属性:
state = {
data: [],
sort: {
column: null,
direction: 'desc',
},
};
当然你也应该有一个像这样的排序处理程序:
onSort = (column) => (e) => {
const direction = this.state.sort.column ? (this.state.sort.direction === 'asc' ? 'desc' : 'asc') : 'desc';
const sortedData = this.state.data.sort((a, b) => {
if (column === 'accountName') {
const nameA = a.accountName.toUpperCase(); // ignore upper and lowercase
const nameB = b.accountName.toUpperCase(); // ignore upper and lowercase
if (nameA < nameB) {
return -1;
}
if (nameA > nameB) {
return 1;
}
// names must be equal
return 0;
} else {
return a.contractValue - b.contractValue;
}
});
if (direction === 'desc') {
sortedData.reverse();
}
this.setState({
data: sortedData,
sort: {
column,
direction,
}
});
};
我从 MDN 得到排序。