Javascript 如何在其操作回调中禁用自定义DataTables按钮?
这是一个C#ASP.NET MVC 5 web应用程序,使用DataTables版本1.10.22 我将DataTable配置为具有自定义按钮。按钮的操作是一个回调函数。在该函数执行一次之后,我想禁用该按钮 我可以禁用与DataTable关联的所有按钮。但是,如何仅禁用一个按钮 DataTables文档,例如,有一个示例,似乎通过。。。他们的CSS类名Javascript 如何在其操作回调中禁用自定义DataTables按钮?,javascript,c#,jquery,datatables,Javascript,C#,Jquery,Datatables,这是一个C#ASP.NET MVC 5 web应用程序,使用DataTables版本1.10.22 我将DataTable配置为具有自定义按钮。按钮的操作是一个回调函数。在该函数执行一次之后,我想禁用该按钮 我可以禁用与DataTable关联的所有按钮。但是,如何仅禁用一个按钮 DataTables文档,例如,有一个示例,似乎通过。。。他们的CSS类名 var table = $('#myTable').DataTable(); var buttons = table.buttons( ['.e
var table = $('#myTable').DataTable();
var buttons = table.buttons( ['.edit', '.delete'] );
buttons.disable();
但是,如何唯一标识自定义按钮
按钮的操作回调函数似乎提供了几个表示按钮的参数。但是,节点
似乎没有disable()
功能。将config.enabled
更改为false无效。我还能试什么
以下是我在视图/Foo/Index.cshtml中尝试执行的操作:
<script>
$( document ).ready( onReady );
function onReady()
{
var config = {};
config.buttons =
[
// A button to create data for the table.
{
text: '<span class="fa fa-plus"/>',
titleAttr: 'Create states',
action: createStates,
enabled: true,
}
... other buttons ...
];
... other configuration ...
$( '#state-table' ).DataTable( config ) );
}
/**
* Create the states.
*
* Parameters:
* e (object): The event.
* table (object): The DataTable.
* node (jQuery): The jQuery instance of the button that was clicked.
* config (object): The button configuration.
*/
function createStates( e, table, node, config )
{
//------------------------------
// Create client-side state data in the table.
table.clear();
for ( var i = 0; i < 3; i++ )
{
var data = { Id: i, Name: 'state ' + i };
table.row.add( data );
}
//------------------------------
// Calling draw() at the end updates the DataTable internal caches.
table.rows().draw();
//------------------------------
// Disable the button, so that states cannot be created again.
// *** How ? ***
// Just changing this property has no effect on the button.
config.enabled = false;
// This disables all buttons, not just the one I want.
table.buttons().disable();
}
</script>
$(文件).ready(onReady);
函数onReady()
{
var config={};
配置按钮=
[
//用于为表创建数据的按钮。
{
文本:“”,
titleAttr:“创建状态”,
行动:各国,
启用:对,
}
…其他按钮。。。
];
…其他配置。。。
$('#state table').DataTable(config));
}
/**
*创建状态。
*
*参数:
*e(对象):事件。
*表(对象):数据表。
*节点(jQuery):单击的按钮的jQuery实例。
*配置(对象):按钮配置。
*/
函数createStates(e、表、节点、配置)
{
//------------------------------
//在表中创建客户端状态数据。
表1.clear();
对于(变量i=0;i<3;i++)
{
var data={Id:i,Name:'state'+i};
表.行.添加(数据);
}
//------------------------------
//最后调用draw()更新DataTable内部缓存。
table.rows().draw();
//------------------------------
//禁用该按钮,以便无法再次创建状态。
//***怎么做***
//仅更改此属性对按钮没有影响。
config.enabled=false;
//这将禁用所有按钮,而不仅仅是我想要的按钮。
table.buttons().disable();
}
可以为每个DataTables按钮指定一个按钮名和/或类名,然后您可以使用其中一个来引用该按钮,例如:
$(文档).ready(函数(){
变量表=$('#myTable')。数据表({
dom:'Bfrtip',
“按钮”:[
{
文本:“我的按钮”,
类名:“myButtonClass”,
名称:“我的按钮名”
}
]
} );
table.buttons('.myButtonClass').disable();
//table.buttons('myButtonName:name').disable();
});
在上面的示例中,按钮同时具有按钮名和类名
选择一个或多个按钮还有多种其他方法:
buttons( selector );
这些选择器
选项已记录在案
是的,你问题中的例子
var buttons = table.buttons( ['.edit', '.delete'] );
…确实在使用类名选择器。谢谢@andrewjames,这非常有效。