Jquery - table.row(tr)未定义

时间:2015-04-08 12:16:03

标签: javascript jquery datatables jquery-datatables

我正在尝试使用datatables jQuery plugin,但遇到了麻烦,我无法弄清楚为什么会发生这种情况。

我的桌子上有一个动作栏:

table = $('#' + tableId).DataTable({
    processing: true,
    serverSide: true,
    ajax: dataUrl,
    deferRender: true,
    esponsive: true,
    pageLength: 15,
    pagingType: "full_numbers",
    stateSave: true,
    filter: true,
    language: {
        paginate: {
            next: " ",
            previous: " ",
            first: "First",
            last: "Last"
        }
    }

$(document).on('click', ".details-control2", function () {
    var tr = $(this).parent().parent(); // <-- finds the correct tr
    var row = table.row(tr);
    console.log(row); // <-- undefined, why??? 'table' is recognized correctly
}

更新 - 表格的HTML:

<tbody>
<tr id="row_0" role="row" class="odd">
    <td class="sorting_1"></td>
    <td><input type="image" src="/images/plus.png" class="details-control2"> </td>
    <td>rasplap.dll</td>
    <td></td>
    <td>WIN7X86</td>
    <td>DLL</td>
    <td>4/4/2015 3:45:45 PM</td>
    <td>4/4/2015 5:38:32 PM</td>
    <td>0</td>
</tr>
</tbody>

有什么建议吗?如果缺少任何信息,请告诉我。

2 个答案:

答案 0 :(得分:0)

$(this).parentNode.parentNode;

答案 1 :(得分:0)

这在数据表论坛中讨论:http://datatables.net/forums/discussion/11836/getting-data-on-click

我希望这会有所帮助:

$(document).on('click', ".details-control2", function () {

    var row = $(this).closest('tr'),
        data = table._(row),
        id = data[0].id;  

    //do something with your id
    //Get the position of the current data from the node
    var aPos = table.fnGetPosition(id);//you many need to use ('#' + id)

    // Get the data array for this row
    var aData = table.fnGetData(id[0]);//you many need to use ('#' + id[0])
});