SrinivasAppQube
SrinivasAppQube

Reputation: 301

how to push selected checkbox data into array using angularjs?

I have a tables array consist of two objects. Each object having the tablename. I use ng-repeat for the checkbox. When I select the checkbox selected object will push into array [tablelist] and the array will use for drodownlist.

$scope.tables=[{"tabelname":"t1},{{tabelname:t2}}]
$scope.tablelist=[]

this is my html code

<div ng-repeat="table in tables">
    <span class="col-sm-1">
    <input type="checkbox" ng-model="table.tablename" class="form-group">{{table.tablename}}</span>
</div>

<select ng-options="table.tablename as table.tablename for table in  tablelist></select>

Upvotes: 1

Views: 1448

Answers (2)

vipin Bhandari
vipin Bhandari

Reputation: 192

<div ng-repeat="table in tables">
<span class="col-sm-1">
<input type="checkbox" ng-click="saveData(table.tablename)" ng-model="table.tablename" class="form-group">{{table.tablename}}</span></div><select ng-options="table.tablename as table.tablename for table in  tablelist></select>

Simple pass Your Table name to the saveData func. then push this name to array

$scope.saveData = function(tbaleName) {$scope.tablelist.push(tableName)}

Make Sure that there is no existing value

Upvotes: 0

Nikhilesh K V
Nikhilesh K V

Reputation: 1480

<div ng-repeat="table in tables">
    <span class="col-sm-1">
    <input type="checkbox" ng-click="saveData($index)" ng-model="table.tablename" class="form-group">{{table.tablename}}</span>
</div>

<select ng-options="table.tablename as table.tablename for table in  tablelist></select>

In your controller:


$scope.saveData = function(index) {
if(table.tablename)
    $scope.tablelist[index] = $scope.tables[index];
};

Upvotes: 1

Related Questions