Michal
Michal

Reputation: 2039

jQuery dataTables: sorting a table with custom filter applied causes rows disappear

I use custom filtering for the following datatable...

var table = $('#placeholder').DataTable({
  "bJQueryUI":true,
  "bFilter": true,
  "bPaginate": true,
  "bLengthChange": false,
  "pageLength": 25,
  "bDeferRender": true,
  "bSortClasses": false,
  "data": {$data} // nette injected
});

in order to display only unique rows with respect to column 2 value...

var rows = [];

$.fn.dataTable.ext.search.push(function (settings, data, dataIndex) {

  // Retrieve info for unique row filtering
  // If value is set, then unique filtering is enabled
  var filterUVal = parseInt($('#frm-filterForm-unique').val());
  var columnDVal = data[2];

  // Filter unique row
  if (!isNaN(filterUVal)) {
    if (~rows.indexOf(columnDVal)) {
      return false;
    } else {
      rows.push(columnDVal);
    }
  }
  return true;
});

If I sort according to any column and consequently apply filter, then it works. However, if I apply custom filter first and sort according to any column, then all rows disappear and I see only empty datatable.

Upvotes: 2

Views: 645

Answers (1)

davidkonrad
davidkonrad

Reputation: 85538

This is because rows never is resetted. Think about it, eventually rows will contain all unique data[2] values, thus each and every row is returned with a false regardless of the search term.

I believe you could overcome this by resetting rows in a draw handler :

$('#placeholder').on('draw.dt', function() {
  rows = []
})

Upvotes: 1

Related Questions