javascript - jQuery 选择器不适用于数据表

标签 javascript jquery laravel datatables angular-bootstrap-toggle

嘿伙计们,我的数据表插件有问题 问题是我无法选择操作列中的复选框将其转换为 bootstrap-toggle 它之前可以工作,但是当我使用数据表的服务器端并在 Controller 中声明复选框时,没有任何效果(对不起我的英语) 请帮忙!! 这是 Controller 代码

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

这是 View 中的js代码,我认为问题来自这里

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

最佳答案

使用服务器端时,结果表会在整页加载后显示。因此,当数据表显示结果时,引导元素已经生成。

您可以通过调用一个函数来解决这个问题,该函数负责在“完整”ajax 处理程序中显示引导元素。 (这里 $.extend 适用于数据表选项对象)

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

所以在你的情况下,这可能是这样简单的事情:

$(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"},
        ]
    });
});

关于javascript - jQuery 选择器不适用于数据表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59228087/

相关文章:

javascript - “react-router”不包含名为 'browserHistory' 的导出

javascript - 将用户输入添加到数组中并在单击时显示

mysql - Laravel 查询生成器 - 获取表结果 `as`

php - 不止一名 guard 在路上

javascript - 执行存储方法时出现 Ajax CRUD 错误

javascript - session 未通过 AJAX 正确发送

javascript - 如何获得正确的列索引和行索引?

javascript - 为什么这段代码可以在 IE 中运行,而在 Firefox 和 Chrome 中运行失败?

javascript - 引用错误: $ is not defined issue

jquery - 如何使用jquery从多选下拉列表中获取选定的选项值