@@ -63,7 +63,25 @@ angular.module('ui.sortable', [])
63
63
ui . item . sortable . _destroy ( ) ;
64
64
}
65
65
66
- var opts = { } ;
66
+ // return the index of ui.item among the items
67
+ // we can't just do ui.item.index() because there it might have siblings
68
+ // which are not items
69
+ function getItemIndex ( ui ) {
70
+ return ui . item . parent ( ) . find ( '> [ng-repeat], > [data-ng-repeat], > [x-ng-repeat]' )
71
+ . index ( ui . item ) ;
72
+ }
73
+
74
+ // restrict the items option to include only ng-repeat items
75
+ function restrictItemsOption ( items ) {
76
+ items = items . trim ( ) ;
77
+ return items + '[ng-repeat],' + items + '[data-ng-repeat],' + items + '[x-ng-repeat]' ;
78
+ }
79
+
80
+ var opts = {
81
+ // this is jquery-ui sortable's default, we state it explicitly so even
82
+ // if it's not specified, we will apply restrictItemsOption on it
83
+ items : '> *'
84
+ } ;
67
85
68
86
// directive specific options
69
87
var directiveOpts = {
@@ -84,6 +102,8 @@ angular.module('ui.sortable', [])
84
102
85
103
angular . extend ( opts , directiveOpts , uiSortableConfig , scope . uiSortable ) ;
86
104
105
+ opts . items = restrictItemsOption ( opts . items ) ;
106
+
87
107
if ( ! angular . element . fn || ! angular . element . fn . jquery ) {
88
108
$log . error ( 'ui.sortable: jQuery should be included before AngularJS!' ) ;
89
109
return ;
@@ -114,9 +134,10 @@ angular.module('ui.sortable', [])
114
134
}
115
135
116
136
// Save the starting position of dragged item
137
+ var index = getItemIndex ( ui ) ;
117
138
ui . item . sortable = {
118
- model : ngModel . $modelValue [ ui . item . index ( ) ] ,
119
- index : ui . item . index ( ) ,
139
+ model : ngModel . $modelValue [ index ] ,
140
+ index : index ,
120
141
source : ui . item . parent ( ) ,
121
142
sourceModel : ngModel . $modelValue ,
122
143
cancel : function ( ) {
@@ -184,7 +205,7 @@ angular.module('ui.sortable', [])
184
205
// update that happens when moving between lists because then
185
206
// the value will be overwritten with the old value
186
207
if ( ! ui . item . sortable . received ) {
187
- ui . item . sortable . dropindex = ui . item . index ( ) ;
208
+ ui . item . sortable . dropindex = getItemIndex ( ui ) ;
188
209
var droptarget = ui . item . parent ( ) ;
189
210
ui . item . sortable . droptarget = droptarget ;
190
211
@@ -325,6 +346,10 @@ angular.module('ui.sortable', [])
325
346
value = wrappers [ key ] ( value ) ;
326
347
}
327
348
349
+ if ( key === 'items' ) {
350
+ value = restrictItemsOption ( value ) ;
351
+ }
352
+
328
353
opts [ key ] = value ;
329
354
element . sortable ( 'option' , key , value ) ;
330
355
} ) ;
0 commit comments