[英]How to get values from selected table row?
当用户使用复选框选项选择一行,然后单击“计算折旧”时,我要获取{{asset.purchaseValue}},{{asset.residualValue}},{{asset.purchaseDate}}和{{ asset.ddate}}执行计算以发送回给用户。 如何在jQuery,AngularJS或Vanilla Javascript中获得这些值?
<table align="center" class="assetlist">
<thead>
<tr>
<th width="45px">
<select ng-model="sortBy" class="localSearch">
<option value="title">Title</option>
<option value="ddate">Disposal Date</option>
<option value="date">Purchase Date</option>
<option value="residualValue">Residual Value</option>
<option value="purchaseValue">Purchase Value</option>
</select>
</th>
<th class="thead" ng-hide="hide"><input type="text" class="localSearch" placeholder="Title" ng-model="searchString.title" /></th>
<th class="thead" ng-hide="hide"><input type="text" class="localSearch" placeholder="Purchase Date" ng-model="searchString.date" /></th>
<th class="thead" ng-hide="hide"><input type="text" class="localSearch" placeholder="Purchase Value" ng-model="searchString.purchaseValue" /></th>
<th class="thead" ng-hide="hide"><input type="text" class="localSearch" placeholder="Disposal Date" ng-model="searchString.ddate" /></th>
<th class="thead" ng-hide="hide"><input type="text" class="localSearch" placeholder="Residual Value" ng-model="searchString.residualValue" /></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="asset in assets | orderBy:sortBy | filter:searchString : searchStrict | limitTo: rowLimit">
<td><input type="checkbox" ng-model="checked"/></td>
<td>{{asset.title}}</td>
<td>{{asset.date | date: "MMMM d, y"}}</td>
<td>{{asset.purchaseValue | currency:"£"}}</td>
<td>{{asset.ddate | date: "MMMM d, y"}}</td>
<td>{{asset.residualValue | currency:"£"}}</td>
</tr>
</tbody>
</table>
<button class="formbtn" ng-disabled="!checked">Calculate Depreciation</button>
当用户选中该框时,将行传递给:
HTML:
<input type="checkbox" ng-model="checked" ng-checked="evaluate(asset)"/>
JavaScript:
$scope.evaluate = function(asset) {
//var purchaseValue = asset.purchaseValue
//var residualValue = asset.residualValue
//var purchaseDate = asset.purchaseDate
//var ddate = asset.ddate
//do calculations
}
这是您想要的输出的修改后的代码:(复制并尝试!)
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
var purchaseValue,residualValue,life=1;
$(".ckb").on('click',function(){
var checked=$(this).is(':checked');
if(checked){
//checkbox checked
var tr=$(this).parent().parent().parent();
var titlevalue=tr.find('td:eq(1)').html();
purchaseValue=tr.find('td:eq(3)').html();
residualValue=tr.find('td:eq(5)').html();
}else{
//checkbox unchecked
}
});
$("#calculateId").on('click',function(){
if(residualValue==undefined&&residualValue==undefined){
alert('please select the checkbox');
}else{
// alert(purchaseValue);
//alert(residualValue);
var result=((parseInt(purchaseValue)-parseInt(residualValue)) /life);
alert('result:'+result);
}
});
});
</script>
</head>
<body>
<table align="center" class="assetlist">
<thead>
<tr>
<th width="45px">
<select ng-model="sortBy" class="localSearch">
<option value="title"> Title</option>
<option value="ddate"> Disposal Date</option>
<option value="date"> Purchase Date</option>
<option value="residualValue">residualValue</option>
<option value="purchaseValue">purchaseValue</option>
</select>
</th>
<th class="thead" ng-hide="hide"><input type="text" class="localSearch" placeholder="Title" ng-model="searchString.title" /></th>
<th class="thead" ng-hide="hide"><input type="text" class="localSearch" placeholder="Purchase Date" ng-model="searchString.date" /></th>
<th class="thead" ng-hide="hide"><input type="text" class="localSearch" placeholder="Purchase Value" ng-model="searchString.purchaseValue" /></th>
<th class="thead" ng-hide="hide"><input type="text" class="localSearch" placeholder="Disposal Date" ng-model="searchString.ddate" /></th>
<th class="thead" ng-hide="hide"><input type="text" class="localSearch" placeholder="Residual Value" ng-model="searchString.residualValue" /></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="asset in assets | orderBy:sortBy | filter:searchString : searchStrict | limitTo: rowLimit">
<td><input type="checkbox" class="ckb" ng-model="checked"/></td>
<td>title1</td>
<td>date1</td>
<td>20</td>
<td>ddate1</td>
<td>10</td>
</tr>
</tbody>
</table>
</form>
<button class="formbtn" ng-disabled="!checked" id="calculateId">Calculate Depreciation</button>
</body>
</html>
由于存在多个资产,因此应在复选框上单击<td>
中的ng-model="asset.selected"
,将新selected
属性绑定到该资产的true(或切换)。
<tr ng-repeat="asset in assets | orderBy:sortBy | filter:searchString : searchStrict | limitTo: rowLimit">
<td><input type="checkbox" ng-model="asset.selected" /></td>
<td>{{asset.title}}</td>
<td>{{asset.date | date: "MMMM d, y"}}</td>
<td>{{asset.purchaseValue | currency:"£"}}</td>
<td>{{asset.ddate | date: "MMMM d, y"}}</td>
<td>{{asset.residualValue | currency:"£"}}</td>
</tr>
<button class="formbtn" ng-click="calculateDep()">Calculate Depreciation</button>
在控制器中
//For Calculation
$scope.calculateDep = function() {
angular.forEach($scope.assets, function(asset) {
if (asset.selected) {
//Here are asset.purchaseValue,asset.residualValue ...
//perform calculation individually for an asset
//or send the enitire selected assets for calculation by creating a new scope array for selected ones
}
})
}
更新:
请参见下面的代码段,当您选中一个或多个复选框并计算dep时,您可以在控制台中看到整个对象。
angular.module("app", []).controller("ctrl", function($scope) { $scope.assets = [{ title: 'Asset1', date: '', purchaseValue: 500, ddate: '', residualValue: 100 }, { title: 'Asset2', date: '', purchaseValue: 500, ddate: '', residualValue: 100 }]; //For Calculation $scope.calculateDep = function() { console.clear(); angular.forEach($scope.assets, function(asset) { if (asset.selected) { console.log(asset); } }) } });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script> <body ng-app="app" ng-controller="ctrl"> <table> <tr ng-repeat="asset in assets track by $index"> <td><input type="checkbox" ng-model="asset.selected" /></td> <td>{{asset.title}}</td> <td>{{asset.date}}</td> <td>{{asset.purchaseValue}}</td> <td>{{asset.ddate}}</td> <td>{{asset.residualValue}}</td> </tr> </table> <button class="formbtn" ng-click="calculateDep()">Calculate Depreciation</button> </body>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.