简体   繁体   English

React-Bootstrap-Table-Next 对动态表上的字母数字字符串进行排序

[英]React-Bootstrap-Table-Next Sort Alphanumeric Strings on Dynamic Table

I have a dynamic table that generates 70+ columns in React-Bootstrap-Table-Next.我有一个在 React-Bootstrap-Table-Next 中生成 70 多列的动态表。 I'm running into a problem trying to sort the columns in alphanumerical order (some columns are numbers and some columns are letters).我在尝试按字母数字顺序对列进行排序时遇到问题(有些列是数字,有些列是字母)。 The data being fed into the columns are all strings .输入列的数据都是字符串 I'm using the sortFunc but of course it only sorts columns that are numerical strings.我正在使用 sortFunc 但当然它只对数字字符串的列进行排序。 How can I get the sortFunc to sort both?如何让 sortFunc 对两者都进行排序?

This is my function I have tried.这是我试过的 function。 Checkout the sortFunc.签出 sortFunc。

let columns = [];
let headers = Object.keys(this.props.reconDetails[0]); //creating the text for headers
const newColumn = {
   dataField: header,
   text: header,
   sort: true,
   sortFunc: (a, b, order, dataField) => {
      if (order === 'asc') {
          return b - a;
      }
       return a - b;
      };
   columns.push(newColumn);
   this.setState({
     columns
   })
}

Bootstrap Table:引导表:

BootstrapTable
   striped
   headerClasses="tableHeader"
   classes="tableBody"
   wrapperClasses="table-responsive"
   keyField={"Case_Line_Number"}
   data={this.state.RECON_DETAIL}
   columns={this.state.columns}
   />

Found the answer here as it explains it well.在这里找到答案,因为它解释得很好。 The author utilized the localeCompare method.作者使用了 localeCompare 方法。 http://fuzzytolerance.info/blog/2019/07/19/The-better-way-to-do-natural-sort-in-JavaScript/ http://fuzzytolerance.info/blog/2019/07/19/The-better-way-to-do-natural-sort-in-JavaScript/

I solved my problem below:我在下面解决了我的问题:

sortFunc: (a, b, order, dataField) => {
   if (order === 'asc' || !order) {
       return b.localeCompare(a, navigator.languages[0] || navigator.language, {numeric: true, ignorePunctuation: true});
       }
       return a.localeCompare(b, navigator.languages[0] || navigator.language, {numeric: true, ignorePunctuation: true});
       },

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

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