Datatables with jQuery-toggles not working on page

2020-03-31 08:35发布

问题:

I have a Data-table that is being populated with server-side code.

<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>

I use this jQuery to initialize all of the jQuery Toggles to be set to what the database returned.

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

I use this code to alter their state when they are clicked on. Off goes to on and vice versa.

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

Now all of this works perfectly, except when I click on any page other than the first page for Data-tables. Page 2+ they all default to being turned on.

Any idea why this works for the first page but not for other pages in data-tables?

回答1:

Initialise the toggles plugin dynamically, so you also "toggles" elements injected when changing page, sorting etc :

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

You must also use a delegated event handler. As it is now, you only have a click-handler for the toggles on the first page :

$('#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/ where all .toggle elements become "toggled" and on / off state is remembered correctly across the pages.



回答2:

You can use a DataTable callback function like this:

$('#email_alerts').DataTable( {

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

} );

It works fine for me. Good luck!!!



回答3:

Just sharing my answer since I am working with Bootstrap Toggle and not JQuery Toggle, but I used some of the accepted answer above:

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

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

        });

Below code doesn't work since whenever I click the pagination of DataTable, the bindings become erratic, so I used the modified version code above:

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

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

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