javascript - How to make a selectAll checkbox and triggering every function of ng-click on every checkbox? -
i have code when click checkbox, perform ng-click. here js of it.
$scope.selecttitle = function(evt, selected){ evt.stoppropagation(); var filtered = _.findwhere($scope.selectedtitles, {id: selected.id}); var index = _.indexof($scope.selectedtitles, selected); if(selected === filtered){ $scope.selectedtitles.splice(index, 1) } else{ $scope.selectedtitles.push(selected); } console.log('titles', $scope.selectedtitles, 'filtered', filtered, 'index', index); };
it in table ng-repeat ng-click code used .stoppropagation()
prevent activating of ng-click function of table.
now need made select checkbox. here code.
$scope.selectall = function (filteredtitles) { if ($scope.selectedall) { $scope.selectedall = false; } else { $scope.selectedall = true; } _.foreach(filteredtitles, function(cpportfolioitem) { cpportfolioitem.selected = $scope.selectedall; if(cpportfolioitem.selected){ $scope.selecttitle(); } });
when run it. there error saying typeerror: cannot read property 'stoppropagation' of undefined
.
i can't remove stoppropagation because prevents said earlier. can give me suggestions on how can select checkbox , call ng-click function of each checkbox? in advance.
a few thoughts.
why add scope selectedall? should private var inside controller.
var selectedall = false; $scope.selectall = function(){ selectedall = !selectedall; // quick toggle. _.foreach(filteredtitles, function(title){ title.isselected = selectedall; } }
then checkbox should hook right title.isselected state. rather easy change individually or selectall.
see: https://docs.angularjs.org/api/ng/directive/ngchecked
<input ng-checked="title.isselected" .../>
where 'title' ng-repeat data object.
also i'd suggest using directive inside ng-repeat.
sample directive:
angular.module('appname') .directive('portfolioitem', function() { return{ restrict:'e', // element style replace:true, templateurl:'portfolioitem.view.html', scope:{ data:'=' // binds attribute scope } // can add controller here well. }; });
then create script ng-template "portfolioitem.view.html"
<script ng-template="portfolioitem.view.html"> <section class="item"> {{data}} <input ng-checked="data.isselected" ... /> </section> </script>
https://docs.angularjs.org/api/ng/directive/script
if out weee bit more. think select item function should changed. push data factory, can backbone across controllers. do, reduces watchers , increases ability work data.
angular.module('appname') .factory('datamanager', datamanager); function datamanager($log, $timeout, itemmodel) { var mngr, config = getconfig(); // default values $log.debug('datamanager init'); mngr = { currentsearchterm: null, items: [], abort: abort, getdata: getdata, // function call data. getmoreresults: getmoreresults }; function getdata(){ dataservice.getdata().then(function(response){ // ...parse data, etc...loop , : mngr.items.push(parseddataitem); }; } return mngr; }
then controller repeat off datamanager.items (or filter underscore or angular). make sense?
Comments
Post a Comment