How do you locate a row position based upon call values?
How do you locate a row position based upon call values?
timhemming
Posts: 2Questions: 1Answers: 0
in General
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:
This discussion has been closed.
Answers
How are you adding the row? If you are using
row.add()
orrows.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
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?
You do not need to search the data, the solution in your code is the use of the rows() method...
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
@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.
@vogomatix - can you link me to a page showing the problem so I can debug it?
Allan
@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... :-)