【问题标题】:How to set a header as the "default sort" in React?如何在 React 中将标题设置为“默认排序”?
【发布时间】:2021-07-07 19:40:45
【问题描述】:

我在我的 React 应用程序中使用 Sorttable 来使我的表的标题可排序。它按预期运行,但我希望能够将默认排序设置为特定标题。 我已经检查了文档 here 并且还尝试了 this 来自类似问题的堆栈溢出响应的解决方案。 我无法让默认排序工作。关于如何在 React 中实现这一点的任何建议;使用 sorttable 还是其他方式?谢谢!

【问题讨论】:

    标签: reactjs html-table sorttable.js


    【解决方案1】:

    我能够在我的数组上使用 .sort() 方法设置默认排序。

    这是一个对属性进行排序的函数。如果您想要升序与降序,您可以将退货顺序从 a 更改为 b。

      function dynamicSort(property) {
        return function (a, b) {
          return b[property] - a[property];
        };
      }
    

    然后使用 sort() 方法以您要排序的属性的名称调用函数。

      trucks.sort(dynamicSort("status"));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-19
      • 1970-01-01
      • 2012-07-03
      • 2021-01-19
      • 1970-01-01
      • 1970-01-01
      • 2020-06-23
      相关资源
      最近更新 更多