简体   繁体   English

如何在angular-ui中动态禁用ui-sortable指令

[英]How to dynamically disable ui-sortable directive in angular-ui

I am using angular-ui for sortable using ui-sortable directive. 我使用angular-ui进行可排序使用ui-sortable指令。 Is it possible to dynamically enable/disable sortable functionality based on the scope state? 是否可以根据范围状态动态启用/禁用可排序功能? So I need to have a button which changes the state of the scope property and depending on this property sortable either should work or not. 所以我需要一个按钮来改变范围属性的状态,并根据这个属性进行排序要么应该工作,要么不工作。

The angular directive supports watching when the sortable options change: angular指令支持观察可排序选项何时更改:

scope.$watch(attrs.uiSortable, function(newVal, oldVal){

So all you had to do was look at the jqueryui sortable documentation, and update the correct property on the plugin. 所以你要做的就是查看jqueryui可排序文档,并更新插件上的正确属性。

Html HTML

<ul ui-sortable="sortableOptions" ng-model="items">
   <li ng-repeat="item in items">{{ item }}</li>
 </ul>
<button ng-click="sortableOptions.disabled = !sortableOptions.disabled">Is Disabled: {{sortableOptions.disabled}}</button>

JS JS

app.controller('MainCtrl', function($scope) {
  $scope.items = ["One", "Two", "Three"];

  $scope.sortableOptions = {
    disabled: true
  };
});

My first attempt at this would include a new directive with a link function that compiles a template fragment either including or not including ui-sortable based upon some value in the scope. 我对此的第一次尝试将包括一个带有链接函数的新指令 ,该函数根据范围中的某些值编译包含或不包括ui-sortable的模板片段。

Code when I have time. 我有时间的代码。

You could use ui-if to toggle between a ui-sortable version and a non-sortable version, however this is a horrible design. 您可以使用ui-ifui-sortable版本和不可排序版本之间切换,但这是一个可怕的设计。 However if you checked out the jQuery Sortable Docs it seems that there's an option for disabled . 但是,如果你检查了jQuery可排序文档 ,似乎有一个disabled选项。 If the directive currently watches the options object for changes, you could simply toggle this option. 如果指令当前正在查看选项对象以进行更改,则只需切换此选项即可。 If the options object is watched by reference and not by value, then perhaps you should open a pull-request with the tweak? 如果通过引用而不是值来监视选项对象,那么也许您应该通过调整打开一个pull-request?

HTML HTML

<div class="group-container" ui-sortable="vm.groupSortable" ng-model="group.groups">

JS JS

vm.groupSortable = {
    connectWith: ".group-container",
    disabled: true
};

vm.disableDragAndDrop = function(bVar)
{
    vm.groupSortable.disabled = bVar;
};

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

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