How do you locate a row position based upon call values?

How do you locate a row position based upon call values?

timhemmingtimhemming Posts: 2Questions: 1Answers: 0

After inserting a new row into a datatable I'd like to be able to locate the row afterwards. This should take into account sorting, filtering and pagination. I have access to the data for the inserted row so is there a way to find the row position based upon its cell data?

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 63,687Questions: 1Answers: 10,500 Site admin

    How are you adding the row? If you are using row.add() or rows.add() they return an API instance with the new rows in the result set. So you can simply do: rows.add( ... ).indexes() to get the indexes for the added row (for example).

    Allan

  • timhemmingtimhemming Posts: 2Questions: 1Answers: 0
    edited May 2014

    Thanks for the quick response. That jsbin isn't quite what I was looking for. When inserting the new row and redrawing the table I want to then find the inserted row's display location with sorting and filtering applied.

    I've built a proof of concept that allows searching for a row's display position based upon its contents: http://live.datatables.net/cacipox/1/edit?js,output

    Is there a more standard way to do this or do you think my approach is generally sound?

  • vogomatixvogomatix Posts: 38Questions: 3Answers: 7
    edited May 2014 Answer ✓

    You do not need to search the data, the solution in your code is the use of the rows() method...

    newRow = table.row.add(....);
    table.draw(); // do we need to draw or can we reorder without drawing here?
    newIdx = newRow.index();
    pos = table.rows()[0].indexOf(newIdx);
    
    // draw item on correct page, from page.jumpToData()
    if ( pos >= 0 ) {
        var page = Math.floor( pos / table.page.info().length );
        table.page( page ).draw( false );
    }
    
  • allanallan Posts: 63,687Questions: 1Answers: 10,500 Site admin
    Answer ✓

    That's probably the best way of doing it at the moment. I do want to add a selector option for the cells, rows and columns selectors that would allow selection by data. But that might not happen for a little while yet...

    Allan

  • vogomatixvogomatix Posts: 38Questions: 3Answers: 7
    edited July 2014

    @allan: Using the above code, it seems that occasionally the row added is not inserted in the correct order. I can replicate the problem by adding a row at the end of the current sort order, then adding another row which should be in the middle of the sort order, but which ends up being placed at the end.

  • allanallan Posts: 63,687Questions: 1Answers: 10,500 Site admin

    @vogomatix - can you link me to a page showing the problem so I can debug it?

    Allan

  • vogomatixvogomatix Posts: 38Questions: 3Answers: 7
    edited July 2014

    @allan: I've found it's not your fault (which is perhaps another way of saying it is mine) :-)

    adding rowdata containing strings to the table where columns are numeric is probably the reason sorting is having an issue... :-)

This discussion has been closed.