gpt4 book ai didi

javascript - 如何将记录传递给 Angular 中的另一个表?

转载 作者:行者123 更新时间:2023-11-30 20:14:05 25 4
gpt4 key购买 nike

当我选中第一个表中的复选框时,整行应显示在第二个表中。在这里,我使用硬编码来完成此操作,但我需要以动态方式执行此操作。

var app = angular.module('myApp',[]);
app.controller("homeCtrl", function($scope) {
$scope.items = [{
itemID: 'BR063',
itemValue: '145154'
}, {
itemID: 'BR053',
itemValue: '145154'
}];
$scope.selectedItems = [];

$scope.addRec = function(result, i){
if(result == true){

$scope.selectedItems.push($scope.items[i]);
}
else{
$scope.selectedItems.pop();
}
}
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular-route.js"></script>
<div ng-app = 'myApp' ng-controller="homeCtrl">
<h1>Select Rows</h1>
<table style="width:50%" class="table-responsive table-bordered ">
<tr>
<th class="text-center">Index</th>
<th class="text-center">Item ID</th>
<th class="text-center">Item Values</th>
<th class="text-center">Select</th>
</tr>
<tr ng-repeat="x in items">
<td class="text-center">{{$index+1}}</td>
<td class="text-center">{{x.itemID}}</td>
<td class="text-center">{{x.itemValue}}</td>
<td class="text-center"><input type="checkbox" ng-model="itsVal" ng-change = "addRec(itsVal, $index+1)";/></td>
</tr>
</table>

<h1>Selected Rows</h1>
<table style="width:50%" class="table-responsive table-bordered ">
<tr>
<th class="text-center">Index</th>
<th class="text-center">Item ID</th>
<th class="text-center">Item Values</th>
<th class="text-center">Select</th>
</tr>
<tr ng-repeat="x in selectedItems">
<td class="text-center">{{$index+1}}</td>
<td class="text-center">{{x.itemID}}</td>
<td class="text-center">{{x.itemValue}}</td>
<td class="text-center"><input type="checkbox" /></td>
</tr>
</table>
<div>

最佳答案

将addRec函数改为

$scope.addRec = function(result, i){
if(result == true){

$scope.selectedItems.push($scope.items[i]);
}
else{
$scope.selectedItems.pop();
}
}

关于javascript - 如何将记录传递给 Angular 中的另一个表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52093936/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com