从jQuery Datatable隐藏列

时间:2017-04-19 21:02:20

标签: javascript jquery html datatables

我有一个jQuery数据表填充了来自db'表的信息,我隐藏了两列,现在我需要从两个隐藏列中获取值但是我不能,这是我的数据表的代码并且有为最后两列设置属性visible:false

    $('#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];

你能不能帮我从隐藏的列中获取显示脚本代码的值? BTW这是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 个答案:

答案 0 :(得分: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 ...
});

答案 1 :(得分:1)

如何获取表格的所有td值:

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

它们将存储在数组tdItems中。

答案 2 :(得分:0)

使用jQuery Datatable API函数获取包括隐藏列的完整行数据-

  • fnGetPosition
  • fnGetData

首先获取数据表的实例,然后执行函数以获取完整的行数据。 例如,您想单击特定的行以获得完整的行数据,下面的代码可以在表的fnDrawCallback回调函数内部提供帮助-

"fnDrawCallback": function (oSettings) {
    var oTable = $('#example').dataTable();
    $('#example tbody tr').on('click', 'tr', function () {            
        var position = oTable.fnGetPosition(this);
        var full_row = oTable.fnGetData(position);
        console.log(full_row);  // will print full row data including hide columns
    });              
 }

谢谢