HTML Elements in Column().Data().Filter()

HTML Elements in Column().Data().Filter()

highflyinghighflying Posts: 9Questions: 5Answers: 0

Hi all,

My question is how are html elements and number symbols (ex: -, %) in td's processed when you run column().data().filter(). I keep getting a type error when I use the following example. I think it's the html elements or the % signs next to my numbers. Not sure.

Here's my code:

var table = $("#example").DataTable();
table.column(2).data().eq(0).filter( function (value, index) { return value > 2 ? true : false } ).draw()

Here's the error I'm getting:

Uncaught TypeError: Function.prototype.apply: Arguments list has wrong type jquery.dataTables.js:6659
_Api jquery.dataTables.js:6708
_Api.eq jquery.DropDownMenuSort.js:239
(anonymous function) jquery-1.11.1.min.js:3
m.event.dispatch jquery-1.11.1.min.js:3
m.event.add.r.handle

Thanks in advance.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,795Questions: 1Answers: 10,115 Site admin
    Answer ✓

    First of all, I doubt that filter() is going to do what you want here. It filters the result set - not the table!

    As the documentation says:

    This method should not be confused with search() which is used to search for records in the DataTable.

    From your code above it looks like you expect to be able to search the table and draw with only value > 2 results. Currently there is no way to pass a function into column().search() - that is something that I will be adding for v1.11.

    At the moment you would need to use a search plug-in.

    Allan

  • highflyinghighflying Posts: 9Questions: 5Answers: 0
    edited March 2015

    Thanks Allan. That's helpful, I'll give it a shot.

    I'm a bit confused, however; don't both methods return a new instance of the Datatables Api? Would you provide an example of when one would use the filter method?

    Thanks.
    Matt

  • allanallan Posts: 61,795Questions: 1Answers: 10,115 Site admin
    edited March 2015

    don't both methods return a new instance of the Datatables Api?

    Yes, but they do different things!

    table.search("Allan").draw();
    

    will search the table for "Allan" and display only rows which have "Allan" in them.

    However:

    var result = table.column(0).data().filter( function ( v ) {
      return v.indexOf( 'Allan' ) !== -1;
    } );
    

    will filter the array of data (in this case from column 0) and result any result that contains "Allan" into the result Api instance (use toArray() to make it a real array). This does not effect the table display in any way.

    Allan

  • highflyinghighflying Posts: 9Questions: 5Answers: 0

    Ok, thanks Allan. That's very useful.

This discussion has been closed.