具有 jQuery-切换的数据表在第 1 页以外的页面上不起作用

Datatables with jQuery-toggles not working on pages other than page 1

我有一个 Data-table 正在填充服务器端代码。

<table id="email_alerts" class="table">
    <thead>
        <tr>
            <th>Name</th>
            <th>Position</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model.crmContactsList)
        {
            <tr>
                <td>@item.fname</td>
                <td>
                    @if (item.claimsOpenedAlert)
                    {
                        <div class="control-label">
                            <div class="toggle toggle-primary" data-toggle-on="true" data-contact-id="@item.crmContactID"></div>
                        </div>
                    }
                    else
                    {
                        <div class="control-label">
                            <div class="toggle toggle-primary" data-toggle-on="false" data-contact-id="@item.crmContactID"></div>
                        </div>
                    }
                </td>
            </tr>
        }
    </tbody>
</table>

我使用这个 jQuery 来初始化所有 jQuery 切换到数据库返回的内容。

jQuery('.toggle').each(function () {
            $(this).toggles({
                on: $(this).data('toggle-on')
            });
        });

我使用这段代码来改变它们被点击时的状态。关闭变为开启,反之亦然。

// Toggle Switches
        $('.toggle').on('click', function () {
            console.log($(this).data('toggle-on'));
            if ($(this).data('toggle-on') == true) {
                $(this).data('toggle-on', false)
                console.log('Turning off ' + $(this).data('contact-id'));
            }
            else {
                $(this).data('toggle-on', true)
                console.log('Turning on ' + $(this).data('contact-id'));
            }
        });

现在所有这一切都完美无缺,除了当我点击 Data-tables 的第一页以外的任何页面时。第2+页 它们都默认开启。

知道为什么这适用于第一页但不适用于 data-tables 中的其他页面吗?

动态初始化切换插件,因此您还可以 "toggles" 在更改页面、排序等时注入元素:

jQuery('#example').on('draw.dt', function() {
    jQuery('.toggle').each(function() {
        $(this).toggles({
           on: $(this).data('toggle-on')
        });
    });
}); 

您还必须使用委托事件处理程序。就像现在一样,您只有一个用于第一页切换的点击处理程序:

$('#example').on('click', '.toggle', function () {
    console.log($(this).data('toggle-on'));
    if ($(this).data('toggle-on') == true) {
        $(this).data('toggle-on', false)
        console.log('Turning off ' + $(this).data('contact-id'));
    } else {
        $(this).data('toggle-on', true)
        console.log('Turning on ' + $(this).data('contact-id'));
    }
});

demo -> http://jsfiddle.net/gmptpbqg/ 其中所有 .toggle 元素变为 "toggled" 并且在页面中正确记住开/关状态。

您可以像这样使用 DataTable 回调函数:

$('#email_alerts').DataTable( {

    "scrollCollapse": true,
    "paging":         true,
    "fnDrawCallback": function() {
        jQuery('.toggle').bootstrapToggle();
    }

} );

对我来说效果很好。祝你好运!!!

只是分享我的答案,因为我正在使用 Bootstrap Toggle 而不是 JQuery Toggle,但我使用了上面接受的一些答案:

$('#tblMyTable').on('draw.dt', function () {
            $('.myInputCheckBoxClass').bootstrapToggle();

            fncAdditionalBindingLogicForBoostrapToggle(); //In case you need any logic when you click the toggle button

        });

以下代码不起作用,因为每当我单击 DataTable 的分页时,绑定就会变得不稳定,因此我使用了上面修改后的版本代码:

$('#tblCFRMode').on('draw.dt', function () {

              $('.myInputCheckBoxClass').each(function () {

              if ($(this).prop('checked') == true) {
                $(this).bootstrapToggle('on');
              } else {
                $(this).bootstrapToggle('off');
              }
          });
      });