【问题标题】:How to make a table in ReactJS sortable?如何使 ReactJS 中的表格可排序?
【发布时间】: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


【解决方案1】:

根据我的评论,这是一个简单的示例:

class ParentComponent extends Component {
  constructor(props) {
    super(props);
    this.state = { data: [] };
    this.onSort = this.onSort.bind(this)
  }

  componentDidMount() {
    fetch("http://hostname:xxxx/yyyy/zzzz")
      .then(function(response) {
        return response.json();
      })
      .then(items => this.setState({ data: items }));
  }

  onSort(event, sortKey){
    /*
    assuming your data is something like
    [
      {accountname:'foo', negotiatedcontractvalue:'bar'},
      {accountname:'monkey', negotiatedcontractvalue:'spank'},
      {accountname:'chicken', negotiatedcontractvalue:'dance'},
    ]
    */
    const data = this.state.data;
    data.sort((a,b) => a[sortKey].localeCompare(b[sortKey]))
    this.setState({data})
  }

  render() {
    var newdata = this.state.data;

    return (
      <table className="m-table">
        <thead>
          <tr>
            <th onClick={e => this.onSort(e, 'accountname')}>AccountName</th>
            <th onClick={e => this.onSort(e, 'negotiatedcontractvalue')}>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;

【讨论】:

  • 谢谢。但我理想中想要的是同时进行升序和降序排序。一旦我单击按升序排序的标题,它应该按降序排序,反之亦然。
  • @MidhunMathewSunny 您应该能够从示例中弄清楚如何做到这一点...试一试,如果您无法使其正常工作,请发布另一个问题
  • 我已经按照你的要求添加了另一个问题。我没有太多时间去探索。如果您能快速发布答案,那就太好了。 [stackoverflow.com/questions/44402937/…
  • @PedroJR 对,因为它在类组件中使用本地状态。您可以通过使用状态挂钩或将其提取到您选择的存储中来使其在功能组件中工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-08
  • 2014-04-18
  • 2016-06-06
  • 2018-10-06
  • 2021-08-22
  • 2017-12-31
  • 2018-05-10
相关资源
最近更新 更多