How to use filter() in datatables and get the whole row for it

How to use filter() in datatables and get the whole row for it

jssalvadorjssalvador Posts: 3Questions: 1Answers: 0

How can I get the whole row for the column that I filter that I put in datatables? The user will input the filter and I want the system to search for it without changing the row data of table.

I tried this one

var rowData = $('#table').DataTable().column(2).data().filter(function(value,index){
        return value == $('input[name=filter]').val() ? true:false;
      }).toArray();

but this will just get the column(2) value if its exist. I want to get all the data in that row. I'm searching only the column(2) but I want to get all the column data or that row where the filter result.

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,343Questions: 26Answers: 4,954
    Answer ✓

    I found this example to work:
    http://live.datatables.net/lejahoko/1/edit

    If utilizes rows().indexes() instead of column().data() and builds an array of row indexes that match the criteria. Then uses those indexes as the row selector to get the data.

    Kevin

  • jssalvadorjssalvador Posts: 3Questions: 1Answers: 0
    edited July 2019

    It's not working I tried it and I'm getting a empty result.

    This is the code that I tried

    var indexes = $('#table').DataTable().rows().indexes().filter(function(value,index){
                return $('input[name=filter]').val() === $('#table').DataTable().row(value).data()[2];
              });
              var rowData = $('#table').DataTable().rows(indexes).data().toArray();
    

    Edited by Kevin:  Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

  • jssalvadorjssalvador Posts: 3Questions: 1Answers: 0

    @kthorngren answered is acceptable I just need to remove the other = in the code. Just like this

    var indexes = $('#table').DataTable().rows().indexes().filter(function(value,index){
                return $('input[name=filter]').val() == $('#table').DataTable().row(value).data()[2];
              });
              var rowData = $('#table').DataTable().rows(indexes).data().toArray();
    

    Thanks Alot!

This discussion has been closed.