0

Using react-bootstrap-table2, When trying to filter large numbers with commas (12,000) it doesn't work. Is it possible to filter numbers with commas?

example enter image description here

Cheers!

Gearóid
  • 179
  • 1
  • 3
  • 11

4 Answers4

2

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.

mediaguru
  • 1,807
  • 18
  • 24
  • Thanks, Yeah that makes sense. I was hoping the react-bootstrap-table plugin would have a setting that takes care this. – Gearóid Nov 12 '18 at 10:20
0

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:

{
    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;
}
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Luca Natali
  • 349
  • 6
  • 17
0

I solved this problem, here is my Bootstrap Table sort function。

 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;
  }
-1
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 :

    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

fcdt
  • 2,371
  • 5
  • 14
  • 26