3

I am working on Angular UI for making my menus sortable, sometimes the code works and sometimes its breaks like duplicating the entries or filling the blank menus when I am re arranging items.

Following is the full code -

<!doctype html>
<html lang="en" ng-app="myapp">
<head>
    <title>Angular Sortable Demo</title>
    <script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.21/jquery-ui.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.min.js"></script>
    <script src="http://cdnjs.cloudflare.com/ajax/libs/angular-ui/0.4.0/angular-ui.min.js"></script>
    <script>
        var myapp = angular.module('myapp', ['ui']);

        myapp.controller('controller', function ($scope) {
            $scope.list = ["one", "two", "three", "four", "five", "six"];
        });

        //angular.bootstrap(document, ['myapp']);   
    </script>
</head>
<body>
<div ng:controller="controller">
    <ul ui:sortable ng:model="list">
        <li ng:repeat="item in list" class="item">{{item}}</li>
    </ul>
    <hr />
    <div ng:repeat="item in list">{{item}}</div>
</div>
</body>
</html>
<style>
    .item, .placeholder {
    padding: 2px;
    width: 50px;
    height: 20px;
    border: 1px solid #333;
    background: #EEE;
}

.placeholder {
    background: #AEF;
}

</style>

Also If someone could help me while creating this in JSFiddle as I tried this but unable to set it as a fiddle - Fiddle Link

EDIT-

This is how it becomes at times Screenshot (Menu Items duplicated)

6
  • You need to check up typo ng-repeat, ng-model, ... Commented Jul 8, 2014 at 6:18
  • @JungryulChoi No it works even with the : Commented Jul 8, 2014 at 6:21
  • check this for a "fiddle": plnkr.co/edit/nAMngvPX4vZWP0dqFZND?p=preview no changes made though, not sure what's wrong yet. Commented Jul 8, 2014 at 6:23
  • You need to add ng-app in fiddle jsfiddle.net/k2cb2/10 Commented Jul 8, 2014 at 6:32
  • @ChrisPreston Try re arranging "six" element multiple times you can see that it breaks Commented Jul 8, 2014 at 6:42

2 Answers 2

7

I wrote my own directive (plnkr):

<ol dnd-list="wrap.names">
    <li ng-repeat="name in wrap.names">{{name}} is {{$index}}</li>
</ol>


// directive for a single list
app.directive('dndList', function() {
     return function(scope, element, attrs) {

        // variables used for dnd
        var toUpdate;
        var startIndex = -1;

        // watch the model, so we always know what element
        // is at a specific position
        scope.$watch(attrs.dndList, function(value) {
            toUpdate = value;
        },true);

        // use jquery to make the element sortable (dnd). This is called
        // when the element is rendered
        $(element[0]).sortable({
            items:'li',
            start:function (event, ui) {
                // on start we define where the item is dragged from
                startIndex = ($(ui.item).index());
            },
            stop:function (event, ui) {
                // on stop we determine the new index of the
                // item and store it there
                var newIndex = ($(ui.item).index());
                var toMove = toUpdate[startIndex];
                toUpdate.splice(startIndex,1);
                toUpdate.splice(newIndex,0,toMove);

                // we move items in the array, if we want
                // to trigger an update in angular use $apply()
                // since we're outside angulars lifecycle
                scope.$apply(scope.model);
            },
            axis:'y'
        })
    }
});
Sign up to request clarification or add additional context in comments.

Comments

1

Its Working Fine.... Take a look at this

Preview

JSFiddle

enter image description here

html

<div ng:controller="controller">
    <ul ui:sortable ng:model="list">
        <li ng:repeat="item in list" class="item">{{item}}</li>
    </ul>
    <hr />
    <div ng:repeat="item in list">{{item}}</div>
</div>

script

var myapp = angular.module('myapp', ['ui']);

myapp.controller('controller', function ($scope) {
    $scope.list = ["one", "two", "three", "four", "five", "six"];
});

angular.bootstrap(document, ['myapp']);

5 Comments

I tried yours fiddle too but it breaks jsbin.com/figisage/1 & screenshot - awesomescreenshot.com/02233yzle4
its working fine within jsbin, then how come..... here break means what, is it means when you applied that in your application, is the screen short given is part of that
No its from your jsBin..just try drag drop "six" element up, down etc and you will see in your demo too that few items either duplicated or removed.
yeah its working but I guess you replaced the library..I am looking for this library because I am using the same at multiple places :)
Yeah sure..was still searching to make it working with my current version

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.