Niketan Raval
Niketan Raval

Reputation: 479

ngRepeat filter, Checkbox select parent child vise vesa

Json

$scope.tmp = {
    'ax':{
       label:'AX',
       checked:false
    },
    'by':{
       label:'BY', 
       linkTo:'ax',
       checked:false     
    },
    'xy':{
       label:'BY', 
       linkTo:'by',
       checked:false     
    }
}

I need to check parent if any of its child is checked and if parent is selected then child is also selected with the help of linkTo attribute.

I have tried below code

<div ng-repeat='(k,v) in tmp'>
    <input type="checkbox" ng-model="tmp[k].checked" ng-change="v.linkTo != undefined ? tmp[k].checked=tmp[v.linkeTo].checked:{tmp[k].checked = (tmp | filter : {linkTo:k}).checked }">Linked With {{obj.label}} 
</div>

Upvotes: 1

Views: 168

Answers (1)

Viplock
Viplock

Reputation: 3319

You should create a function for it , to handle change activity. Try Using this -

   <div ng-app ng-controller="TController">
    <div ng-repeat='(k,v) in tmp'>
    <input type="checkbox" ng-model="tmp[k].checked" ng-change="change(v.linkTo,k)">{{k}} Linked With {{v.linkTo}} <br />
    </div>
  </div>

And method like this

$scope.change=function(linkto,k){
  if(linkto!=undefined){
  $scope.tmp[linkto].checked=$scope.tmp[k].checked;
}
else{
       angular.forEach($scope.tmp, function (value, key) {

            if (value.linkTo === k) {
                value.checked=$scope.tmp[k].checked;
            }
          })

        }

    }

Try this Js Fiddel

Upvotes: 1

Related Questions