【发布时间】:2014-02-13 10:10:38
【问题描述】:
我有一个带有 HTML 表格的 Angular js html 页面。这有大约 100 行。用户选择 10 或 15 行并进行后端调用。后端调用是使用 $.ajax({...}) 进行处理和更新数据库。处理后,后端返回 2 或 3 条记录返回屏幕。这些返回的对象将具有新的状态和一些新的值。所以我需要将这些状态同步回我范围内的同一个对象,以便它们反映在屏幕上。
我知道我们可以遍历每个对象并更新状态。但由于数据量太大(有时甚至 1000 行),我想知道 Angular 中是否有任何现成的功能可以完成这项工作。如果 Angular 中没有这样的功能,请推荐任何其他可以为我提供帮助的免费开源工具。
我在这里附上代码 sn-p。只是出于保密的原因,我将我的问题转换为客户帐户场景
<table >
<thead >
<tr>
<th >Customer Name</th>
<th >Account Number</th>
<th >Status </th>
<th >Date</th>
</tr>
</thead>
<tbody ng-repeat="customer in customerList">
<tr ng-repeat="account in customer.accounts" >
<td>{{customer.name}}</td>
<td>{{account.acctNum}}</td>
<td>
<input type="checkbox" ng-model="account.acctId" ng-change="selectThisAccount(account)" /> {{account.status}}
</td>
<td>{{account.activationTimestamp}}</td>
</tr>
</tbody>
</table>
<input type="button" value="Activate Accounts" ng-click="activateAccounts()" />
Controller
===========
$scope.customerList = ....
// I have my customer list retrieved by way of another call and the data is assigned to the scope.customerList variable
$scope.selectedAccounts = [];
$scope.selectThisAccount = function(account) {
$scope.selectedAccounts.push(account);
};
$scope.activateAccounts = function() {
$scope.successfullyActivatedAccounts = [];
$scope.successfullyActivatedAccounts = AccountService.activateAccounts($scope.selectedAccounts);
}
AccountService.js
======================
this.activateAccounts = function(accounts)
{
var accountRequest ={};
accountRequest.accountList = accounts;
return $.ajax({
type : 'POST',
url: 'activateAccounts',
data:JSON.stringify(accountRequest),
dataType: "json",
contentType: "application/json",
success: function(accountresponse){
return accountresponse.accountList;
},
error : function(xhr, ajaxOptions, thrownError) {
hideReportMessage();
alert( "Error: " + xhr.responseText + "\n" + thrownError );
}
});
},
为了解释这一点,一个客户可以有多个帐户,每个帐户可以处于不同的状态。最终用户将看到客户帐户列表,选择其中的列表并尝试激活帐户。只有那些被激活的账户对象才会被服务器返回。我需要更新 account.status 的值来识别正确的对象,而无需遍历整个列表。
请帮忙。
【问题讨论】:
-
后端restful服务发送JSON对象。
-
我无法控制将服务调用从 $.ajax 更改为 $http。这是一个可重用的服务调用,运行良好,我只需要使用它。后端是 spring 控制器,它发送转换为 JSON 的 java 对象列表,表中的每一行都是一个对象。如果您认为我没有正确理解您的问题“这些是什么类型的对象”,请详细说明。
-
抱歉,如果您展示一些控制器和标记,我们或许可以提供帮助
-
@calebboyd,我已经用代码 sn-p 更新了我的问题,更详细地解释了这个问题。请帮忙。谢谢。
-
Angular 没有说明数据的大小。如果您使用 ng-repeat 超过 1000 行,则可能是性能问题。还有一点是你应该用 $http 调用替换 $.ajax 并使用 Angular Promise
标签: html angularjs html-table sync angularjs-scope