Datatable with checkbox column and option to select or deselect all
Datatable with checkbox column and option to select or deselect all
Hi all,
Is there anyone who have implemented a DataTable with a checkbox column and then when you tick the checkbox on the header it select or deselect all rows in the table?
I've seen quite a few samples including the documentation, but, I was not able to make it working.
It'd be very nice if anyone can post a working sample or point me to the right direction on how to accomplish this.
Also, my last column is a operation column for CRUD operations.
This is what I have done.
$.ajax({
url: "http://localhost:52800/api/sample",
method: "GET",
contentType: 'application/json'
}).done(function (data) {
$('#tblTable').dataTable({
lengthChange: false,
pageLength: 10,
deferRender: true,
dom: 'Bfrtip',
order: [[1, 'asc']],
"aaData": data,
"columns": [
{
targets: 0,
data: null,
className: 'text-center',
searchable: false,
orderable: false,
render: function (data, type, full, meta) {
return '<input type="checkbox" id="check_' + data.id + '" class="check" name="check" value="' + data.id + '">';
},
width: "5%"
},
{
data: "email",
width: "50%"
},
{
data: "name",
width: "30%"
},
{
data: null,
className: "text-center",
defaultContent: "<a href='#' class='_edit'><span class='glyphicon glyphicon-edit'></a> " +
"<a href='#' class= '_details'><span class='glyphicon glyphicon-eye-open'></a> " +
"<a href='#' class= '_delete'><span class='glyphicon glyphicon-trash'></a> " +
"<a href='#' class= '_invite'><span class='glyphicon glyphicon-envelope'></a>",
orderable: false
}
],
buttons: [
{
text: 'Create New',
action: function () {
let html;
$.get('form.html', function (data) {
html = data.replace("StateControlsPlaceHolder", "value='new'");
}).done(function () {
$('#ActionContainer').html(html);
});
}
},
{
text: 'Invite',
action: function () {
alert("Invite multiple users at the same time");
}
},
{
text: 'Delete',
action: function () {
alert("Delete multiple users at the same time");
}
}
]
});
});
Thank you very much!
Replies
Hi @AlexanderLamas ,
You can do this easily with the Select extension - see the example here.
Cheers,
Colin
Hi Colin,
Thank you very much. Much appreciated your help.
Based on your answer I have question.
It looks like when you use var table ... is more flexible and you can manipulate the dataTable easier, is that correct?
If I user the var table ... how can I populate this table from an ajax call?
Is it better to use table the way you did in your sample
or this way, getting the data from the server as json format
Thank you very much in advance.
Regards,
Alex
Hi Alex,
Colin asked me to take a look at your follow up questions:
Yes - because you can manipulate the DataTable through its API. You don't have to do it that way, you can access the API other ways, but that is the most common.
Make your Ajax call (using
fetch
or$.ajax
or whatever) and userows.add()
to add the rows to the table based on the return from the server.It depends . You might already have an Ajax library for handling security etc, that you want to use rather than using
$.ajax
. Or perhaps you just want the complete control that using$.ajax
directly gives you. If so, then doing it that way is absolutely fine.For me, I tend to use the
ajax
option because then you can use the DataTables Ajax API methods (e.g.ajax.reload()
) which you couldn't do if you were making the Ajax calls yourself.Allan
Hi Allan,
Thank you very much your reply and explanation.
Can I ask you please to post a sample of using datatable API with ajax Option and ajax reload() as you mentioned?
Also, would you have a sample when using var table = ...dataTable being populated via a ajax call?
Thank you so much for your time!
Regards,
Alex
Hi @AlexanderLamas ,
This example here is a good one. It shows
serverSide
processing,ajax
, and with two types ofajax.reload()
,Cheers,
Colin
Thank you very much Colin!
Hi Colin,
I've used the var table = ... It worked really well. So, thank you very much for that.
Sorry, I've got one more question for you.
I've implemented the multiple selection in the dataTable.
The header checkbox is selecting and deselecting all items, but, it does not really select the rows.
To select the rows I have to click on anywhere in the row but not in the checkbox.
Do you have any idea why that is happening? Do I have to implement anything else to help the dataTable API to be able to make that work?
Please post some sample if you have or send me a link with a multiple selection option.
Thank you so much again.
Regards,
Alex
How are you selecting the rows when using the header checkbox?
Are you using the
rows().select()
androws().deselect()
API's?Kevin
I didn't use anything, I mean, I didn't implement anything.
I'm just using the dataTable API.
Where and when should I implement the selecting and deselecting for one item and all items?
Thanks a lot Kevin!
Regards,
Alex
Hi @AlexanderLamas ,
If you didn't implement anything, that's probably the issue If you do what Kevin said in a
click
event that should do the trick, something like this.Cheers,
Colin
Hi Colin,
Thank you very much for your help.
It works as a charm now. Thanks to you guys!
Can I ask you if this code is optimal, by that I mean, if is running the fastest possible way and retrieving the data from the ajax call in the best way? Feel free to suggest improvements.
Thank you so much!
Regards,
Alex
Looks good to me, happy to have helped!
C
Hi Colin,
I don't want to abuse, but, I've got another question.
How can I make the information example: "Showing 1 to 10 of 47 entries" and pagination in the same line and keep the information aligned to the left?
I've got the below sample,
dom: "Bfrtip <'row'<'col-sm-12'tr>> <'row'<'col-sm-4'l><'col-sm-4'i><'col-sm-4'p>>",
but it's not really working.
is there a place to find all the possible settings for the 'dom' to help on this matter?
Thank you again!
Hi @AlexanderLamas ,
See
dom
for the settings, and this thread here will help too, since it's showing how to move elements with CSS,Cheers,
C0lin
Hi Colin,
Thank you very much for replying my last question, I've not defined the final result yet, but, I've been playing with the settings and I think I should be fine. It's quite powerful. So, well done job to you guys.
I've another question for you.
I have add a rendered column in the table, and I wonder if it's possible to change the value of the column, like change the value='1' if I click on the column and if I click again it changes to value='0'. Like change to true to false and vice-versa.
I've added a CSS class in the column with an image to change to ON/OFF if I click on, the image is changing with no problem, but I could not get the click to change the column value.
Is there any way I can accomplish that?
Again, thank you very much!
Cheers,
Alex
Hi @AlexanderLamas ,
Yep, if you click on the age column in this example here, you'll see it incrementing on each click, it'll be trivial to change to a boolean.
Cheers,
Colin
Hi Colin,
Thanks for your help and sending the link.
It helped me a lot.
I just had to do a little tweak in my case to be able to trigger the event.
The only thing now is how I can't get the row ID in that event?
This is my event.
I actually created a function to get the row ID, but I think it just work in the row level.
but the function doesn't work inside of the event.
Can you please help me on how to retrieve the row ID in the column event?
Thank you very much!
Regards,
Alex
In looking at your above code it doesn't look like you have a column defined for
id
usingcolumns.data
. Do you have one?After this line
let data = table.DataTable().row(tr).data();
I would useconsole.log(data);
to see if you are getting the row data. Looks like you should have the row data.return data.id;
should return the value of theid
column assuming you have one defined. Otherwise please describe what you are expecting as the row 'ID'.Kevin
Hi Kevin,
Thanks for your reply.
Actually, the column is a radio input, so, I had to inject the ID in the value of the radio to be able to get the original ID of the record.
Before I tried to get the ID by looking at the table API, but, as its a radio it was changing the row order after clicking on the radio, then I was losing the row ID.
That's why the injection worked better as I can keep the ID no matter the radio input's behavior.
The sample you sent me, didn't really work, and the ID was nested very deep inside of the table row and I could not make it clear to retrieve.
Is there any other way to do that without injecting the ID in the input's value?
Thank you!
Regards,
Alex
Does your data returned from the AJAX request contain the
ID
for each row? It doesn't have to be a column but you should be able to access it.Assuming this line is working:
let data = table.DataTable().row(tr).data();
You should be able to access the
ID
data object in thereturn data.id;
line. Just changeid
to the object key name you want to retrieve.Maybe you can put together a simple example using an example of some data retrieved in the AJAX response and your table. For me it would be easier to help if I could work with your specific data and code.
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin
Hi Colin and Kevin,
Thank you very much for your help so far, I really appreciate the time and effort.
Regards,
Alex
Hi Colin and Kevin,
Do you guys know how to style the checkbox in the datatable?
I don't know if it's possible to change the style of a checkbox, but if it is not, is it possible to wrap the checkbox in a DIV tag for example?
Thank you!
Regards,
Alex
Hi @AlexanderLamas ,
Yep, possible to do both. If you can post a test case, we can make suggestions on your setup.
Cheers,
Colin
Boa tarde. Estou usando o datatable-checkbox e uma das colunas da tabela é uma DATA (Y-m-d), porém ela sempre inicia a listagem ordenando da DATA mais antiga para a DATA mais atual, e eu gostaria de colocar como padrão que ela iniciasse sempre da DATA mais atual para a DATA mais antiga, como faço?
Hi @alexjorgemota ,
That is a different issue, so please post in a new thread with a test case. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.
Cheers,
Colin