【问题标题】:How to add buttons dynamically in Kendo UI toolbar?如何在 Kendo UI 工具栏中动态添加按钮?
【发布时间】:2015-08-03 20:18:20
【问题描述】:

我的 Kendo UI 工具栏有问题。实际上,每个按钮都是在工具栏初始化期间加载的。

但有些要求要求动态添加按钮。

这是我们如何在页面控制器中加载按钮的示例:

...
$scope.toolbarButtons = [{
    type: 'button',
    text: 'Button 1',     
    click: 'clickButton1'
}, {
    type: 'toggleButton',
    text: 'Button 2'
}, {
    type: 'button',
    text: 'Button 3'
}
...

以及我们如何添加工具栏并将按钮传递给指令:

<toolbar buttons="toolbarButtons"></toolbar>

return {
    scope: false,
    restrict: 'E',
    template: '<div kendo-toolbar="toolbar"></div>',
    controller: 'ToolbarController',
    link: function ($scope, element, attr) {

        $scope.buttons = $scope[attr.buttons];

        // Code to manage the toolbar
        ...
    };

我尝试更改按钮数组的范围绑定:

scope: {
    buttons: '='
}

但是当我在toolbarButtons数组中添加一个按钮时,按钮是不显示的。

【问题讨论】:

    标签: javascript arrays angularjs angularjs-directive kendo-ui


    【解决方案1】:

    在 Kendo UI toolbar 中有一个 add 方法用于添加按钮。你应该这样做:

    var toolbar = $("#toolbar").data("kendoToolBar");
    toolbar.add ({ type: "button", text: "Button N", id: "buttonN" });
    

    例子:

    $(document).ready(function() {
      $("#toolbar").kendoToolBar({
        items: [
          {
            type: 'button',
            text: 'Button 1'
          },
          {
            type: 'button',
            text: 'Button 2'
          },
          {
            type: 'button',
            text: 'Button 3'
          }
        ]
      });
    
      var n = 4;
      $("#add").on("click", function() {
        var toolbar = $("#toolbar").data("kendoToolBar");
        toolbar.add ({ type: "button", text: "Button " + n, id: "button" + n });
        n++;
      });
    });
    <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2015.2.624/styles/kendo.common.min.css" />
    <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2015.2.624/styles/kendo.default.css" />
    
    <script src="https://kendo.cdn.telerik.com/2015.2.624/js/jquery.min.js"></script>
    <script src="https://kendo.cdn.telerik.com/2015.2.624/js/kendo.all.min.js"></script>
    
    <button class="k-button" id="add">Add Button</button>
    <div id="toolbar"></div>

    【讨论】:

    • 感谢您的回复,但我尝试做的是操作按钮数组以在 X 位置插入项目作为示例。这就是为什么我尝试更改按钮数组的绑定。
    猜你喜欢
    • 2016-07-21
    • 2013-01-30
    • 1970-01-01
    • 2014-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多