我在ng-repeat中有选择列表列。每次重复中都有复选框列。
说我有一个预定义的税值,应该将其更新为我检查过的行的选择列表项。
我该如何实现?在选择列表中使用ng-model更新所有项目,即整列而不是选定的行。
这是代码。
$scope.data = {
products: [
{ name: "Product #1", description: "A product",
category: "Category #1", price: 100, isChecked:false,
taxList: [
{label: "Vat 20%"},
{label: "Vat 30%"},
{label: "Vat 40%"}
]
},
{ name: "Product #2", description: "A product",
category: "Category #1", price: 110, isChecked:false,
taxList: [
{label: "Vat 20%"},
{label: "Vat 30%"},
{label: "Vat 40%"}
]
},
{ name: "Product #3", description: "A product",
category: "Category #2", price: 210, isChecked:false,
taxList: [
{label: "Vat 20%"},
{label: "Vat 30%"},
{label: "Vat 40%"}
]
},
{ name: "Product #4", description: "A product",
category: "Category #3", price: 202, isChecked:false,
taxList: [
{label: "Vat 20%"},
{label: "Vat 30%"},
{label: "Vat 40%"}
]
}]
};
angular.forEach($scope.data.products, function(product){
product.taxList.unshift({label: "Select Tax"});
$scope.selectedTax = product.taxList[0].label;
});
$scope.updateListTax = function(){
$scope.preDefinedTax = "Vat 40%";
angular.forEach($scope.data.products, function(product){
if(product.isChecked){
$scope.selectedTax = $scope.preDefinedTax;
}
});
}
ul,li{
list-style:none;
}
.group:after{
content: '';
display: table;
clear: both;
}
<div class="col-sm-12">
<section>
<div class="group">
<div class="col-sm-6"><strong>Product Details</strong></div>
<div class="col-sm-6"><strong>Tax</strong></div>
</div>
<ul>
<li ng-repeat="item in data.products" class="row">
<div class="col-sm-6">
<span class="col-sm-1">
<input type="checkbox" ng-model="item.isChecked">
</span>
<span class="col-sm-11">{{item.name}}</span>
</div>
<div class="col-sm-6">
<select ng-options="tax.label as tax.label for tax in item.taxList" ng-model="selectedTax"></select>
</div>
</li>
</ul>
<button ng-click="updateListTax()" class="btn btn-default">save</button>
</section>
最佳答案
selectedTax在父范围内,因此每个产品都相同。向每个项目对象添加一个selectedTax键,并使用ng-model。
$scope.data = {
products: [
{ name: "Product #1", description: "A product",
category: "Category #1", price: 100, isChecked:false,
taxList: [
{label: "Vat 20%"},
{label: "Vat 30%"},
{label: "Vat 40%"}
]
},
selectedTax: null
...
angular.forEach($scope.data.products, function(product){
product.taxList.unshift({label: "Select Tax"});
product.selectedTax = product.taxList[0].label;
});
$scope.updateListTax = function(){
$scope.preDefinedTax = "Vat 40%";
angular.forEach($scope.data.products, function(product){
if(product.isChecked){
product.selectedTax = $scope.preDefinedTax;
}
});
}
的HTML
<select ng-options="tax.label as tax.label for tax in item.taxList" ng-model="item.selectedTax"></select>
关于javascript - 使用预定义值更新ng-repeat中的选择列表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37628056/