如何通过字段函数获取一列的总数?

How to get the total of a column with a field function?

我正在尝试计算我有字段函数的列 TOTAL。 这是我到目前为止所拥有的: https://plnkr.co/edit/vhstPeg2BYz1oWGGwido?p=preview

var app = angular.module('myApp', ['ui.grid']);
app.controller('MyCtrl', function($scope) {
$scope.myData = [
         {x: 1, y: 50},
                 {x: 4, y: 43},
                     {x: 12,y: 27},
                     {x: 9, y: 29},
                     {x: 23, y: 34 }];

    angular.forEach($scope.myData,function(row){
      row.getTotal = function(){
        return this.x  + this.y ;
      };
    });




$scope.gridOptionsString = { 
    data: 'myData',
    columnDefs: [{field: 'x', displayName: 'x'},
                 {field:'y', displayName:'y'},
                 {field: 'getTotal()', displayName: 'sum'},
                 ]
    };

});

谢谢!

应该这样做:

来自控制器的相关代码:

var used = [];
$scope.grandTotal = 0;
angular.forEach($scope.myData,function(row,idx){
  row.getTotal = function(){
    if (used.indexOf(idx) == -1) {
      $scope.grandTotal += this.x  + this.y;
      used.push(idx);
    }
    return this.x  + this.y ;
  };
});

您更新的 Plunker 在这里,http://plnkr.co/edit/1FHgSViYgpfXgQEPfXr5?p=preview

更新(基于下面的response/plunker-link/comment)

新屏幕布局:

来自控制器的相关代码:

var used = [];
$scope.grandTotal = 0;
angular.forEach($scope.myData, function(row, idx) {
  row.getTotal = function() {
    var value;
    if (this.xBox) {
      value = this.x + this.z;
    } else if (this.yBox) {
      value = this.y + this.z;
    }
    if (used.indexOf(idx) == -1) {
      $scope.grandTotal += value;
      used.push(idx);
    }
    return value;
  };
});
$scope.updateXRowClear = function(row) {
  row.entity.yBox = false;
  /* Need to check the ybox cell when unchecked */
  if (row.entity.xBox === false) {
    row.entity.yBox = true;
    $scope.grandTotal += row.entity.y - row.entity.x;
  } else {
    $scope.grandTotal += row.entity.x - row.entity.y;
  }
};
$scope.updateYRowClear = function(row) {
  row.entity.xBox = false;
  /* Need to check the xbox cell when unchecked */
  if (row.entity.yBox === false) {
    row.entity.xBox = true;
    $scope.grandTotal += row.entity.x - row.entity.y;
  } else {
    $scope.grandTotal += row.entity.y - row.entity.x;
  }
};

新更新的 Plunker,https://plnkr.co/edit/ixdN0J2oVvOlbbziJMCY?p=preview

再次更新(基于以下评论)

新屏幕布局:

来自控制器的相关代码:

var used = [];
$scope.grandTotal = 0;
angular.forEach($scope.myData, function(row, idx) {
  row.getTotal = function() {
    var value;
    if (this.xBox) {
      value = this.x * this.qty;
    } else if (this.yBox) {
      value = this.y * this.qty;
    } else if (this.zBox) {
      value = this.z * this.qty;
    }
    if (used.indexOf(idx) == -1) {
      $scope.grandTotal += value;
      used.push(idx);
    }
    return value;
  };
  $scope.$watch(
    function($scope) {
      return row.getTotal();
    },
    function(newValue, oldValue) {
      $scope.grandTotal += (newValue ? newValue : 0) - (oldValue ? oldValue : 0);
    }
  );
});
$scope.updateXRowClear = function(row) {
  row.entity.yBox = false;
  row.entity.zBox = false;
  /* Need to check the ybox cell when unchecked */
  if (row.entity.xBox === false) {
    row.entity.yBox = true;
  }
  if (row.entity.yBox === false) {
    row.entity.xBox = true;
  }
};
$scope.updateYRowClear = function(row) {
  row.entity.xBox = false;
  row.entity.zBox = false;
  /* Need to check the xbox cell when unchecked */
  if (row.entity.yBox === false) {
    row.entity.xBox = true;
  } else if (row.entity.xBox === false) {
    row.entity.yBox = true;
  }
};
$scope.updateZRowClear = function(row) {
  row.entity.xBox = false;
  row.entity.yBox = false;
  /* Need to check the zbox cell when unchecked */
  if (row.entity.zBox === false) {
    row.entity.xBox = true;
  } else if (row.entity.xBox === false) {
    row.entity.zBox = true;
  } else if (row.entity.yBox === false) {
    row.entity.yBox = true;
  }
};

还有所有重要的工作 Plunker,https://plnkr.co/edit/1rRRWEIyQhKVkYRdtIFu?p=preview

如果您有任何其他问题,请告诉我,很乐意为您提供帮助!