Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Incorrect / inconsistent value used for filtering before / after sorting applied. #43

Open
re5et opened this Issue · 1 comment

2 participants

@re5et

I have a custom sort in use in a table (pulling a value from a data attribute), and after a table has been sorted on one of these columns, if I attempt to filter, the value used for any column that has been sorted by this custom attribute is used instead of the text in the table cell. The filtering behavior only changes if the column has already been sorted. For example, with the following custom sort:

// accessor for extracting cell sort data from data attribute
jQuery.fn.dataTableExt.afnSortData['dom-data'] = function(oSettings, iColumn){
  var aData = [];
  $( 'td:eq('+iColumn+')', oSettings.oApi._fnGetTrNodes(oSettings) ).each( function () {
    aData.push( $(this).data('sort') );
  });
  return aData;
};

And the following table:

<table id="my-table">
  <thead>
    <tr>
      <th>Name</th>
      <th>Time</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Joe</td>
      <td data-sort="946713600">abc</td>
    </tr>
    <tr>
      <td>Bob</td>
      <td data-sort="1104566400">bcd</td>
    </tr>
  </tbody>
</table>

After the initial instantiation, done like so:

$('#my-table').dataTable({
  "aoColumns": [null, { "sSortDataType": "dom-data", "sType": 'numeric' }]
});

Filtering works as expected, typing "bc" shows both rows, "abc" shows only the first row, "bcd" shows only the second row.

After sorting by the "Time" column, the filtering examples shown above do not work. After this sort, the filtering seems to be using the sort values instead of the text values in the tds, because I can filter by "9476" and the first row will remain.

I assume that this is a bug because the behavior is inconsistent. If it is not, can you please explain how I can get the behavior I want, where the sorting goes by my custom sort, but the filter goes by the text in the td?

@DataTables
Owner

I'm sorry to say that you are hitting a limitation in DataTables at the moment. basically DataTables shares the data for sorting and filtering and thus when the data for the sort is gathered, it will also effect the filter :-(. This is very definitely a limitation in the core that I will be looking to address in future versions. Back at the start I assumed that both sorting and filtering would use the same data - which of course is not always the case.

So there are actually a couple options. What you could do is modify your markup slightly to use something like:

<td><span title="946713600">abc</span></td>

and then use the sorting plug-in to sort that type of data: http://datatables.net/plug-ins/sorting#hidden_title

That will do exactly what you want I believe, but it does require changing your markup (since the attribute from the TD cell is not available to the sorting function).

The alternative is to use mDataProp as a function. This does actually allow different data to be used for sorting, filtering and display since it will give you the original data for the table, which you can then modify as required for each of the types (the function will tell you what type it is looking for). The disadvantage is that it is slightly more complicated in the code, and I suspect a bit slower since it will be querying the DOM for the attribute during the sort - but it is a lot more comprehensive, and to a great extent overcomes the limitations I detailed at the start of this post :-).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.