How to Post Value/s from a table with CheckBox
How to Post Value/s from a table with CheckBox
Okay this is the last of my series of dicussions on checkboxes and expandable rows.
first one: ( https://datatables.net/forums/discussion/67087/detailed-expandable-rows#latest )
second one: ( https://datatables.net/forums/discussion/67099/how-to-add-checkbox-to-expandable-rows#latest )
For my Last request i would Like to do 3 things. The Test Case : ( http://live.datatables.net/weluyanu/2/edit ) Added listA
(1) Add a button to the top of the table to toggle selection of all values in the table.
(2) How to Post all the selected values from the table
(3) This one is gonna be quite abstract but here goes, Lets say I have a table with 1- 10 values being represented by the check boxes on the table, lets say I'm given 'listA' that contains certain values from that 1- 10 represented in the table, how do i only 'check' the boxed in the table whose values exist in 'listA'.
This is my current Javascript from my project:
var table = $("#datatables").DataTable({
"data": data,
"pagingType": "full_numbers",
"lengthMenu": [
[10, 25, 50, -1],
[10, 25, 50, "All"]
],
responsive: true,
language: {
search: "_INPUT_",
searchPlaceholder: "Search records",
},
"columns": [
{
orderable: false,
className: '',
defaultContent: '<div class="form-check">' +
'<label class="form-check-label">' +
'<input class="form-check-input" type="checkbox" value="">' +
'<span class="form-check-sign"><span class="check"></span>' +
'</span>' +
'</label>' +
'</div>',
data: null,
title: '',
width: 100
},
{
data: "accessRightName",
},
{
"className": 'details-control',
"orderable": false,
"data": null,
"defaultContent": '',
width: 10
},
],
select: {
style: 'os',
selector: 'td:first-child'
},
});
This is the HTML from my project::
<form>
<div class="card-body">
<div class="material-datatables">
<table id="datatables" class="table table-striped table-no-bordered table-hover" cellspacing="0" width="100%" style="width:100%">
<thead>
<tr>
<th></th>
<th>Access Right Name</th>
<th></th>
</tr>
</thead>
<tfoot>
<tr>
<th></th>
<th>Access Right Name</th>
<th></th>
</tr>
</tfoot>
</table>
</div>
</div>
<div class="card-footer justify-content-center">
<button type="submit" class="btn btn-fill btn-rose">Submit</button>
</div>
</form>
This question has accepted answers - jump to:
Answers
The easiest way is to use the Gyrocode checkboxes plugin. Or you can look on the forum as there are others who have done the same with just using Select. Like this example from this thread.
This example shows how to get the selected rows.
One option is to use
rows().every()
to loop all the rows and if it meets the criteria userow().select()
to select the row. Another is to userows().indexes()
to get the indexes of the rows that meet the criteria. Then use the indexes with therows().select()
API. Use therow-selector
as a function to choose the rows.Kevin
@kthorngren Im having a bit of an issue outputting the selected row, Here is the test case, http://live.datatables.net/weluyanu/11/edit
You are getting this error:
You capitalized the
C
in console. It should be lower case, like this:http://live.datatables.net/weluyanu/12/edit
Kevin
Hello @kthorngren , currently I have gotten the Indicies of the rows that need to be 'Checked', but im not sure how to go about it. Im trying to use the second method you mentioned, im just not sure how to use the selector function.
Here is the test case: http://live.datatables.net/weluyanu/24/edit
Take a look at the
row-selector
docs to see all the options you have. An integer is interpreted as the row index. You can pass an array of all the options. Since you have an array of indexes just use that as therow-selector
, for example:See the updated example:
http://live.datatables.net/weluyanu/25/edit
Kevin
ohh thats what i needed, okay @kthorngren i just noticed that im selecting rows by their index, im actually suppose to be selecting rows by the checkbox values i.e value 1 will be the first row, not the second row.
Im guessing i have to add a criteria on the rows().indexes(), not particularly sure how to do this.
Here the test case http://live.datatables.net/weluyanu/28/edit
As I mentioned you can use
rows().every()
to loop the rows to see if the row data matches the condition. In this case it sounds like you want to check you array position 0 against the array listA. See this example:http://live.datatables.net/weluyanu/30/edit
Kevin
Perfect, An with that i Thank you for all you have done to help, extremely help. Very much appreciated.
Thanks.