how to correctly determine the row index when searching

how to correctly determine the row index when searching

izumovizumov Posts: 178Questions: 14Answers: 0

I need to find the index of the row that contains certain information in the 1st column.I tried to solve this problem with such code.

var table=$('#goods').DataTable();
        var row=table.search(id_good);
        var i=table.row(row).index;

But I didn’t succeed. Where did I make a mistake?

Replies

  • kthorngrenkthorngren Posts: 21,167Questions: 26Answers: 4,921

    Using var row=table.search(id_good); isn't going to provide the row results in the variable row. The search() API only affects the rows displayed in the table. The filter() API is used for finding rows that match a particular value and plaving the results in a variable.

    Kevin

  • izumovizumov Posts: 178Questions: 14Answers: 0

    I tried to determine the row index using an expression

    var i=table.columns(0).search(id_good).row().index();
    

    but regardless of the value id_good. I get the result 0. What is the code error? How to achieve the desired?

  • allanallan Posts: 63,205Questions: 1Answers: 10,415 Site admin

    Use the row-selector as a function if you want to programmatically find certain rows:

    let indexes = table
      .rows( (idx, data, node) => {
        if (data[0] === 'whateverSearchTerm') {
          return true;
        }
        return false;
      })
      .indexes();
    

    Note that I've assumed you are using arrays of data, but if you are using objects your use data.myProperty === ....

    Allan

  • izumovizumov Posts: 178Questions: 14Answers: 0

    thank you very much I was able, thanks to your prompts, to determine the index of the row with the required information, now the code works as it should

This discussion has been closed.