jQuery   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了jquery-ui – AngularJS动态列表总和大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用angularJS ontop我的应用程序.

我有一个控制器的基本示例:

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 - @R_979_10586@l 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,我有多个列表,其中li项在每个列表之间被拖放.我的问题是,如何让XXX动态更新ul中每个li中所有权重的值,或者更多的问题甚至可以做到这一点?

我真的不想使用onDrop事件,因为这对预先填充的列表不起作用,所以理想情况下我想使用从ul中所有van1.orderWeight值获取值的代码.

任何关于最佳方法的建议都将非常感谢!之前有人问我使用[[和]]而不是{{和}},因为我使用的是jinja2模板.

更新:

好了,看完下面的答案后,将原控制器修改为:

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,0);
    }

还有我的模板

<div id="app" ng-controller="OrderListCtrl">

<div id=1>
<h1>Van 1 - @R_979_10586@l 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库来帮助执行计算,但我似乎只能使用初始数据来使用它,而不是在从另一个ul拖入新订单时更新

解决方法

这在Angular中实现的非常好.您必须在控制器中编写一个函数,为您进行计算并在视图中插入该函数.就像是

<div id=1>
<h1>Van 1 - @R_979_10586@l Weight = [[getVanWeight()]] </h1>
<ul class="sortdrag">
    <li ng-repeat="van1 in vans" id="[[ van1.id ]]">
      [[van1.custName]] [[van1.accountnumber]] [[van1.orderWeight]]

    </li>
</ul>
</div>

在您的控制器内,您可以:

$scope.getVanWeight = function(){
// write your logic for calculaTing van weight here.

}

大佬总结

以上是大佬教程为你收集整理的jquery-ui – AngularJS动态列表总和全部内容,希望文章能够帮你解决jquery-ui – AngularJS动态列表总和所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。