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

本小妞迷上赌 提交于 2020-02-05 04:11:05

问题


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);
})

回答1:


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>


来源:https://stackoverflow.com/questions/59657456/checkbox-click-event-not-working-in-datatable-row-when-buttons-colvis-act

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!