简体   繁体   English

jQuery 选择器不适用于数据表

[英]jQuery selector won't work with Datatables

hey guys i have a problem with Datatable plugin the problem is that i can't select the checkbox in the action column to convert it into bootstrap-toggle it was working before but when i use the serverside of datatable and declare the checkbox in the controller nothing works (Sorry for my english ) please help!!嘿伙计们,我对 Datatable 插件有问题,问题是我无法选择操作列中的复选框将其转换为 bootstrap-toggle,它以前可以工作,但是当我使用数据表的服务器端并在控制器中声明复选框时没有任何效果(对不起我的英语)请帮忙!! this is the controller code这是控制器代码

return DataTables::of($participant)
        ->addColumn('action',function($participant){
            $route = route('admin.participant.presence',$participant);

            return '<form action="'.$route.'" method="PATCH">
            <input type="hidden" name="presence" value="0">
            <input type="checkbox" class="participation" onchange="this.form.submit()" '.isChecked($participant).' name="presence" value="1">
            </form>';
        })->make(true);

and here's the js code in the view and I think the problem comes from here这是视图中的js代码,我认为问题来自here

<script>
        $(document).ready( function(){
            var id = {{request()->route('id')}};
        $('#table').DataTable({
        "processing": true,
        "serverSide": true,
        "ajax": "/admin/evenment/event/participant/ajaxdatatable/"+id,
        "columns":[
            {"data": "adherent_id"},
            {"data": "nom_participant"},
            {"data": "prenom_participant"},
            {"data": "fonction"},
            {"data": "action"},
        ]
    });
    $('.participation').bootstrapToggle({
        on: 'Oui',
        off: 'Non',
        onstyle: 'success',
        offstyle: 'danger',
        width: '70'
    });
    });

    </script>

When using serverSide, the resulting table is displayed after the full page load.使用 serverSide 时,结果表在整个页面加载后显示。 Therefore the bootstrap elements have already been generated when datatable displays the results.因此,当 datatable 显示结果时,bootstrap 元素已经生成。

You can solve this by calling a function which takes care of displaying bootstrap elements in the "complete" ajax handler.您可以通过调用一个函数来解决这个问题,该函数负责在“完整”ajax 处理程序中显示引导程序元素。 (here the $.extend applies to a datatable options object) (这里 $.extend 适用于数据表选项对象)

$.extend(true, options, {
    "ajax": {
        "url": self.data('url'),
        "data": function ( d ) {
            d.action = "drawDatatable"
        },
        "type": "POST",
        "complete": function() {
            prepareDisplayElements("#"+self.attr("id"));
        }
    }
});

So in your case this could be something simple like this:所以在你的情况下,这可能是这样的简单:

$(document).ready( function(){
    var id = {{request()->route('id')}};

    $('#table').DataTable({
        "processing": true,
        "serverSide": true,
        "ajax": {
            "url": "/admin/evenment/event/participant/ajaxdatatable/"+id,
            "complete": function() {
              $('.participation').bootstrapToggle({
                  on: 'Oui',
                  off: 'Non',
                  onstyle: 'success',
                  offstyle: 'danger',
                  width: '70'
              });
            }
        },
        "columns":[
            {"data": "adherent_id"},
            {"data": "nom_participant"},
            {"data": "prenom_participant"},
            {"data": "fonction"},
            {"data": "action"},
        ]
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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