angular-ui / ui-sortable

jQuery UI Sortable for AngularJS
http://angular-ui.github.io/ui-sortable/
MIT License
1.26k stars 443 forks source link

ng-repeat-start and ng-repeat-end cause wonky behaviour in indexes #64

Open shinkathe opened 10 years ago

shinkathe commented 10 years ago

Using ng-repeat start and ng-repeat-end to repeat say, two TR-elements inside a table, and then using sorting on that tbody-element, causes the index that ui-sortable uses to be doubled.

So inside the callbacks.stop() function, where ui.item.sortable.dropindex is checked, it reports a value that is double the real value. And for every TR element added, this value is multiplied.

I have something like this:

<tbody ui-sortable="sortableOptions" ng-model="test">
    <tr ng-repeat-start="result in test">
        <td ng-bind="name"></td>
    </tr>

    <tr ng-repeat-end>
        <td ng-bind="name"></td>
    </tr>
</tbody>

And, dragging and dropping those tbody-elements cause the sorting to fail, because the index is wrong.

thgreasi commented 10 years ago

This is not currently in the use cases of the sortable directive. I suppose it would be nice to have as an enhancement. Angular 1.2 changed lots of things for this directive, that have to be fixed first. I would suggest you to use the classic ngRepeat directive (aren't they equivalent?), if thats not a huge pain for you.

Siecje commented 10 years ago

I believe this is the issue I am having.

http://plnkr.co/edit/6XlQLBZwG7UrhINQYwAm?p=preview

In the bottom table I can't actually change the order of the items only add empty rows. I can re-order in the top table.

thgreasi commented 10 years ago

Thanks for providing an example but using ng-repeat-start and ng-repeat-end is not supported and this issue is marked as PR's welcome. README clearly states that:

ui-sortable element should only contain one ng-repeat and not any other elements

RC1245 commented 9 years ago

It would be a very nice enhancement to have. I am assuming this will never happen though ;-(

thgreasi commented 9 years ago

In order for this to work, we should:

In my mind this is a wontfix, since such an implementation would add a lot more code and drastically increase the complexity. I would prefer to just simply wrap my elements with an extra div (or whatever is appropriate).

On Wed, Mar 18, 2015, 19:49 RC1245 notifications@github.com wrote:

It would be a very nice enhancement to have. I am assuming this will never happen though ;-(

— Reply to this email directly or view it on GitHub https://github.com/angular-ui/ui-sortable/issues/64#issuecomment-83088287 .

PhiLhoSoft commented 9 years ago

For the record, I found the issue by trying to use the library on a dl / dt / dd structure:

    <dl ui-sortable ng-model="work.list">
        <dt class="original" ng-repeat-start="item in work.list">{{item.name}}</dt>
        <dd class="translation" ng-repeat-end>{{item.value}}</dd>
    </dl>

I found out it didn't work because ui-sortable moved the dt (for example) and not the dd with it. I understand the limitation, I report this mostly in case somebody is puzzling over the same problem.

jgr3go commented 8 years ago

A workaround, for anyone else having this issue, is to use multiple tbody's, combined with ng-repeat-start if you're already repeating tbody's. In the end the final solution to this for me was:

<table>
    <thead>
       <tr> ...... </tr>
    </thead>
    <tbody ng-repeat-start="item in list">
       <tr> ...... </tr>
    </tbody>
    <tbody ng-repeat-end ui-sortable ng-model="item.sublist">
       <tr ng-repeat="subitem in item.sublist" > ..... </tr>
    </tbody>
</table>

This should work (as far as I can see) in all scenarios that need repeats on tables.

funzeye commented 8 years ago

Hi @jgr3go One thing I noticed using your solution is that if I have, for example, two sublists with two items in each - If I drag an item from position 2 to 1 in the second sublist, the two items in the first sublist also switch around even though I havent touched them. Have you seen this issue also? It's like UI Sortable cannot differenciate between both sublists. Oddly , if I switch an item from position 2 to 1 (or vice versa) in the first sublist the second sublist items dont move.

jgr3go commented 8 years ago

@funzeye you might just need a track by. If you have a unique identifier, best to track by that, but otherwise ng-repeat="item in sublist track by $index" should probably do it.

larbijirari commented 8 years ago

Hello , i faced this problem also, the solution is to put ng-repeat in <tbody... level and put the ui-sortable on <table... level here is an example: http://jsfiddle.net/larox/u4g79t1r/1/

hop it will works, cheers.

sravan50 commented 6 years ago

Hi, i know it is very late reply. but it may help come people. I updated using laroox solution. in this example it is possible to drag drop nested levels table rows. http://jsfiddle.net/u4g79t1r/81/