How can i find row and column indexes of certain value With datatable?

How can i find row and column indexes of certain value With datatable?

toroNGTtoroNGT Posts: 7Questions: 2Answers: 0

I have certain string value. I want to search that value in DataTable and get the row and column indexes of that value. How Can i accomplish this with datatable?

This question has accepted answers - jump to:

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599
    Answer ✓

    Hi @toroNGT ,

    You can use search() to search the table which would then put those matching rows into the table. table.rows( {search:'applied'} ).indexes() would return the row index of those matching records.

    Hope that helps,

    Colin

  • toroNGTtoroNGT Posts: 7Questions: 2Answers: 0
    edited May 2018

    Dear @colin

    The problem is that i am using table.columns(index).search(val).draw(); code to filter DataTable. I do not want the previous search results, that are displayed to the user to be changed and i do not want to change search val value. I just want to search whole datatable (under whole i mean whole table data, ignoring currently applied by search val value) and get row and column indexes.

    How can i accomplish that?

  • colincolin Posts: 15,240Questions: 1Answers: 2,599
    Answer ✓

    Hi @toroNGT ,

    You can just get the data in an array with something like, table.rows().data(), and iterate through the array to see if it matches. Also, filter() may work for you too.

    Cheers,

    Colin

  • toroNGTtoroNGT Posts: 7Questions: 2Answers: 0

    Dear @colin

    Thank you for your response. I have not tried filter() method. Because i was able to solve my problem using row-selector function with following code:

       var rowIndexes = [];
       table.rows( function ( idx, data, node ) {              
                if(data.somefield === somevalue){
                   rowIndexes.push(idx);                   
                }
    
                return false;
            }); 
    

    Are there any downsides of this approach?

  • colincolin Posts: 15,240Questions: 1Answers: 2,599
    Answer ✓

    Hi @toroNGT ,

    That's as good a way to go as any other :)

    Cheers,

    Colin

This discussion has been closed.