DataTables中设置checkbox回显选中

场景

在进行编辑时DataTables中第一列的checkbox回显默认选中。

效果

实现

dataTables赋值代码

 columns: [
                { data: 'id' ,
                    "orderable" : false},
                { data: 'goodsLocationNumber' },
                { data: 'locationTypeName' ,
                    "orderable" : false},
                { data: 'saveMaterialTypeName' ,
                    "orderable" : false},
                { data: 'materielStatusName',
                    "orderable" : false},
                { data: 'shelveName' ,
                    "orderable" : false},
                { data: 'remark',
                    "orderable" : false }
            ],columnDefs: [{
                //   指定第1列,从0开始,0表示第一列,1表示第二列……
                "targets": 0,
                "bSortable": false,
                "render": function(data, type, row, meta) {
                    if (row.isSelected == 1){
                        return '<input type="checkbox" class="checkchild" onclick="childClick(this)" checked="checked" value="' + row.id + '" />'
                    }
                    return '<input type="checkbox" class="checkchild" onclick="childClick(this)" value="' + row.id + '" />'
                }
            }],

完整初始化DataTables代码

$(document).ready(function() {
    //获取下拉框的值,判断是否为空,不为空则是编辑,那么就要初始化dataTables
    var selected = $("#businessInitiator option:selected").val();
    if(selected!=""){
        var t = $('#example').DataTable({
            "language": {
                "processing": "处理中...",
                "lengthMenu": "显示 _MENU_ 项结果",
                "zeroRecords": "没有匹配结果",
                "info": "显示第 _START_ 至 _END_ 项结果,共 _TOTAL_ 项",
                "infoEmpty": "显示第 0 至 0 项结果,共 0 项",
                "infoFiltered": "(由 _MAX_ 项结果过滤)",
                "infoPostFix": "",
                "search": "搜索:",
                "searchPlaceholder": "搜索...",
                "url": "",
                "emptyTable": "表中数据为空",
                "loadingRecords": "载入中...",
                "infoThousands": ",",
                "paginate": {
                    "first": "首页",
                    "previous": "上页",
                    "next": "下页",
                    "last": "末页"
                },
                "aria": {
                    paginate: {
                        first: '首页',
                        previous: '上页',
                        next: '下页',
                        last: '末页'
                    },
                    "sortAscending": ": 以升序排列此列",
                    "sortDescending": ": 以降序排列此列"
                },
                "decimal": "-",
                "thousands": "."
            },
            "processing": true,
            "searching" : false,
            "pageLength": 100,
            "serverSide": true,
            "columnDefs": [ {
                "searchable": false,
                "orderable": false,
                "targets": "_all",
            }],
            "dom": '<"top">rt<"bottom"flpi><"clear">',

            columns: [
                { data: 'id' ,
                    "orderable" : false},
                { data: 'goodsLocationNumber' },
                { data: 'locationTypeName' ,
                    "orderable" : false},
                { data: 'saveMaterialTypeName' ,
                    "orderable" : false},
                { data: 'materielStatusName',
                    "orderable" : false},
                { data: 'shelveName' ,
                    "orderable" : false},
                { data: 'remark',
                    "orderable" : false }
            ],columnDefs: [{
                //   指定第1列,从0开始,0表示第一列,1表示第二列……
                "targets": 0,
                "bSortable": false,
                "render": function(data, type, row, meta) {
                    if (row.isSelected == 1){
                        return '<input type="checkbox" class="checkchild" onclick="childClick(this)" checked="checked" value="' + row.id + '" />'
                    }
                    return '<input type="checkbox" class="checkchild" onclick="childClick(this)" value="' + row.id + '" />'
                }
            }],
            "ajax": function (data, callback, setting) {
                data.editActionId = $("#refundOrderId").val();
                data.selected=selected;
                $.ajax({
                    type: 'POST',
                    url: "/busGoodsLocation/getRejectsLocationsList",
                    cache: false,  //禁用缓存
                    //async: true,
                    data: JSON.stringify(data),  //传入组装的参数
                    contentType: "application/json",
                    dataType: "json",
                    success: function (result) {
                        callback(result);
                    }
                })
            }
        });
    }
});

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

霸道流氓气质

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值