Change to index processing, for example when using rows().remove() followed by rows().add()
Change to index processing, for example when using rows().remove() followed by rows().add()

I am upgrading from 1.13.6 to 2.2.2.
First of all, let me say I have circumvented the problem I am about to describe, and resolving it is not urgent.
I have some code that loops through rows and updates cells. I wrote it many years ago before understanding the difference between the DataTables internal row index and the position of the row in the displayed table. The loop I was using was in this block of code . . .
let dtColumns: IDateTimeColumnsObject[] = c9.appVar.document.chronology.dateTimeColumns();
let column, rowCount, cellData, question, zString;
for (let i = 0, j = dtColumns.length; i < j; i++) {
column = dtColumns[i];
rowCount = c9.appVar.dtAPI.rows().count();
for (let m = 0, n = rowCount; m < n; m++) {
cellData = c9.appVar.dtAPI.cell(m, column.index).data();
question = cellData.substr(0, 1) == '?' ? '?' : '';
... specifically for (let m = 0, n = rowCount; m < n; m++) {
After upgrading, and exercising this bit of code after deleting the first row of a table, I ran into a problem on the last line quoted:
TypeError: Cannot read properties of undefined (reading 'substr')
at c9dataTable_formatDates (https://localhost/c9/c9enter/compiled/c9enter.js:7872:33)
Running a code snippet to display the internal row indices revealed that index 0 was no longer there.
Investigation
I looked into row index processing with the following snippet. Apologies if it does not seem immediately related, but I came to it further through my investigations.
logRows('before');
c9.appVar.dtAPI.rows().remove();
c9.appVar.dtAPI.rows.add(c9.appVar.document.chronology.events).draw();
logRows('after');
function logRows(msg) {
c9.appVar.dtAPI.rows().every(function(a,b,c){
console.log(msg + ":" + a);
})
}
(I keep a reference to the API in variable c9.appVar.dtAPI
, and the table is populated from an array in c9.appVar.document.chronology.events
).
Running this in 1.13.6 output this:
but in 2.2 it gave this:
As you can see, the row indexes no longer start at zero . . . I think this might be displaying another facet of the root cause of my original problem.
Concluding remarks
This no longer presents me with the original problem, as I have updated my loop by using the every() technique and thus not assuming anything about internal indices:
for (let i = 0, j = dtColumns.length; i < j; i++) {
column = dtColumns[i];
columnIndex = parseInt(column.index);
c9.appVar.dtAPI.columns(columnIndex).cells().every(function() {
cellData = this.data();
question = cellData.substr(0, 1) == '?' ? '?' : '';
However, I would apppreciate your comments on this in pursuit of a full understanding.
I have put up a fiddle at https://live.datatables.net/tegucoyo/1/edit which uses the nightly build and illustrates the non-zero indices. I tried to update it to use DataTables 1.13.6, but without success, getting an error message
"Script error. (line 0)"
This question has accepted answers - jump to:
Answers
The test case doesn't have any of your JS code so not sure what you intended but I added your "investigation" code here:
https://live.datatables.net/tegucoyo/2/edit
Here is an 1.13.6 version where I added the
logRows()
output afterrow().remove()
, yes removing only one row:https://live.datatables.net/kukudeli/1/edit
The 2.0.0 release notes state this:
IIRC there have been threads in the past that indexes change after removing rows. I believe this is to fix the issues others have reported in the past.
Kevin
Kevin, thanks for the reply.
I am sorry my code was missing from the bin I posted - I am not 100% familiar with the playground.
For completeness, I have now produced updated versions of it and of your 1.13.6 version so that my original report is properly documented.
https://live.datatables.net/tegucoyo/3/ runs the nightly DataTables and shows the indices NOT being reset to zero.
https://live.datatables.net/kukudeli/4/ runs in 1.13.6 and shows the indices being reset to zero.
Thanks. Do you still have questions does my explanation answer them?
Kevin
No more questions thanks Kevin, I only added the two bins for completeness. I am happy to accept that DataTables changed processing between releases without me noticing, as I was using the API incorrectly in the first place!
Just to stick my oar in - it's exactly as Kevin says! Rows in DataTables 2 will get a unique position in the data array (i.e. a unique data index) and that cannot be reused (when client-side processing, server-side processing however does, but indexes are a lot less important for that).
Previously indexes could be reused which let to some interesting and difficult to trace bugs. For example:
has different behaviour between v1 and v2. With v1 it would delete the first row, and then whatever took its place with the second call. That is no longer the case in v2.
No extra information here really, but just wanted to illustrate why it was needed.
Allan