我有數據的陣列,顯示在一個表中,該表是可編輯的,並希望這些字段是彼此依賴。Angularjs輸入文本值取決於從另一個輸入文本,反之亦然
我如何能做到這一點通過改變細胞的百分比,更改該行,反之亦然量,當你改變的量,也改變百分比是多少?總是驗證不超過百分比總數的100%,以及總額不超過總額。
angular
.module("app", [])
.controller("appController", function ($scope) {
$scope.Data = [
{
Percent: 25.0,
Value: 1000.0
},
{
Percent: 25.0,
Value: 1000.0
},
{
Percent: 25.0,
Value: 1000.0
},
{
Percent: 25.0,
Value: 1000.0
}
];
$scope.TotalAmount = 4000.0;
});
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<link rel="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" type="text/css" />
</head>
<body ng-app="app">
<div class="container" ng-controller="appController">
Total amount: {{TotalAmount}}
<table class="table table-striped">
<tr><th>Percent</th><th>Value</th></tr>
<tr ng-repeat="invoice in Data">
<td><input type="number" ng-model="invoice.Percent" /></td>
<td><input type="number" ng-model="invoice.Value" /></td>
</tr>
</table>
</div>
</body>
</html>
基本上它的怪人!但您可以在輸入框中添加ng-change事件,基於總值或%更新其他行值&%。因爲您需要根據您進行更改的行對其他行進行計算。 更新其他行等價值像((總價值變化行)/(總行數-1)) 或者你可以使隨機數的生成,並確保其與總值和%驗證,它會有很多排列和組合,所以更好地使用以上提到的情況。 –