Requirement :
We need to make a checkbox for every option available in our $scope object and preserve the option selected for further use.
Step 1:
Store your required option list in a $scope.
Example :
$scope.options : SalesOrderNos
In our example the SalesOrderNos have a list of sales order, on which we require the checkbox.
Step2: Declare another scope variable to store the selected options values
$scope.selection = [ ];
Step 3 : use ng-repeat to repeat our sales order list
<tr ng-repeat="s in options">
<td><input type="checkbox" value="{{s.SalesOrderNo}}" ng-checked="selection.indexOf(s.SalesOrderNo) > -1" ng-click="toggleSelection(s.SalesOrderNo)"/></td>
<td>{{s.SalesOrderNo}}</td>
</tr>
Step 4 : On our js page to capture the toggle of checkbox
//toggle selection for a given sales order by salesOrderNo
$scope.toggleSelection = function toggleSelection(SalesOrderNo) {
var idx = $scope.selection.indexOf(SalesOrderNo);
//is currently selected
if (idx > -1) {
$scope.selection.splice(idx, 1);
}
//is newly selected
else {
$scope.selection.push(SalesOrderNo);
}
};
$scope.toggleSelection = function toggleSelection(SalesOrderNo) {
var idx = $scope.selection.indexOf(SalesOrderNo);
//is currently selected
if (idx > -1) {
$scope.selection.splice(idx, 1);
}
//is newly selected
else {
$scope.selection.push(SalesOrderNo);
}
};
Step 5: Now, try console.log($scope.selection)
The selected options must be in the selection array.
No comments:
Post a Comment