简体   繁体   中英

Checkbox click event not working in datatable row when buttons: [ 'colvis' ] activeted

I have 1 question here: How to get a Checkbox Value in JavaScript .

  • With the help it worked very well. However, when I added buttons: ['colvis'] to DataTable, the click checkbox events did not seem to work correctly. I have looked for help on the DataTable forum, but to no avail.

Code:

<input id="listvalue" name="selectedCB">
<table id="datatest" class="table table-striped table-bordered nowrap" style="width:100%">
<thead>
    <tr>
        <th class="no-sort checkboxor">
            <input type="checkbox" onclick="toggle(this)" name="checkedAll" id="checkedAll" /> #
        </th>
        <th class="no-sort">IDtest</th>
    </tr>
</thead>
<tbody>
    <tr>
        <td class="tycheck">
            <input type="checkbox" name="checkAll" value="2" class="checkSingle" />
        </td>
        <td>1</td>
    </tr>
    <tr>
        <td class="tycheck">
            <input type="checkbox" name="checkAll" value="1" class="checkSingle" />
        </td>
        <td>2</td>
    </tr>
</tbody>
</table>

$(document).ready(function () {
        $('#datatest').DataTable({
            buttons: [
               'colvis'
            ]

        });
});

var idsArr = [];
var displayField = $('input[name=selectedCB]');
var checkboxes = Array.from($(".tycheck input[type=checkbox]"));
function toggle(source) {
var values = checkboxes.map(x => {
  x.checked = source.checked;

  return source.checked ? x.value : '';
}).join(source.checked ? ',' : '');

displayField.val(values);
}
function printChecked() {
var values = checkboxes.filter(x => x.checked).map(x => x.value);

displayField.val(values);
}
$.each(checkboxes, function () {
$(this).change(printChecked);
})

In this case, you may need to update the event onchange for the checkbox #checkedAll .

Since you want to use it with jquery, you can remove the attribute onclick="toggle(this)" from

<input type="checkbox" onclick="toggle(this)" name="checkedAll" id="checkedAll" />

Then, in your js file, you can write the event like this

$('#checkedAll').on('change', toggle);

Then, because we use toggle function as the event for this checkbox, we don't need source parameter anymore, just replace it with this :

function toggle() {
  var values = checkboxes.map(x => {
    x.checked = this.checked;

    return this.checked ? x.value : '';
  }).join(this.checked ? ',' : '');

  displayField.val(values);
}

 $(document).ready(function () { $('#datatest').DataTable({ buttons: [ 'colvis' ] }); }); var idsArr = []; var displayField = $('input[name=selectedCB]'); var checkboxes = Array.from($(".tycheck input[type=checkbox]")); function toggle() { var values = checkboxes.map(x => { x.checked = this.checked; return this.checked ? x.value : ''; }).join(this.checked ? ',' : ''); displayField.val(values); } function printChecked() { var values = checkboxes.filter(x => x.checked).map(x => x.value); displayField.val(values); } $.each(checkboxes, function () { $(this).on('change', printChecked); }); $('#checkedAll').on('change', toggle);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script> <script src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script> <input id="listvalue" name="selectedCB"> <table id="datatest" class="table table-striped table-bordered nowrap" style="width:100%"> <thead> <tr> <th class="no-sort checkboxor"> <input type="checkbox" name="checkedAll" id="checkedAll" /> # </th> <th class="no-sort">IDtest</th> </tr> </thead> <tbody> <tr> <td class="tycheck"> <input type="checkbox" name="checkAll" value="2" class="checkSingle" /> </td> <td>1</td> </tr> <tr> <td class="tycheck"> <input type="checkbox" name="checkAll" value="1" class="checkSingle" /> </td> <td>2</td> </tr> </tbody> </table>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM