Callback createdRow dataIndex
Callback createdRow dataIndex
I had a question about the createdRow callbacks dataIndex parameter.
Heres the DT code thus far first:
var $fields_dt = $fields_table.DataTable({
rowReorder: {
snapX: 10,
selector: '.row-grip'
},
bPaginate: false,
bFilter: false,
bSort: true,
columnDefs: [
{ targets: 0, visible: false },
{ targets: '_all', orderable: false }
],
fnCreatedRow: function( nRow, aData, iDisplayIndex ) {
// Row id attr
$( nRow ).attr( 'data-row-id', iDisplayIndex);
}
});
So as you can see, when the row is created, I add a data-row-id
attribute with the iDisplayIndex value, which is the row index.
Plenty of items in the row will use that as a reference point, for example, to add a select value in the row, it will grab the value of $(this).closest( 'tr' ).data('row-id')
and pass it to the modal, then the modal will grab that value, and add it to the select thats found in the $('#data-table').find('tbody').find("tr[data-row-id='" + row_id + "']")
row.
This has worked fine, until I realized that the iDisplayIndex value is actually the exact row number that its in, meaning if I add 3 rows, the ID's will be:
- 1
- 2
- 3
Then if I delete the 2nd row, and click add again, the ID's will be:
- 1
- 3
- 3
This is, I'm assuming, because the CreatedRow callback will see its going to be added as the third row to the table, as opposed to the 4th.
I tried to just use $('#data-table').find('tbody').find('tr:last').data('row-id')++
for the data-row-id
value, but the problem with that, is when you first get to the page, the initial first row is hardcoded into the HTML, with data-row-id="0"
, and for some reason, that really throws DataTables off, it seems to try to re-initialize the first row and reset the data-row-id="1"
, but then for some reason it starts the count over again at 1, so the second row is data-row-id="1"
, the third row is data-row-id="1"
... and its just driving me crazy.
So basically, when the age is loaded, the table is loaded with one row, with a hardcoded data-row-id="0"
, what I need, is for when DataTables is initialized, for that to stay as data-row-id="0"
, then every row after that to increment the row id +1, and when a row is deleted, to keep that number going, instead of going back to what the row number is.
Thanks!
P.S. Think this is the first time ive used the markdown, definitely useful! lol
This question has an accepted answers - jump to answer
Answers
I would suggest passing the row ID by the actual DOM or JSON call instead by the createdRow method.
or if in Json
Oh, I probably should have posted an update. I kinda stumbled onto the solution, which at first didnt seem like it should work, but it seems to be working fine thus far.. Heres the code
So, like I said, that seems to work, but if you see any holes in it, let me know.
And FYI, I use Mustache because some it works in JS and PHP, and if you are editing a "thing", then the rows will already exist, if its new, then you will add rows via JS, and this way, I can edit the row in one source, and it will reflect in both. If anyone knows of a better way, or a better template engine that works in JS or PHP (preferably with at least some basic logic), let me know.