jQuery DataTables在点击时获得id

时间:2014-08-25 02:31:47

标签: javascript jquery onclick datatables

我有一个带有静态列的数据表,其中填充了删除按钮,因此用户可以删除表中的条目。但是,我正在试图弄清楚如何获得排的' id'单击链接时的属性,以便在发送删除请求时将其包含为参数。

代码:

            // Set up our datatable
            var table = jQuery('#service_record').dataTable( {
                "data": t.responseJSON,
                "paging": true,
                "ordering": true,
                "order": [[ 0, "desc" ]],
                "info": false,
                "columns": columns,
                "columnDefs": [ {
                    "targets": -1,
                    "data": null,
                    "defaultContent": "<u><a href='#'>Delete</a></u>"
                } ],
                "fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull) {
                    jQuery(nRow).attr('id', aData['primary_id_field']);
                    return nRow;
                }
            });

            // Add a listener to respond to the deletion click
            jQuery('#service_record').on('click','.delete', function (e) {

                var id = jQuery(this).attr('id');

                // Return
                return false;
            });

1 个答案:

答案 0 :(得分:0)

aData[]是一个数组,需要一个整数,而不是一个字符串。

因此,您需要执行aData['primary_id_field']或主ID的列索引而不是aData[0]。因此,如果ID位于第一列,则为0等。