Datatables filter numbers greater than

2019-03-01 13:40发布

问题:

I am using datables, I need to filter particular column whose values are greater than 50 , I tried below code , but nothing happens

oTable.api().column( 10 )
  .data()
  .filter( function ( value, index ) {
     var htmlObject = $(value);
     var ball = htmlObject.text();
     return parseInt(ball) > 50 ? true : false;
   })
  .draw();

回答1:

"Nothing happens" because nothing is expected to happen. filter() should be used to create internal datasets or extract subsets of data - if you want to filter the rows in the table you should use a custom filter. It could look like this :

$.fn.dataTable.ext.search.push(function(settings, data, dataIndex) {
  return data[10] > 50 //type conversion is not necessary
})

This works as a "master" filter before .search() (and other filters). If you want to remove the filter you can use $.fn.dataTable.ext.search.pop(). So

  1. $.fn.dataTable.ext.search.push( filter function )
  2. table.draw()
  3. $.fn.dataTable.ext.search.pop()