2017-04-19 142 views
1

我有一個jQuery數據表填充信息從一個數據庫'表,我有隱藏兩列,現在我需要從兩個隱藏列的值,但我不能,這是我的DataTable的代碼,並設置屬性可見:假的最後兩個欄從jQuery Datatable隱藏列獲取值

$('#myTable').DataTable({ 
     searching: false, 
     paging: true, 
     responsive: true, 
     ordering: false, 
     bInfo: false, 
     bLengthChange: false, 
     processing: true, 
     info: false, 
     deferRender: true, 
     orderMulti: false, 
     "ajax": { 
      "url": "../home/CargarTabla?id=" + noDocumento, 
      "type": "GET", 
      "datatype": "json" 
     }, 
     "columns": [ 
       { "data": "nombres", "autoWidth": true, "orderable": false }, 
       { "data": "apellidos", "autoWidth": true, "orderable": false }, 
       { "data": "dui", "autoWidth": true, "orderable": false }, 
       { "data": "numero_isss", "autoWidth": true, "orderable": false }, 
       { "data": "cargo_participante", "autoWidth": true, "orderable": false }, 
       { "data": "genero", "autoWidth": true, "orderable": false, "visible": false }, 
       { "data": "nivel_puesto", "autoWidth": true, "orderable": false, "visible": false }, 
       { "defaultContent": " <a href='#' id='select'>Modificar</a> ", "autoWidth": true, "orderable": false } 
     ], 
     "oLanguage": { 
      "sEmptyTable": "No hay registros disponibles", 
      "sInfo": " _TOTAL_ registros. Mostrando de (_START_ a _END_)", 
      "sLoadingRecords": "Por favor espera - Cargando...", 
      "sSearch": "Filtro:", 
      "sLengthMenu": "Mostrar _MENU_", 
      "oPaginate": { 
       "sLast": "Última página", 
       "sFirst": "Primera", 
       "sNext": "Siguiente", 
       "sPrevious": "Anterior" 
      }, 
      "oAria": { 
       "sSortAscending": ": Activar para ordenar la columna de manera ascendente", 
       "sSortDescending": ": Activar para ordenar la columna de manera descendente" 
      } 
     } 
    }); 

,這是我從那些對用戶可見的列值的方式,它工作得很好:

$("#myTable").on('click', '#select', function (e) { 
      e.preventDefault(); 
      var currentRow = $(this).closest("tr"); 
      var Nombres = currentRow.find("td:eq(0)").text(); 
      var Apellidos = currentRow.find("td:eq(1)").text(); 
      var DUI = currentRow.find("td:eq(2)").text(); 
      var ISSS = currentRow.find("td:eq(3)").text(); 
      var Cargo = currentRow.find("td:eq(4)").text(); 

      alert(Nombres + Apellidos + DUI +ISSS+ Cargo); 
     }); 

但是我如何從隱藏的列中獲取值?我在$("#myTable").on('click', '#select', function (e)沒有成功

alert(table.row(this).data()[5]); 
alert(table.row(this).data()[6]); 

別的辦法沒有成功嘗試這種

var row = $(this).parents("td")[0]; 
var pos = oTable.fnGetPosition(row); 
var Genero = oTable.fnGetData(pos[5])["idGenero"]; 

和最後

var arr = $('#myTable').dataTable().fnGetData($(currentRow)); 
      var Genero = arr[5]; 
      var Nivel = arr[6]; 

能否請你幫我從隱藏得值顯示腳本代碼的列?順便說一句,這是HTML代碼

<div class="table-responsive"> 
    <table class="table table-striped table-condensed" id="myTable" style="width:100%; margin:0 auto;"> 
     <thead> 
      <tr> 
       <th>Nombres</th> 
       <th>Apellidos</th> 
       <th>DUI</th> 
       <th>ISSS</th> 
       <th>Cargo</th> 
       <th>Sexo</th> 
       <th>Nivel</th> 
       <th></th> 
      </tr> 
     </thead> 

     <tbody></tbody> 
    </table> 
</div> 

回答

3

使用下面的代碼:

$("#myTable").on('click', '#select', function (e) { 
    e.preventDefault(); 

    var currentRow = $(this).closest("tr"); 

    var data = $('#myTable').DataTable().row(currentRow).data(); 
    console.log(data['genero']); 
    console.log(data['nivel_puesto']); 

    // ... skipped ... 
}); 
+0

真的有幫助,感謝@Gyrocode –

1

如何獲得所有表的td值:

var tdItems=[]; 
$("#myTable tr td").each(function(){ 
    tdItems.push($(this).text());  
}); 

他們將被存儲在陣列tdItems英寸