Reputation: 87
I am trying to extract the ID from an object which is at 0 in the index in the object. I need to then convert this key to a string to be able to pass it into an api endpoint.
My Controller event:
$scope.attach = function () {
var rules_id = $scope.rules.selected;
if (rules_id) {
var l = rules_id.length;
for (var i = 0, j = l; i < j;)
{
var key_value = rules_id[i];
}
}
console.log($scope.rules);
console.log(key_value);
console.log($scope.rules.selected);
console.log($scope.asset.id);
};
At present, I am just trying to define each variable in the console, the only thing returning undefined is the loop variable "key_value".
I have checked this against a number of examples and standard setups for a loop and this isn't working.
$scope.rules.selected is a checkbox result which key is represented by a rule ID. See below:
<form name="rules" method="post">
<table class="table table-striped table-hover" ng-model="associated_rules">
<thead>
<th>Rule ID:</th>
<th>Rule Types:</th>
<th>Description:</th>
<th>Start Time:</th>
<th>End Time:</th>
<th>Apply Rule to Vehicle:</th>
</thead>
<tr ng-repeat="associated_rule in associated_rules">
<td>@{{ associated_rule.id }}</td>
<td>@{{ associated_rule.resource_ids.accounts }}</td>
<td>@{{ associated_rule.description }}</td>
<td>@{{ associated_rule.start_time }}</td>
<td>@{{ associated_rule.end_time }}</td>
<td><input type="checkbox" ng-model="rules.selected[associated_rule.id]"aria-label="associated_rule.id" ng-true-value="true"ng-false-value="false" value="rules.selected"></td></tr>
</table>
<button class="btn btn-primary" ng-click="attach()"><i class="fa fa-paperclip"></i> Attach</button>
</form>
Upvotes: 1
Views: 78
Reputation: 3622
On this approach we take some well-formed input and do some transformations so we can do a better use of it
// creates the module
angular.module("samplemodule",[])
// apppend the controller to the module
angular.module("samplemodule").controller("SampleCtl",function(){
// log data from somewhere
this.originalData = [
"{db77370c-fbfe-11e5-8468-ed8c21fd14a1: true, c9f2c76c-fbfe-11e5-8fe5-70c120bc96b5: true}",
"{db77370c-fbfe-11e5-8468-ed8c21fd14a2: false, c9f2c76c-fbfe-11e5-8fe5-70c120bc96b6: true}",
"{db77370c-fbfe-11e5-8468-ed8c21fd14a3: true, c9f2c76c-fbfe-11e5-8fe5-70c120bc96b7: true}",
"{db77370c-fbfe-11e5-8468-ed8c21fd14a4: true, c9f2c76c-fbfe-11e5-8fe5-70c120bc96b8: false}",
]
this.values = this.originalData.map(function(e){
e = e.replace(/{/g,"{\"").replace(/:/g,"\":").replace(/, /g,", \"")
e = JSON.parse(e)
var kv = []
for(var attr in e)
kv.push({key:attr,value:e[attr]})
return kv
})
this.attach = function attach(){
var selectedones = [].concat.apply([],this.values).filter(function(e){
return e.value
})
alert(selectedones)
}
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="samplemodule" ng-controller="SampleCtl as ctl">
<table>
<tbody>
<tr ng-repeat="vl in ctl.values">
<td ng-repeat="kv in vl">
{{kv.key}}
<input type="checkbox" ng-model="kv.value"/>
</td>
</tr>
</tbody>
</table>
<button ng-click="ctl.attach()">The selected ones</button>
</div>
Upvotes: 0
Reputation: 3622
What you want maybe can archived if you rethink in a more declarative way:
// creates the module
angular.module("samplemodule",[])
// apppend the controller to the module
angular.module("samplemodule").controller("SampleCtl",function(){
this.values = [// sample data
{a:"XXX",b:0,c:new Date(),d:true },
{a:"XXY",b:1,c:new Date(),d:false},
{a:"XYX",b:2,c:new Date(),d:false},
{a:"XYY",b:3,c:new Date(),d:true },
{a:"YXX",b:4,c:new Date(),d:false},
{a:"YXY",b:5,c:new Date(),d:false}
]
this.attach = function attach(){
var selectedones = this.values.filter(function(e){
return e.d // just the selected ones
}).map(function(e){
return e.b // just the ids or anything you want
}).join(",")
alert(selectedones)
}
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="samplemodule" ng-controller="SampleCtl as ctl">
<table>
<thead>
<tr>
<th>A</th>
<th>B</th>
<th>C</th>
<th></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="vl in ctl.values">
<td>{{vl.a}}</td>
<td>{{vl.b}}</td>
<td>{{vl.c}}</td>
<td><input type="checkbox" ng-model="vl.d"/></td>
</tr>
</tbody>
</table>
<button ng-click="ctl.attach()">The selected ones</button>
</div>
Upvotes: 0
Reputation: 5167
Your for loop is wrong, also you are trying to console log a variable that is declared inside the for loop, it will always be undefined! This is the correct for loop example:
for (i = 0; i < rules_id.length; i++) {
console.log(rules_id[i]);
}
Please have a read on how for loop works
But the real problem here is with your Angular Code
Upvotes: 2