简体   繁体   English

DataTables API row()。data()

[英]DataTables API row().data()

Hi everybody and happy new year :) So, I use dataTables library. 大家好,新年快乐:)所以,我使用dataTables库。 In their web site i found this example, where function must return the row of table, which was clicked. 在他们的网站上我找到了这个例子,其中函数必须返回被点击的表行。

var table = $('#example').DataTable();

$('#example tbody').on( 'click', 'tr', function () {
    console.log( table.row( this ).data() );
} );

I try use this example for my code, but i have the error 我尝试将此示例用于我的代码,但我有错误

Uncaught TypeError: aucTable.row is not a function 未捕获的TypeError:aucTable.row不是函数

my code: 我的代码:

var mainTable = $('#mainTable');
$(document).ready(function () {
        mainTable.dataTable({
            'searching': false,
            'ajax': 'assets/static_data/data.json',
            'columns': [
                {
                    title: "Name",
                    data: "name"
                },
                {
                    title: "Office",
                    data: "office"
                },
                {
                    title: "Extn.",
                    data: "extn"
                },
                {
                    title: "Salary",
                    data: "salary"
                },
                {
                    title: "Start date",
                    data: "start_date"
                },
                {
                    title: "Details",
                    data: null,
                    defaultContent: "<button class='details-btn btn'>More details</button>",
                    sorting: false
                }
            ]
        });
    });

$('#mainTable').on('click', '.details-btn', function () {

        var selectedRow = aucTable.row(this).data();
        console.log(selectedRow);

        $("<div id='details-dialog'/>").dialog({
            modal: true,
            show: true,
            maxWidth: 620,
            maxHeight: 300,
            minWidth: 500,
            minHeight: 200,
            title: "Hello World"
        });
    });

Can somebody tell me, why I have this error? 有人能告诉我,为什么我有这个错误? And why i can't get the row, which was clicked? 为什么我不能得到被点击的行?

Tanks for everybody. 每个人的坦克。 Best regard and have fun. 最好的关注,享受乐趣。

I'm not familiar with datatables , however you might try changing it to the following: 我不熟悉的datatables ,但是你可以尝试将其更改为以下几点:

var mainTable = null;
$(document).ready(function () {
    mainTable = $('#mainTable').dataTable({...});
});

$('#mainTable').on('click', '.details-btn', function () {
    var tr = $(this).closest('tr');
    var selectedRow = mainTable.row(tr).data();
    console.log(selectedRow);
    //...
});

Note that I'm storing the result to the $('#mainTable').dataTable() call in the mainTable variable so that you can reference it later when calling the row() function. 请注意,我将结果存储在mainTable变量中的$('#mainTable').dataTable()调用中,以便稍后在调用row()函数时可以引用它。

The other thing to note is that in your click handler, it looks like you need to find the tr from the datatable - calling mainTable.row(this) does not yield a row because this is the button that was clicked, not the row of the table. 另外需要注意的是,在你的点击处理程序中,看起来你需要从数据表中找到tr - 调用mainTable.row(this)不会产生一行,因为this是点击的按钮,而不是行桌子。

See this link for an example that seems similar to what you're doing. 请参阅此链接以获取与您正在执行的操作类似的示例。

In the current code, the acuTable var not exist. 在当前代码中, acuTable var不存在。 So you can change your code in order to have an acuTable var pointing to the datatable instance, something like: 因此,您可以更改代码,以便将acuTable var指向数据表实例,例如:

var mainTable = $('#mainTable');
var acuTable;
$(document).ready(function () {
        acuTable = mainTable.dataTable({
            'searching': false,
            'ajax': 'assets/static_data/data.json',
            'columns': [
                {
                    title: "Name",
                    data: "name"
                },
                {
                    title: "Office",
                    data: "office"
                },
                {
                    title: "Extn.",
                    data: "extn"
                },
                {
                    title: "Salary",
                    data: "salary"
                },
                {
                    title: "Start date",
                    data: "start_date"
                },
                {
                    title: "Details",
                    data: null,
                    defaultContent: "<button class='details-btn btn'>More details</button>",
                    sorting: false
                }
            ]
        });
    });

$('#mainTable').on('click', '.details-btn', function () {

        var selectedRow = aucTable.row(this).data();
        console.log(selectedRow);

        $("<div id='details-dialog'/>").dialog({
            modal: true,
            show: true,
            maxWidth: 620,
            maxHeight: 300,
            minWidth: 500,
            minHeight: 200,
            title: "Hello World"
        });
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM