How to select and check a row in createdRow event?
How to select and check a row in createdRow event?
jstuardo
Posts: 104Questions: 41Answers: 0
Hello...
I have a DataTable with Select extesion and this code in createdRow event:
$(row).addClass('selected');
$(row).select();
with that, only the row is hightlighted but the checkbox is not checked. How can I do it?
This question has an accepted answers - jump to answer
Answers
This won't select the row. It just highlights the row making it look selected. Look at the info element under the table and it won't show any rows as selected.
This is executing the jQuery select() method not the Datatables
row().select()
API.Try this instead:
Kevin
How can I access the API globally?
In createdRow I am calling other function with only 2 parameters,
row
anddata
, and I cannot add a third one.So by callng
this.api().row(row).select();
throws an error.Jaime
then
table
is the API instance. You'd use that. Or you can do:if you need an API instance to an existing DataTable and you can't access the variable you previously created.
Beyond that, we'd need to see your code.
Allan
But isn't there a problem to call
new
for every created row?This is my
createdRow
code:createdRow: function (row, data, dataIndex) {
if (typeof rowCallback === 'function')
rowCallback(row, data);
else if (!data.habilitado) {
$(row).children('td').addClass('text-danger');
}
}
where
rowCallback
is an external function that is passed to a common function to initialize the grid.For this case, that
rowCallback
is this (actually this is surrounded by anif
condition but it is not meaningful for this problem):function (row, data) {
this.api().row(row).select();
}
Jaime
Allan
I see two options:
rowCallback
function that is the Datatable API. If something calls it and doesn't pass the third parameter then it will beundefined
in the function or you can set a default value.Kevin
Thanks Kevin, I chose to add a third parameter. If that is not used by other callers, it does not matter.
That way it works, but with a side effect. It works only for the first time.
Let me tell you now that I have the DataTable inside a bootstrap modal. When the modal is closed, I am calling
where
grid
is the DataTable. After doing that, I destroy the modal.When I open the modal for the second time, I get this error:
Is this the proper way to completely free DataTable resources?
The line #171 of grid.js is line 4 of this one:
where grid is the DataTable.
When calling for the second time, the call to
simpleDataGrid.rows(indexes).data()
returns an empty array. That is why the error.That does not happen if I don't call
this.api().row(tr).select();
. It seems, I need to freeSelect
extension resources.Any more advice, please?
Thanks
Jaime
Are
grid
andsimpleDataGrid
referencing the same Datatable? If so its possiblesimpleDataGrid
is pointing to the old reference. If not then its confusing to my why you have aselect
event for one Datatable but reference another with the row indexes.You may need to dig a bit deeper in the
select
event to understand whysimpleDataGrid.rows(indexes).data()
returns an empty array. Like what is the value of indexes.Its hard to say what the problem might be with just the code snippets. We will need to understand your code flow to help debug. Please provide a link to a test case replicating the issue.
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin
Hi Kevin... yes... sorry for my mistake.
simpleDataGrid
andgrid
is the same DataTable.However, the problem was my fault. I had this in DataTable:
I am not sure why I did that, but I inherited that code when I used other checkbox in an older version of DataGrid.
I have removed those "on" and it is working properly now. Thanks.