简体   繁体   English

在react-bootstrap-table2中用逗号对数字进行排序

[英]Sorting number with comma in react-bootstrap-table2

Using react-bootstrap-table2, When trying to filter large numbers with commas (12,000) it doesn't work.使用 react-bootstrap-table2,当尝试用逗号 (12,000) 过滤大数字时,它不起作用。 Is it possible to filter numbers with commas?是否可以用逗号过滤数字?

example例子在此处输入图片说明

Cheers!干杯!

Without seeing your code it is hard to say.没有看到你的代码很难说。 But perhaps each cell data in your table is a string, not a number, which could cause sorting weirdness.但也许表格中的每个单元格数据都是字符串,而不是数字,这可能会导致排序异常。 I had same issue before.我以前有同样的问题。 Sort it by number, then populate your table cells with the formatted string version of the number after sort, if that makes sense.按数字对其进行排序,然后在排序后使用数字的格式化字符串版本填充表格单元格,如果这有意义的话。

Let's assume that:让我们假设:

  • your field name is "estimation"您的字段名称是“估计”
  • thousands separator is ' (apostrophe)千位分隔符是 '(撇号)
  • decimal separator is , (comma)小数点分隔符是,(逗号)

You can write your sort function ( sortFunc ) as follows:您可以按如下方式编写排序函数 ( sortFunc ):

{
    dataField: 'estimation',
    text: 'Estimation',
    sort: true,
    sortFunc: (a, b, order, dataField, rowA, rowB) => {
    const numA = parseFloat(a.replace('\'', '').replace(',', '.'));
    const numB = parseFloat(b.replace('\'', '').replace(',', '.'));
    if (order === "asc") {
        return numB - numA;
    }
    return numA - numB;
}

I solved this problem, here is my Bootstrap Table sort function。我解决了这个问题,这里是我的 Bootstrap Table 排序函数。

 sortFunc: (a, b, order) => {
    const numA = parseFloat(a.replace(/\,/g,''), 10)
    const numB = parseFloat(b.replace(/\,/g,''), 10)
    if (order === "asc") {
        return numB - numA;
    }
    return numA - numB;
  }
import ToolkitProvider, { Search } from "react-bootstrap-table2-toolkit";
import paginationFactory from "react-bootstrap-table2-paginator";

First please check number or non-number.首先请检查号码或非号码。

  1. If your array inside number return number else is non-number :如果你的数组里面的 number 返回 number else 是 non-number :

     yourDataArray.map(function (item, x) { item._Number_of_stocks = parseInt(item.Number_of_stocks); });
  2. Sort function in here:排序功能在这里:

     sortFunc: (a, b, order, dataField, rowA, rowB) => { if (order === "asc") { return ( isNaN(rowB._Number_of_stocks) - !isNaN(rowB._Number_of_stocks) ); } return !isNaN(rowB._Number_of_stocks) - isNaN(rowB._Number_of_stocks); },

See: https://github.com/react-bootstrap-table/react-bootstrap-table2/blob/master/docs/columns.md请参阅: https : //github.com/react-bootstrap-table/react-bootstrap-table2/blob/master/docs/columns.md

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

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