从jQuery Datatable隐藏列中获取值 [英] Get the values from jQuery Datatable hidden columns

查看:186
本文介绍了从jQuery Datatable隐藏列中获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a jQuery datatable populated with info from a db' table and I have hidden two columns, now I need to get the value from the two hidden columns but I couldn´t, this is the code of my datatable and had set the property visible: false for the two last columns

    $('#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"
            }
        }
    });

这是我从用户可见的列中获取值的方法,它工作得很好:

and this is the way I get the value from the columns that are visible to the user, it works just fine:

$("#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)中尝试了此尝试,但未成功

But how I get the values from the hidden columns? I have tried this in the $("#myTable").on('click', '#select', function (e) with no success

 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代码

could you please help me to get the values from the hidden columns with the script code showed? BTW this is the HTML code

<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>

推荐答案

使用以下代码:

$("#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 ...
});

这篇关于从jQuery Datatable隐藏列中获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆