简体   繁体   English

选中行时启用自定义按钮(默认情况下禁用)

[英]Enabling custom button (disabled by default) when row is selected

I have a DataTable displaying data for Branches with two custom buttons defined: Add and Update. 我有一个DataTable显示分支的数据,定义了两个自定义按钮:添加和更新。 They are initialized at the top of the Javascript section 它们在Javascript部分的顶部初始化

var buttons;
var tblBranch;

$.fn.dataTable.ext.buttons.add = {
            className: 'button-add',
            text: "Add Branch",
            action: function (dt) {
                onBtnAddClicked()
            }
        };

$.fn.dataTable.ext.buttons.update = {
            className: 'button-update',
            text: "Update",
            action: function (dt) {
                onBtnUpdateClicked()
            }
        };

I'd like to disable the Edit button on page load and only enable it to be clickable when a row has been selected. 我想在页面加载时禁用“编辑”按钮,并且仅在选择行时才能使其可单击。 Problem is, I'm using custom buttons and I can't find anything on datatables.net about how to enable/disable them depending on conditions. 问题是,我正在使用自定义按钮,我在datatables.net上找不到任何关于如何根据条件启用/禁用它们的内容。 So far what I've tried is this: 到目前为止,我尝试过的是:

tblBranch = $("#tblBranches").DataTable({
        dom: 'Blfrtip',
        buttons: {
            buttons :[
                'add', 'update'
            ]
        }
        select: true;
})

$("#tblBranches tbody").on('click', 'tr', function () {
        if (tblBranch.row(this).hasClass('selected')) {
             $('button-update').removeClass("DTTT_disabled");
        }
        else {
             table.$('tr.selected').removeClass('selected');
             $('button-update').addClass("DTTT_disabled");
        }
});

But I don't know what the code to disable the Edit button when the page loads should be like, and I've looked here , here , here and here . 但我不知道在页面加载时禁用编辑按钮的代码应该是什么样的,我已经看过这里这里这里这里

Thanks for any guidance. 谢谢你的指导。

The last link you are referring to hold the solution you are looking for. 您指的最后一个链接包含您正在寻找的解决方案。 But the documentation is a little bit vague, guess it need a solid example. 但文档有点模糊,猜测它需要一个坚实的例子。 It is not clear how you create the buttons (you show both methods) but below is an inline example, it would work with constructor as well. 目前尚不清楚如何创建按钮(显示两种方法),但下面是一个内联示例,它也适用于构造函数。 Simply give the button a class, like .edit and set it to disabled from start : 只需给按钮一个类,如.edit并从开始时将其设置为禁用:

var table = $('#example').DataTable( {
  dom: 'Bfrtip',
  select: true,
  buttons: [
    {
      text: 'Edit',
      className: 'edit',
      enabled: false,
      action: function (e, dt, node, config) {
        //do something
      }
    },
    {
      text: 'Add',
      action: function (e, dt, node, config) {
        //do something
      }
    }
  ]
})

Then use the Select plugins select and deselect events to update the enabled status of the .edit button : 然后使用Select plugins selectdeselect事件来更新.edit按钮的enabled状态:

$('#example').on('select.dt deselect.dt', function() {
  table.buttons( ['.edit'] ).enable(
    table.rows( { selected: true } ).indexes().length === 0 ? false : true
  )
})

demo -> https://jsfiddle.net/pmee6w2L/ 演示 - > https://jsfiddle.net/pmee6w2L/

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

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