简体   繁体   English

如何在 React 中将 header 设置为“默认排序”?

[英]How to set a header as the "default sort" in React?

I'm using Sorttable in my React application to make the headers of my table sortable.我在我的 React 应用程序中使用 Sorttable 来使我的表的标题可排序。 It's functioning as expected but I'd like to be able to set the default sort to a specific header. I've checked the documentation here and have also attempted this solution from a stack overflow response on a similar question.它按预期运行,但我希望能够将默认排序设置为特定的 header。我已经检查了此处的文档,并且还尝试从类似问题的堆栈溢出响应中尝试解决方案。 I was unable to get the default sort to work.我无法使用默认排序。 Any advice on how I can accomplish this in React;关于如何在 React 中完成此操作的任何建议; either using sorttable or otherwise?使用 sorttable 还是其他方式? Thanks!谢谢!

I was able to set the default sort using the.sort() method on my array.我能够使用数组上的 .sort() 方法设置默认排序。

Here is a function to sort the properties.这里有一个function来排序属性。 You can change the return order from a to b if you'd like to have ascending vs descending order.如果您想要升序与降序,您可以将退货顺序从 a 更改为 b。

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

Then use the sort() method to call the function with the name of the property you'd like to sort by.然后使用 sort() 方法调用 function 以及您要作为排序依据的属性的名称。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM