如何从数据表中获取复选框值?

时间:2019-01-08 04:09:12

标签: datatables

嗨,我想从数据表中获取选中的复选框值,在我的表中,我有两列,第一列是复选框,第二列是显示值。

这里只是返回一个复选框。我们怎么知道有点击发生? 救救我。

这是代码

function BindColumSelectTable(DataExchangeList) {
debugger
$('#columnSelectTable').DataTable({      
    "data": DataExchangeList,
    "destroy": true,
    "columns": [
        {
            data: 'check', render: function (data, type, row) {
                debugger;
                return '<input type="checkbox"/>'
            }
        },
        { data:"FieldCaption" },
    ],
    "columnDefs": [
        {                
            orderable: false,
            className: "select-checkbox",
            targets:0
        },
        { className:"tabletdAdjust","targets":[1]}
    ],       
});}

我正在使用jquery数据表

2 个答案:

答案 0 :(得分:0)

function BindColumSelectTable(DataExchangeList) {
$('#columnSelectTable').DataTable({      
"data": DataExchangeList,
"destroy": true,
"columns": [
    {
        data: 'check', render: function (data, type, row) {
            var checkbox = $("<input/>",{
              "type": "checkbox"
            });
        if(data === "1"){
            checkbox.attr("checked", "checked");
            checkbox.addClass("checked");
        }else{
            checkbox.addClass("unchecked");
        }
        return checkbox.prop("outerHTML")
      }
    },
    { data:"FieldCaption" },
],
"columnDefs": [
    {                
        orderable: false,
        className: "select-checkbox",
        targets:0
    },
    { className:"tabletdAdjust","targets":[1]}
],
});}

尝试一下

答案 1 :(得分:0)

这是我使用onclick函数的答案,每次单击都会触发功能

function BindColumSelectTable(DataExchangeList) {
debugger
$('#columnSelectTable').DataTable({      
    "data": DataExchangeList,
    "destroy": true,
    "columns": [
        {
            data: 'ColumnCheck', render: function (data, type, row) {
                debugger;                    
                return '<input type="checkbox" onclick="ColumnCheck(this)"/>'
            }
        },
        { data:"FieldCaption" },
    ],
    "columnDefs": [
        {                
            orderable: false,
            className: "select-checkbox",
            targets:0
        },
        { className:"tabletdAdjust","targets":[1]}
    ],       
});

}

上面的代码与我在问题中使用的相同,只是我添加的一件事是onclick函数

和onclick函数是

function ColumnCheck(thisObj) {
debugger;
var dataExchangeCheckColumnVM = $('#columnSelectTable').DataTable().row($(thisObj).parents('tr')).data();
var dataExchangeCheckColumnList = $('#columnSelectTable').DataTable().rows().data();
for (var i = 0; i < dataExchangeCheckColumnList.length; i++) {
    if (dataExchangeCheckColumnList[i].FieldCaption !== null) {
        if (dataExchangeCheckColumnList[i].FieldCaption === dataExchangeCheckColumnVM.FieldCaption) {
            dataExchangeCheckColumnList[i].ColumnCheck = thisObj.checked;
        }
    }
}
_dataExchangeColumnList = dataExchangeCheckColumnList;

}

所以我使用了** ColumnCheck **属性,它是布尔变量。如果选中复选框,则每次迭代都会添加一个真值