javascript - AngularJs: uncheck checkbox after item is removed -
after spending lot of time on simple issue , having made lot of research, wondering if give me help.
i have data generated inside of table so:
<tbody> <tr class="odd gradex" ng-repeat="user in ctrl.datas | orderby:ctrl.sorttype:ctrl.sorttypereverse"> <td> <input type="checkbox" class="checkboxes" value="{{user.id}}" ng-click="ctrl.additem(user)"/> </td> <td> {{user.given_name}} </td> <td> {{user.family_name}} </td> <td> <a href="mailto:{{user.emai}}"> {{user.email}}</a> </td> <td class="center" ng-bind-html="ctrl.converttodate(user.created_at) | date: 'dd-mmmm-yyyy hh:mm'"></td> <td> <span class="btn blue-hoki"> details </span> </td> </tr> </tbody>
above container items selected via checkbox, add in array , give user ability delete selected item:
<tr ng-repeat="user in ctrl.checkedobject track $index" ng-show="user.id"> <td>{{user.family_name}}</td> <td>{{user.given_name}}</td> <td> <button class="btn blue" ng-click="ctrl.removeitem($index)">unselect</button> </td> </tr>
in controller, here 2 functions used so:
this.checkedobject = []; //add selected user this.additem = function (user) { self.checkedobject.push(user); }; this.removeitem = function(obj){ delete self.checkedobject[obj]; };
what i'd achieve uncheck corresponding checkbox if user changes selection. thing is, have no idea how target corresponding checkbox. have clue? in advance
i set simple plunker show 1 approach, assign selected
property each user when his/her checkbox checked, , set ng-checked
attribute on checkbox corresponding user.selected
(so unchecked when false).
using approach won't need push , delete array of checkedusers
, can filter users whether selected or not.
function getselected() { ctrl.checkedobject = _.filter(ctrl.datas, {selected: true}); } ctrl.selectuser = function (user) { user.selected = true; getselected(); }; ctrl.removeuser = function(user){ user.selected = false; getselected(); };
Comments
Post a Comment