Using row(0).remove() only works once?
Using row(0).remove() only works once?

I want to remove a row I've added. I can determine the index, let's say it's row 0. I remove it. Shouldn't old row #1 now be row #0 ?
I did a fiddle here, best I could. (Not sure why alert() throws an error... this is a weird editor. Would've done jsfiddle, but couldn't figure out to attach external scripts.)
https://live.datatables.net/vimezoko/7/edit
Is there a way to reindex the rows?
Answers
This is the error:
It's due to the row you are trying to add not the JS Bin editor. First you don't have enough columns in
row().add()
and second is you haven't configuredcolumns.data
so Datatables expects an array not an object for the row data. Updated test case:https://live.datatables.net/vimezoko/7/edit
No, the row indexes are established during initialization and don't change when a row is deleted. New rows will have a new index following the highest index previously created. The row added in the test case will have index 2 and the remaining row stays at index 1.
No. Please provide details of exactly what you are trying to do so we can offer suggestions. You can use
row().index()
with a variety ofrow-selector
options to get a particular row index.Kevin
I simply want to remove the row positionally. The FIRST row in this case. The truth is, how I found the issue was in trying to test some other functionality we have. In testing, I added several rows to a DataTable, then tried to remove them 0 after 0 after 0 to no avail. Found that a co-worker has a wrapper function for deleting rows where he loops over dt.data(), pushes all rows to a new array, then does dt.clear().draw() and then loops over the array and then does dt.row.add(data).draw() repeatedly.
I had some special <input type="text"> values in the table, and when I went to remove a row, an entire redraw happened (including my custom render function), which resulted in emptying it out.
It's not that important, now that I understand that basically the "index" is really DataTable'sOwnRowID as opposed to a "data array index"... (and certainly not the actual <tr> positional element!) it makes more sense to me. And it's clear, too, that the index points to an unsorted value (at least pre-front-end-sort). I just created /8/edit in that fiddler gizmo. Issuing row(0).remove() deletes the visibly second row for a Name sort has true row 0 in the second <tr>. So... just a matter of understanding how it works internally for this scenario.
To re-summarize, it's kind of a non-issue. I don't think we'd ever try to delete "the first visual row". We usually have actionable icon buttons on the row, so we're able to get the correct index as needed based on what was clicked. I just had to poke at it with 100 test cases to get a grasp of my issue.
Thanks for your super speedy reply!
I noticed in your test case it looked like you were trying to update the HTML directly. Datatables doesn't know about these updates. If you are doing something similar here then likely that is why things disappear. See this FAQ for more details.
Row index might not be the best option for this. first row can mean many things but if you want to delete the first row displayed on the page you can use the
row-selector
oftr:eq(0)
. For example:https://live.datatables.net/vimezoko/10/edit
I noticed that my test case link didn't update so you didn't see my change to
row.add()
. I fixed it in the updated test caseThere is a lot of flexibility with the
row-selector
. Using it as a function is very useful.Kevin