我正在使用我的应用程序的angularJS ontop。AngularJS列表的动态总和
我有一个控制器的一个基本的例子:}
function OrderListCtrl($scope, $http) {
$http.get('/static/angular/app/phones/van1.json').success(function(data) {
$scope.van1 = data;
});
$http.get('/static/angular/app/phones/van2.json').success(function(data) {
$scope.van2 = data;
});
和样品JSON入门
{
"id": "3",
"custName": "Mrs Smith",
"accountNumber": "416",
"orderNumber": "12348",
"orderWeight": "120.20"
},
我的HTML看起来像这样:
<div id=1>
<h1>Van 1 - Total Weight = XXX </h1>
<ul class="sortdrag">
<li ng-repeat="van1 in van1" id="[[ van1.id ]]">
[[van1.custName]] [[van1.accountNumber]] [[van1.orderWeight]]
</li>
</ul>
</div>
现在,我想要得到ul中每个li项目的总重量。
这很容易,如果静态列表,但列表使用jQuery-ui和我有多个列表里的项目被拖放到每个列表之间。我的问题是,我怎么能够将XXX动态更新为ul中每个li中所有权重的值,或者更多的问题甚至可以完成?
我真的不想使用onDrop事件,因为这在预填充列表中不起作用,所以理想情况下我想使用从ul中的所有van1.orderWeight值中获取其值的代码。
任何建议最好的方法来处理这将非常感激!之前有人问我使用[[和]]而不是{{和}},因为我正在使用jinja2模板。
UPDATE:
好的阅读修改了原来的控制器下面的答案后:
function OrderListCtrl($scope, $http) {
$http.get('/static/angular/app/phones/van1.json').success(function(data) {
$scope.van1 = data;
// This part is easy, calcuate the sum of all weights from the JSON data
$scope.sumV1 = _.reduce(_.pluck($scope.van1, 'orderWeight'), function (m, w) {return m + w}, 0);
});
$scope.getVan1Weight = function(){
// here I try and write a function to calculate the dynamic weight
// of a van, which will update when items get dropped in/out of the ul
_.reduce(_.pluck($scope.van1, 'orderWeight'), function (m, w) {return m + w}, 0);
}
而且我的模板
<div id="app" ng-controller="OrderListCtrl">
<div id=1>
<h1>Van 1 - Total Weight = [[getVan1Weight()]]
Starting Weight - [[sumV1]]</h1>
<ul class="sortdrag">
<li ng-repeat="van1 in van1" id="[[ van1.id ]]">
[[van1.custName]] [[van1.accountNumber]] [[van1.orderWeight]]
</li>
</ul>
</div>
现在使用underscorejs即时libary来帮助执行计算,但我似乎只能得到这个使用初始数据,而不是更新时,从另一个新的订单被拖入ul
嗨@ganaraj,我用我的新代码更新了我原来的帖子,我仍然无法得到这个新的项目进入我的ul – Crooksey
你可以创建一个plunkr或小提琴你想要实现的?我可以帮你修改代码。 van1也不要做van1(你可以在van1或van1中做van)。它的误导,可能会导致很多混乱。 – ganaraj
尝试http://plnkr.co/edit/x8EPgfk6kyHWRQTIFbN9或http://embed.plnkr.co/x8EPgfk6kyHWRQTIFbN9/preview干杯。 – Crooksey