How to dynamically disable ui-sortable directive in angular-ui

我的未来我决定 提交于 2019-12-03 10:38:12

The angular directive supports watching when the sortable options change:

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.

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

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.

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. However if you checked out the jQuery Sortable Docs it seems that there's an option for 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?

HTML

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

JS

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

vm.disableDragAndDrop = function(bVar)
{
    vm.groupSortable.disabled = bVar;
};
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!