0

In the html below the last column is being sorted as a string, but I need it to be sorted as decimal. I can seem to find the docs to achive that. The Javascript is from https://github.com/wenzhixin/bootstrap-table

<table id="table" data-toggle="table" data-search="true" data-show-columns="true" class="table table-striped">
   <thead>
    <tr>
      <th data-sortable="true">Account</th>
      <th data-sortable="true">1-30</th>
      <th data-sortable="true">30+</th>
      <th data-sortable="true">60+</th>
      <th data-sortable="true">90+</th>
      <th data-sortable="true">Unit Total</th>
      <th data-sortable="true">Total</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><a href="#">st0121</a></td>
      <td>700.00</td>
      <td>0.00</td>
      <td>0.00</td>
      <td>0.00</td>
      <td>200.00</td>
      <td>920.00</td>
    </tr>
    <tr>
      <td><a href="#">st0122</a></td>
      <td>1,200.00</td>
      <td>0.00</td>
      <td>0.00</td>
      <td>8,000.00</td>
      <td>0.00</td>
      <td>9,200.00</td>
    </tr>
    <tr>
      <td><a href="#">st0123</a></td>
      <td>0.00</td>
      <td>0.00</td>
      <td>0.00</td>
      <td>0.00</td>
      <td>0.00</td>
      <td>200.00</td>
    </tr>
    <tr>
      <td><a href="#">st0124</a></td>
      <td>160.00</td>
      <td>0.00</td>
      <td>0.00</td>
      <td>0.00</td>
      <td>160.00</td>
      <td>360.00</td>
    </tr>
    <tr>
      <td><a href="#">st0125</a></td>
      <td>40.00</td>
      <td>0.00</td>
      <td>0.00</td>
      <td>0.00</td>
      <td>40.00</td>
      <td>400.00</td>
    </tr>
    <tr>
      <td><a href="#">st0126</a></td>
      <td>80.00</td>
      <td>0.00</td>
      <td>0.00</td>
      <td>0.00</td>
      <td>80.00</td>
      <td>480.00</td>
    </tr>

  </tbody>
</table>

The fiddle is https://jsfiddle.net/cmzua2ph/3/

Any ideas/suggestions?

I am also surprised that there is no need for the .ready() initialization. How is that achieved?

OK, I think I am ready for downvotes. :-)

gene
  • 156
  • 3
  • 12
  • I don't know what r u trying to do, i changed your fiddle's value into decimals, and it sort normally. – Marcel Ang Aug 15 '16 at 03:23
  • And it don't need to .ready() initializations, because its already be done in the .js files. – Marcel Ang Aug 15 '16 at 03:25
  • How did you change it into decimals? And is the .ready definition on line 7 of bootstrap-table.js? – gene Aug 15 '16 at 03:30
  • i did not change anything. I put 700.20, 700.10, and 700.05 on first columns (in HTML), and press the sorting arrows, it works. No, its in the last 3 lines. If you scroll down to the last line, you'll see : $('[data-toggle="table"]').bootstrapTable();. That's the initializations. – Marcel Ang Aug 15 '16 at 03:37
  • can you save your version of the fiddle and post a link here? – gene Aug 15 '16 at 03:43
  • Thanks for explaining jquery linkage. – gene Aug 15 '16 at 03:45
  • https://jsfiddle.net/marcelAng/h0wrpp7j/1/ => sort the '1-30' column. – Marcel Ang Aug 15 '16 at 03:47
  • That is a simple column after you removed 1,200.00. Take a look at Total column: 9,200.00 is never 1st or last as it should be. It works fine if the "," is removed, i.e. 9200.00. 9,200.00 screws things up. – gene Aug 15 '16 at 03:50
  • AHHH !, okay, i understand !, the comma is not numeric character !, so the js process it as string. okay, let me think first. – Marcel Ang Aug 15 '16 at 03:52

1 Answers1

1

I don't know how to change in in your HTML,

but my solution is to inject codes into bootstrap-table.js :

On bootstrap-table.js line 952 - 953 you'll see this code :

var aa = getItemField(a, name, that.options.escape),
    bb = getItemField(b, name, that.options.escape),

change it into this code :

var aa = getItemField(a, name, that.options.escape).replace(",",""),
    bb = getItemField(b, name, that.options.escape).replace(",",""),

it basically remove all comma when sorting.

here is the fiddle :

https://jsfiddle.net/h0wrpp7j/2/

Good luck

Marcel Ang
  • 119
  • 1
  • 11
  • I am beginning to think it's a bug in bootstrap-table.js. I am trying to find a better solution - I do not like removing the "," from the sorted information. Thank you for unraveling the JS for me. – gene Aug 16 '16 at 19:45
  • Well, that is one of the way to overcome your problem, i did not dig too deep for the data tables JS. Lol – Marcel Ang Aug 18 '16 at 09:19
  • A better solution could be to reproduce their sorting function, adding this twist, and use it as a custom sort. In this way, we don't modify the library, so we can use cdn for bootstrap-table – qichao_he May 17 '17 at 17:47