检索输入文本中的数据表jquery行的信息

时间:2017-03-17 17:59:16

标签: jquery datatables

我使用datatable jQuery来显示表中的数据,并为每一行添加了一个按钮<button>editar</button>,它在每行显示数据和按钮时工作正常,但我想检索输入类型中特定行的信息,问题是我不知道如何捕获事件点击数据表中的按钮并检索信息,请你帮我,这是我的代码到目前为止:< / p>

       $(document).ready(function () {
            $('#myTable').DataTable({
                searching: false,
                paging: true,
                responsive: true,
                "ajax": {
                    "url": "/home/loaddata",
                    "type": "GET",
                    "datatype": "json"
                },
                "columns": [
                        { "data": "pais", "autoWidth": true, "orderable": true },
                        { "data": "nombres", "autoWidth": true, "orderable": false },
                        { "data": "telefono", "autoWidth": true, "orderable": false },
                        { "data": "correo", "autoWidth": true, "orderable": false },
                        { "data": "dui", "autoWidth": true, "orderable": false },
                        { "data": "nit", "autoWidth": true, "orderable": false },
                        { "defaultContent": "<button>editar</button>"}
                ],
                "oLanguage": {
                    "sEmptyTable": "No hay registros disponibles",
                    "sInfo": "Hay _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"
                    }
                }

            });

            //This is the problem
            $('#myTable tbody').on('click', 'button.editar', function () {

                    alert("you just pressed the button");

            });

        });
    </script> 

1 个答案:

答案 0 :(得分:1)

使用以下代码:

$('#myTable tbody').on('click', 'button', function () {
   var data = $('#myTable').DataTable().row($(this).closest('tr')).data();

   console.log(data, data['nombres']);
});