wrong "indexOf" value
wrong "indexOf" value
I'm apparently getting the wrong indexOf value when calling the following:
var index = table.column( 0 ).data().indexOf( 'key' );
The JSON results are returned in Descending order, so I added "order: [0, 'asc']" on the data table initiation.
When I go delete an object the index is reversed - in other words, if my data table has 10 items, I'm visually deleting the item at index 0 (first one i see) but the indexOf method returns index 9, so the bottom record is removed instead...
What can I do to properly use this IndexOf method to find/delete the right item? what if people sort/filter the table?
Answers
Maybe you would have better luck using
filter()
. I put this example together for something else but you may be able to modify for your purposesSounds like
indexOf
is looking at the array in the original order received not the sorted order.Kevin
Kevin, thank you for the reply I was just looking at the filter API but haven't figured out how to use it quite yet.. looking for examples and trying but any help would be great, I think it's a common scenario, how to get a row by ID when the order may no longer be the same as the internal index - may be worth publishing in the API examples. If I find something I'll post my solution.
Sorry I meant to post the example for you:
http://live.datatables.net/kibanoxu/1/edit
Kevin
Thanks Kevin - I'm still not getting the results I need. My use case is filter to ONE item, because it is the ID column, so in my case I tried this:
I modified your code from jsBin to the following:
For some reason this doesn't work on my dataset, since I'm looking through unique ID's there should only be one result … I'm getting multiple, I'll have to look into this further, will post any findings. If there's another approach or feedback welcome.
Is the value in
var itemId = 123;
in the original data set for column 1 (return dt.row(value).data()[1] == itemId;
)? Or are you doing some sort of render with the column to display123
?Kevin
Here's what I ended up with, please chime in if there's a more efficient/elegant way:
I would be curious to know if there's another way to do this or if this is the optimal approach to retrieve the "true" index of the item in the data table (as I now know the indexOf isn't reliable after a user sorts/re-sorts the table).
Do you need to do this?
dt.row(rowIdx).invalidate();
Wonder if doing this instead will work and be more efficient:
dt.draw(false);
If
api draw()
alone doesn't work then maybe you need to set the data then draw:dt.row(rowIdx).data(rowItem).draw(false);
According to the
row().invalidate()
using thedata
methods is preferred.Kevin
Kevin, without invalidate the row display stays stale, draw doesn't pick up the change made to the row fields for some reason.
I saw invalidate used in a knockoutJS example posted on the datatables site, which perhaps is why I need to use it in my case to work - link below:
https://datatables.net/dev/knockout/
I tried draw options (and ones you suggested) without success, the row remains stale.
As far as the filter implementation to get to the right index, would that be the best use of the API ? thanks for all the help with replying on this thread.