【问题标题】:How to pass values of a column of rows whose checkboxes are selected in jQuery Data Table如何传递在jQuery数据表中选中复选框的行列的值
【发布时间】:2018-01-14 14:18:42
【问题描述】:

我有一个由 ajax 调用填充的数据表。以下是代码:

oTable = $('#lenderList').dataTable(
        {
            bServerSide: true,
            bProcessing: true,
            searching: true,
            sAjaxSource: "loanAdminAjax?ajax=true&searchCol="+$('#category').val(),
            sServerMethod: 'POST',
            sPaginationType: "full_numbers",

            columnDefs:[
                {
                    targets:8,
                    checkboxes:{
                        selectrow:true
                    }
                }
            ],
            aoColumns: [
                {
                    "sName": "loanApplicationNumber",
                    mData: "loanApplicationNumber"
                },
                {
                    "sName": "name",
                    mData: "name"
                },
                {
                    "sName": "submissionDate",
                    mData: "submissionDate"
                },
                {
                    "sName": "kycEmailId",
                    mData: "kycEmailId"
                },
                {
                    "sName": "appVersion",
                    mData: "appVersion"
                },
                {
                    "sName": "documentStatus",
                    mData: "documentStatus"
                },
                {
                    "sName": "latestRemark",
                    mData: "latestRemark"
                },
                {
                    "sName": "appName",
                    mData: "appName"
                },
                {
                    nData:   "salaryTransaction",
                    render: function ( salaryTransaction, type, row ) {
                        if ( type === 'display' ) {
                            return '<input type="checkbox" class="editor-active">';
                        }
                        return salaryTransaction;
                    },
                    className: "dt-body-center"
                }
            ],
            dom: '<"button">lfrtip'
        }
    );
});

我必须考虑用户选中复选框的各个行的所有贷款申请号。因此,我可以在单击按钮时触发事件,但是如何获取单击复选框的行的贷款申请编号的值。

我是 javaScript 的新手。请让我知道如何实现。

【问题讨论】:

    标签: javascript jquery checkbox datatables


    【解决方案1】:

    我有这个想法:遍历table 的所有rows,并测试rowcheckbox 是否为checked。如果是,则提取所需单元格的value

    试试这个:

    $(document).ready(function(){
        $("button").click(function(){
          oTable = $("#lenderList").dataTable(); // Get the datatable,
          var loanApplicationNumbers = []; // An array that will contain the "loan application numbers"
          oTable.$('tr').each(function(index,rowhtml){ //Loop through the table rows
            //Check the state of the checkbox
            var checked= $('input[type="checkbox"]:checked',rowhtml).length;
            if (checked==1){
              //If the checkbox is checked, then add the inner text of the cell to the array
              loanApplicationNumbers.push(rowhtml.children[1].innerText);
            }
          });
          console.log(loanApplicationNumbers); //Do whatever you want
        });
    });
    

    注意:rowhtml.children是该行单元格的Array,如果你想获取第一个单元格的值,你应该做rowhtml.children[0].innerText

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-22
      • 1970-01-01
      • 2019-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多