顧客のリストを提供しているように見え、1 つ以上が選択されると、選択された顧客と同じ都市にいる顧客の表が表示されます。
これを行うには、カスタム フィルターが必要です。
// Define our filter
app.filter('selectedCustomerCities', function($filter) {
return function(customers) {
var i, len;
// get customers that have been checked
var checkedCustomers = $filter('filter')(customers, {checked: true});
// Add in a check to see if any customers were selected. If none, return
// them all without filters
if(checkedCustomers.length == 0) {
return customers;
}
// get all the unique cities that come from these checked customers
var cities = {};
for(i = 0, len = checkedCustomers.length; i < len; ++i) {
// if this checked customers cities isn't already in the cities object
// add it
if(!cities.hasOwnProperty(checkedCustomers[i].city)) {
cities[checkedCustomers[i].city] = true;
}
}
// Now that we have the cities that come from the checked customers, we can
//get all customers from those cities and return them
var ret = [];
for(i = 0, len = customers.length; i < len; ++i) {
// If this customer's city exists in the cities object, add it to the
// return array
if(cities[customers[i].city]) {
ret.push(customers[i]);
}
}
// we have our result!
return ret;
};
});
マークアップは次のように変更されます。
<div data-ng-repeat="customer in customers">
<!-- record that this customer has been selected -->
<input type="checkbox" ng-checked="customer.checked" ng-model="customer.checked" /> {{ customer.city }}
</div>
<table>
<!-- table heading goes here -->
<tbody>
<!-- use our custom filter to only display customers from the cities selected -->
<tr data-ng-repeat="customer in customers | selectedCustomerCities">
<td>{{ customer.firstName }}</td>
<td>{{ customer.lastName }}</td>
<td>{{ customer.address }}</td>
<td>{{ customer.city }}</td>
</tr>
</tbody>
</table>
この Plunker で動作しているのを見ることができます: http://plnkr.co/edit/GlHRLKECR4jeBS7Vf7TX?p=preview