2017-08-07 61 views
0

我有一個數據表,它通過ajax調用進行填充。以下是代碼:如何傳遞在jQuery數據表中選中複選框的行的列值

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的新手。請讓我知道如何實現這一點。

回答

1

我有這樣的想法:循環遍歷table的所有rows和測試如果rowcheckboxchecked與否。如果是,則提取所需單元格的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

相關問題