10

Here is a Plunker example. [updated the plunk based on below answers]

Updated Plunk

How can I disable sorting on just one or two items. For example, item 1 and item 3, so the user can't move those two items ?

I tried

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

in sortableOptions, but that disables sorting on the whole list.

I'm looking for something like

  <div ui-sortable="sortableOptions" ng-model="list">    
  <div ng-repeat="item in list">    
    <div ng-switch on="item.text">    

      <div ng-switch-when="Item 1" >    
        <div ui-sortable="sortableOptions.disabled=true">{{item.text}} - Sorting disabled</div>    
      </div>  

     <div ng-switch-default>    
        <div>{{item.text}} - Sorting enabled</div>    
      </div>  

    </div>    
  </div>    
</div>

Thanks.

2 Answers 2

16

You can configure a class for items that should not be dragged:

$scope.sortableOptions = {
cancel: ".unsortable",

Then conditionally add that class in ngRepeat:

<ul ui-sortable="sortableOptions" ng-model="list" class="list">
  <li ng-repeat="item in list" class="item"  
      ng-class="{'unsortable': item.text == 'Item 2'}">
    {{item.text}}

Update
You can prevent the item from being sorted like this:

$scope.sortableOptions = {
cancel: ".unsortable",
items: "li:not(.unsortable)",
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks for your answer. It works for most of the parts. But when I drag item3 and place it on top of item 2, item2 gets dropped from its original position. In my case, I only want the item1 to be fixed in a way that when the user tries to place any other items on top of item 1, sortableOptions should either trigger a sortable.cancel() or just drop the item below item 1. Here is my updated plunker. plnkr.co/edit/9W9XilAO7LS6aAhR0iXk?p=preview
Didn't get your updated answer before I posted mine...but the behavior you're getting is quite similar to what's happening on my example. For the most part it works...but if you drag Item 3...or Item 4 on top of Item 1, Item 1 is sorted down :S
items: "li:not(.unsortable)" prevents the unsortable items from being moved.
I've implemented 'div:not(.unsortable)', and it works well, except when I drag item 3 and drop it just above item 2, item 1 gets pushed down, and item 3 goes to the top. I will try to figure out a way around this, but any help would be much appreciated. Thanks. Here is my new plunk. plnkr.co/edit/KkTEakiWiiTjqSSndq9j?p=preview
Ok I solved it by following this, weblogs.asp.net/jeffwids/… , thanks to this blog
|
1

Have you tried to implement the update option?

From the ui-sortable documentation, a modified and over simplified version for your use case:

First, add a enabled property to your array:

    for (var i = 1; i <= 6; i++){
        tmpList.push({
          text: 'Item ' + i,
          value: i,
          enabled: true
        });
      }

and then assert it's status in the sortableOptions:

    $scope.sortableOptions = {
      update: function(e, ui) {
        if (ui.item.scope().item.enabled === false) {
          ui.item.sortable.cancel();
   }}};

https://github.com/angular-ui/ui-sortable

2 Comments

Thanks for your answer. Yes, I tried that too. But, it gets called only after the item is dragged. So, it still allows me to move the item from its original position. Here is the updated plunker. plnkr.co/edit/9W9XilAO7LS6aAhR0iXk?p=preview
Ah! I see. Yes, it doesn't allow you to drag it...but it can be sorted :|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.