javascript - 如何在其操作回调中禁用自定义 DataTables 按钮?

标签 javascript c# jquery datatables

这是在 C# ASP.NET MVC 5 网络应用程序中,使用 DataTables 版本 1.10.22。

我将 DataTable 配置为具有自定义按钮。按钮的 Action 是一个回调函数。该函数执行一次后,我想禁用该按钮。

我可以禁用与 DataTable 关联的所有按钮。但是,如何只禁用一个按钮?

DataTables 文档,例如 https://datatables.net/reference/api/buttons().disable() , 是否有一个示例似乎通过...它们的 CSS 类名称来识别某些按钮?

var table = $('#myTable').DataTable();
var buttons = table.buttons( ['.edit', '.delete'] );
buttons.disable();

但是,如何唯一标识我的自定义按钮?

按钮的 Action 回调函数似乎提供了几个代表按钮的参数。但是,node 似乎没有disable() 函数。将 config.enabled 更改为 false 无效。我还能尝试什么?

以下是我在 Views/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>

最佳答案

可以为每个 DataTables 按钮指定一个按钮名称和/或类名称 - 然后您可以使用其中任何一个来引用该按钮 - 例如:

$(document).ready(function() {

  var table = $('#myTable').DataTable( {
    dom: 'Bfrtip',
    "buttons": [
      {
        text: 'My Button',
        className: 'myButtonClass',
        name: 'myButtonName'
      }
    ]
  } );

  table.buttons( '.myButtonClass' ).disable();
  //table.buttons( 'myButtonName:name' ).disable();

});

在上面的例子中,按钮既有按钮名又有类名。

还有多种其他方法可以选择一个或多个按钮:

buttons( selector );

这些选择器 选项已记录在案here .

而且,是的,你问题中的那个例子......

var buttons = table.buttons( ['.edit', '.delete'] );

...确实在使用类名选择器。

关于javascript - 如何在其操作回调中禁用自定义 DataTables 按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65080835/

相关文章:

javascript - Nightwatch 从选择框中选择选项

javascript - 在 xasis 上使用 jsreport 和纪元时间绘制图表

c# - 报告服务 : Getting reporting services path

C# - 将一个枚举转换为另一个

javascript - jQuery 验证插件无法识别有效输入

javascript - 在 JavaScript 中写入 JSON 文件

javascript - 为什么我的 JavaScript 函数名称会冲突?

c# - 如何确定导致 Test Explorer 崩溃的异常?

javascript - 选择以前的 anchor 标记并使用 jQuery 添加一个类

javascript - 重新加载 html 元素