Two way binding with table rows
by TheFiddler
HTML
<div ng-app="TableApp">
<form>
<div ng-controller="ViewCtrl">
{{people}}
<div>
<h3>Update status for checked to</h3>
<select ng-options="item.value as item.displayName for item in StatusDropDown" ng-model="person.status" ng-change="updateSelected()"></select>
<button ng-click="save()">Save Data</button>
</div>
<hr>
<h4>Status greater than or equal 1 </h4>
<table>
<thead>
<tr>
<th></th>
<th>ID</th>
<th>Status</th>
<th></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="person in people" ng-if="person.status > 0">
<td>
<input type="checkbox" ng-model="person.selected">
</td>
<td>{{person.personid}}</td>
<td>{{person.status}}</td>
<td>
<select ng-options="item.value as item.displayName for item in StatusDropDown" ng-model="person.status" ng-change="update(person.status, person)"></select>
</td>
</tr>
</tbody>
</table>
<h4>Status less than 1 </h4>
<table>
<thead>
<tr>
<th></th>
<th>ID</th>
<th>Status</th>
<th></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="person in people" ng-if="person.status <1">
<td>
<input type="checkbox" ng-model="person.selected">
</td>
...
CSS
table {
border: 1px solid #333;
}
td {
text-align: center;
border: 1px solid #e4e4e4;
}
JavaScript
var tableapp = angular.module('TableApp', []);
tableapp.factory('PeopleList', function ($http) {
var cachedData;
function getData(callback) {
if (cachedData) {
callback(cachedData);
} else {
var jsondata = [{
"personid": 1234,
"status": -1
}, {
"personid": 4321,
"status": 0
}, {
"personid": 5555,
"status": 1
}];
callback(jsondata);
/*
$http.get('pathtojson/data.json').success(function(data){
cachedData = data;
callback(data);
});*/
}
}
return {
list: getData,
findByName: function (name, callback) {
getData(function (data) {
var person = data.filter(function (entry) {
return entry.name === name;
})[0];
callback(person);
});
},
findById: function (personid, callback) {
getData(function (data) {
var person = data.filter(function (entry) {
return entry.personid === personid;
})[0];
callback(person);
});
}
};
});
tableapp.controller('ViewCtrl', function ($scope, PeopleList, $filter) {
PeopleList.list(function (people) {
$scope.people = people;
//how to set selected status
});
$scope.update = function (statusarg, personarg) {
//how to find the selected person and update the tables
//$scope.people.push({ ??? });
}
$scope.updateSelected = function (statusarg) {
//how to set status for selected and update tables
}
//how to select value in the dropdown lists
$scope.StatusDropDown = [{
value: '',
displayName: 'Change Status...'
}, {
value: -1,
...