angularjs - Updating selected array element after deleting the element from a filtered ng-repeat -
i have list of users click on user select , put element object $scope.selectedmember when selected. i'm using ng-repeat search box filter. important thing $scope.selectedmember should populated member.
problem i'm trying overcome: - splicing last user out needs automatically select last user in filtered array, if it's filtered members out search.
html
<div ng-app="myapp" ng-controller="mainctrl"> <input ng-model="search"></input> <div ng-repeat="(key, member) in members | filter:search | orderby :'-name'"> <li ng-class="{active: retactive(key)}" ng-click="selectthis($index)"> name: {{member.name}} key: {{key}} <button ng-click="deletethis(key)">delete</button> </li> </div> selected member name: {{selectedmember.name}} </div>
js
angular.module('myapp', []); function mainctrl($scope) { $scope.members = [ {"name":"a", "viewindex":0}, {"name":"b", "viewindex":1}, {"name":"bb", "viewindex":2}, {"name":"bb", "viewindex":3}, {"name":"c", "viewindex":4}, {"name":"d", "viewindex":5}, {"name":"e", "viewindex":6} ]; $scope.activeindex = 0; $scope.selectedmember = $scope.members[0]; $scope.selectthis = function (index) { $scope.activeindex = index; //put array member new object $scope.selectedmember = $scope.members[index]; } //splice selected member array $scope.deletethis = function (index) { $scope.members.splice(index, 1); $scope.selectthis(index); } //angular copy , push member array $scope.duplicatethis = function (index) { } // change class if member active $scope.retactive = function (index) { return $scope.activeindex == index; } }
css
.active { color:blue; }
one problem see passing $index selectthis($index). when filter data, loop's $index no longer represents actual index of item in array - should pass selectthis key, not $index.
Comments
Post a Comment