Skip to content
This repository has been archived by the owner on Oct 2, 2019. It is now read-only.

sortable="true" does not update the ng-model #974

Closed
stevematyas opened this issue May 29, 2015 · 8 comments
Closed

sortable="true" does not update the ng-model #974

stevematyas opened this issue May 29, 2015 · 8 comments

Comments

@stevematyas
Copy link

Reference: #209

After drag n' drop of options selected (re-ordering), the selected items (<ui-selelct ... ng-model="mycontrollerscopeattr".. >) is not updated.

Try it out here: http://plnkr.co/edit/eiwaOrkK4V9ZGwfGE5IV?p=preview

If this is not a bug (and developer error) then, can you make the example work? I'll gladly update any documentation.

@homerjam
Copy link
Contributor

Looks like it needs a $scope.$apply on the drop event, or rather $scope.$parent.$apply(). As you can see if your change the order then select a new person the changes are reflected in the view.

You could trigger this using the event uiSelectSort:change for now.

// in your controller for example

$scope.on('uiSelectSort:change', function(event, args) {
    if (!$scope.$$phase) {
        $scope.$apply();
    }
});

@stevematyas
Copy link
Author

Thanks @homerjam for responding. Unfortunately, I'm too new to both Angular and ui-select to know exactly what you mean though at a high-level it is as though you want to copy/assign $select.ngModel.$viewValue to $select.ngModel.$modelValue or $select.selected though not exactly sure. I just don't know enough of both frameworks / libs to know for sure how to get the underlying ngModel updated properly.

        move.apply(theList, [droppedItemIndex, newIndex]);

        scope.$apply(function() {
          $select.selected = $select.ngModel.$viewValue;  // ??? 
          scope.$emit('uiSelectSort:change', {
            array: theList,
            item: itemToMove,
            from: droppedItemIndex,
            to: newIndex
          });
        });

@homerjam
Copy link
Contributor

The model is updated, but the view isn't. The view is updated on a digest cycle. The digest cycle is what needs to be triggered in ui-select (but currently isn't). You can workaround this with my suggested fix - which might be better than trying to guess at a fix to a lib/framework you don't know very well yet.

@stevematyas
Copy link
Author

@homerjam : Thanks for your guidance. I'll do my best to apply what you've offered thus far.

Updated Plunker with @homerjam's edits http://plnkr.co/edit/Oxk6DnZbCdM0cZHXAg8i?p=info and sadly the below doesn't work :(.

Yes, selecting a new item after re-ordering does cause ng-model to reflect the "new selected order" as well as the new item. The issue is that after a drag n' drop of options selected (re-ordering), the selected items (<ui-selelct ... ng-model="mycontrollerscopeattr".. >) is not updated.

<p>selected items (order isn't correct): {{mycontrollerscopeattr}}</p>

app.controller('DemoCtrl', function($scope, $http, $timeout) {
  //added listener per @homerjam per https://github.com/angular-ui/ui-select/issues/974
  $scope.$on('uiSelectSort:change', function(event, args) {
    if (!$scope.$$phase) { //http://stackoverflow.com/questions/20263118/what-is-phase-in-angularjs
        $scope.$apply();
    }
  });

@homerjam
Copy link
Contributor

Quick fix:


  //added listener per @homerjam per https://github.com/angular-ui/ui-select/issues/974
  $scope.$on('uiSelectSort:change', function(event, args) {
    console.log('uiSelectSort:change', args);
    $scope.multipleDemo.selectedPeople = args.array;
    if (!$scope.$$phase) { //http://stackoverflow.com/questions/20263118/what-is-phase-in-angularjs
        $scope.$apply();
    }
  });

@stevematyas
Copy link
Author

@homerjam : Bingo! That worked. (bow)

homerjam added a commit to homerjam/ui-select that referenced this issue Jun 23, 2015
@faisalferoz
Copy link

@homerjam fix works. +1

pgrm added a commit to grmanit/ui-select that referenced this issue Aug 18, 2015
@pgrm
Copy link

pgrm commented Aug 18, 2015

For those, who are looking for a bower-solution until this fix will be merged, you can set this in your bower.json file:

"angular-ui-select": "grmanit/ui-select#master"

pgrm added a commit to grmanit/ui-select that referenced this issue Feb 7, 2016
pgrm added a commit to grmanit/ui-select that referenced this issue Feb 7, 2016
homerjam pushed a commit to homerjam/ui-select that referenced this issue Mar 27, 2016
theanxy pushed a commit to theanxy/ui-select that referenced this issue Apr 6, 2016
homerjam added a commit to homerjam/ui-select that referenced this issue Apr 26, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants