如何在实例化数据表后更新/添加按钮

时间:2016-06-29 16:26:36

标签: javascript datatable datatables datatables-1.10

我是JavaScript的新手,我缺乏知识javascript对象。 我想知道如何在创建后添加数据表1.10按钮的扩展名。

我的代码是:

var table;
$('#MyDiv').DataTable({someCode;});
$.fn.dataTable.ext.buttons.ok = {
    text: 'OK',
    action: function (e, dt, node, config) {
        console.log("Hi");
    }
};
table = $('#MyDiv').DataTable();
//!Here I want to add my button in table var!

1 个答案:

答案 0 :(得分:1)

选项1

最简单的方法(在我看来)是使用按钮声明的选项形式,而不是你试图在这里使用的函数形式。在你的情况下,这看起来像这样:

table = $('#MyDiv').DataTable({
    /*Other DataTables config options go here*/
    buttons: [
        {
            text: 'OK',
            action: function ( e, dt, node, config ) {
                console.log("Hi");
            }
        }
    ]
});

这可以在DataTables examples中找到,这是DataTables信息的重要来源。

选项2

如果你希望继续使用函数表示法,那么你只需要在选项中添加一个按钮声明,而不是上面例子中的整个动作/文本块。见下文:

var table;
//You should not have 2 .DataTable() calls, so I removed this one
//Move any other options you had to the other call below
$.fn.dataTable.ext.buttons.ok = {
    text: 'OK',
    action: function (e, dt, node, config) {
        console.log("Hi");
    }
};
table = $('#MyDiv').DataTable({
    /*Other DataTables config options go here*/
    buttons: [
        'ok'
    ]
});

无论哪种方式都可行,它只取决于您希望如何组织代码。

我还会引导您访问DataTables网站上的custom buttons documentation以获取更多信息或查看我从哪里获得这些代码块。

相关问题