alfajango / jquery-dynatable

A more-fun, semantic, alternative to datatables
http://www.dynatable.com
Other
2.78k stars 363 forks source link

Sorting based on a type, rather than column name? #283

Open TheOddler opened 6 years ago

TheOddler commented 6 years ago

I have a table with multiple rows of simple numbers and wanted to make them sortable. However, when I give multiple rows the attribute data-dynatable-column="number" they suddenly show the same data... I dud this because the documentation talks about this when making custom sorting functions.

So my table looks something like this:

<table class="grafiek-tabel" id="1">
  <thead>
    <tr>
      <th>Eerste notatie</th>
      <th>Nummer</th>
      <th data-dynatable-column="number">Weken op #1</th>
      <th data-dynatable-column="number">Weken in #100</th>
      <th>Hoogste notatie</th>
    </tr>
  </thead>
  <tbody>
...

The 3rd and 4th row are numbers, but didn't sort properly. For instance, when sorted they were like = 1, 10, 6, 7, ... Sorted as strings, rather than numbers.

So I created a custom reader (as per the doc):

$(".grafiek-tabel").dynatable({
    readers: {
        number: function(cell, record) {
            return Number($(cell).text());
        }
    }
});

But now the 3rd column takes the values of the 4th... Rather than keeping their own but properly sort as numbers. (They do sort as numbers now though, so that's good I guess :P)

Is this possible? I have many tables with many rows, it would be impractical to define a reader for each column name, rather than just one a giving a column some sort of type.