Datatables.php中

function: dataTable

->addColumn('select', function ($model) {
    return '<input type="checkbox" onclick="checkItem()" value="'.$model->id.'" name="someCheckbox" />';
})

function:colums

['data' => 'select', 'searchable' => false,'orderable' => false, 'title' => '<input type="checkbox" id="checkAll">全选'],

blade的index.blade.php中

<div class="box-tools pull-left">
    <div class="pull-right mb-10 hidden-sm hidden-xs">
        <a href="javascript:;" class="btn btn-danger btn-xs" onclick="delWord()">批量删除</a>

    </div>
</div>
<script>
    $("#checkAll").click(function () {
        // this 全选的复选框
        var userids=this.checked;
        //获取name=box的复选框 遍历输出复选框
        $("input[name=someCheckbox]").each(function(){
            this.checked=userids;
        });
    });

    function checkItem(){
        //获取选中复选框长度

        var length=$("input[name=someCheckbox]:checked").length;

        //未选中的长度
        var len=$("input[name=someCheckbox]").length;

        if(length==len){
            $("#checkAll").get(0).checked=true;
        }else{
            $("#checkAll").get(0).checked=false;
        }
    };

    function  delWord() {
        if(!confirm("确定要删除这些会员吗?")){
            return ;
        }
        var cks = document.getElementsByName("someCheckbox");
        var arr = [];
        //拼接所有的图书id
        for(var i=0;i<cks.length;i++){
            if(cks[i].checked){
                // str+="id="+cks[i].value+"&";
                arr[i] = cks[i].value;
            }
        }
        //去掉字符串末尾的‘&'
        // str=str.substring(0, str.length-1);
        // console.log(arr);
        $.post(
            "{{url('admin/delWord')}}",
            {id:arr,_token:"{{csrf_token()}}"},
            function (data) {
                // console.log(data);
                alert(data.message);
                window.location.href = window.location.href;
            }
        );

    }
</script>

文档更新时间: 2019-10-02 19:01   作者:cranux