编程语言
首页 > 编程语言> > Javascript-如何在Kendo UI工具栏中动态添加按钮?

Javascript-如何在Kendo UI工具栏中动态添加按钮?

作者:互联网

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: '='
}

但是,当我在工具栏按钮数组中添加按钮时,该按钮不显示.

解决方法:

Kendo UI工具栏中有一个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>

标签:angularjs,angularjs-directive,kendo-ui,arrays,javascript
来源: https://codeday.me/bug/20191119/2039908.html