AngularJS:无法计算二维数组表单模型中的值之和

AngularJS:无法计算二维数组表单模型中的值之和,angularjs,Angularjs,AngularJS:计算二维数组模型中的值之和 我有一个保存销售订单的复杂表单模型。 该模型具有以下结构: order { id: int, lines: [], // array of orderLine objects } orderLine { id: int, lineItems: [], // array of lineItem objects } lineItem { id: int, sku: {}, material:

AngularJS:计算二维数组模型中的值之和

我有一个保存销售订单的复杂表单模型。 该模型具有以下结构:

order {
    id: int,
    lines: [], // array of orderLine objects
}

orderLine {
    id: int,
    lineItems: [], // array of lineItem objects
}

lineItem {
    id: int,
    sku: {},
    material: {},
    skuModel: {},
}

sku {
    id: int,
    name: '',
    retailPrice: float,
    dynamicPricing: bool,
    materials: [],
    models: [],
}
Sku的价格可以是零售价,也可以根据材料和型号计算,具体取决于dynamicPricing是否设置为true。 我编写了以下代码来计算sku的小计:

$scope.subTotal = function (lineIndex, index) {
    if ($scope.order.lines[lineIndex].lineItems[index].material == null
        || $scope.order.lines[lineIndex].lineItems[index].skuModel == null) {
        return 0;
    }
    var total = $scope.order.lines[lineIndex].lineItems[index].material.unitPrice
    * $scope.order.lines[lineIndex].lineItems[index].skuModel.unitValue;
    return total;
};
请参阅下面的HTML。 这管用!当我选择的材料和模型的价格是正确的计算,并在页面上呈现

虽然我不能100%确定我的if语句是否正确,但也许有人会建议一个更好的方法

现在是大问题

我无法计算总数。所以我想计算订单中所有行的所有sku价格(动态或非动态)的总和。这是我得到的代码:

$scope.total = function () {
    var total = 0;
    console.log($scope.order.lines);
    for (var i = 0; i < $scope.order.lines.length; i++) {
        console.log($scope.order.lines[i]);
        angular.forEach ($scope.order.lines[i].lineItems, function (lineItem) {
            if (lineItem.sku.dynamicPricing) {
                if (lineItem.material == null || lineItem.skuModel == null) {
                    console.log("found null in material or skuModel");
                    return 0;
                }
                total = total + lineItem.material.unitPrice * lineItem.skuModel.unitValue;
            } else {
                total = total + lineItem.sku.retailPrice;
            }                
        });
    };
    return total;
};
$scope.total=函数(){
var合计=0;
log($scope.order.line);
对于(变量i=0;i<$scope.order.lines.length;i++){
log($scope.order.line[i]);
angular.forEach($scope.order.lines[i].lineItems,函数(lineItem){
if(lineItem.sku.dynamicPricing){
如果(lineItem.material==null | | lineItem.skuModel==null){
console.log(“在物料或模型中发现为空”);
返回0;
}
总计=总计+lineItem.material.unitPrice*lineItem.skuModel.unitValue;
}否则{
总计=总计+lineItem.sku.retailPrice;
}                
});
};
返回总数;
};
此控制台输出:

  • 资源{id:1,名称:“ProductEen”,defaultSku:Object,optionalSkus:Array[4],$promise:Object…}
  • [对象]
  • [对象]
  • 线路发现
  • 对象{id:null,product:Resource,lineItems:Array[5],$$hashKey:“024”}
  • [对象]
  • 线路发现
  • 对象{id:null,product:Resource,lineItems:Array[5],$$hashKey:“024”}
  • [对象]
  • 线路发现
  • 对象{id:null,product:Resource,lineItems:Array[5],$$hashKey:“024”}
  • [对象]
  • 线路发现
  • 对象{id:null,product:Resource,lineItems:Array[5],$$hashKey:“024”}
函数只在HTML上输出null,函数成功结束

这里提供的完整代码是将产品添加到模型和HTML的函数

$scope.selectProduct = function (row) {
    $scope.product = Product.get({id: row.entity.id});
    $scope.product.$promise.then(function (data) {

        var orderLine = {id: null, product: data, lineItems: []};
        var item = {id: null, sku: null, material: null, skuModel: null}; 
        item.sku = orderLine.product.defaultSku;

        for (var i = 0; i < orderLine.product.optionalSkus.length; i++) {
            var item = {id: null, sku: null, material: null, skuModel: null}; 
            item.sku = orderLine.product.optionalSkus[i];
            orderLine.lineItems.push(item);
        }

        $scope.order.lines.push(orderLine);
        console.log($scope.order.lines);
    });
    $('#saveProductModal').modal('hide');
};

<table>
    <thead>
        <tr>
            <th class="text-center">QTY</th>
            <th>ITEM</th>
            <th>DESCRIPTION</th>
            <th>PRICE</th>
            <th>SUBTOTAL</th>
        </tr>
    </thead>
    <tbody ng-repeat="orderLine in order.lines">
        <tr>
            <td class="text-center"><strong>1</strong>{{$index}}</td>
            <td colspan="3">
                {{orderLine.product.name}}
            </td>
            <td id="subTotalProduct($index)"></td>
        </tr>
        <tr ng-repeat="lineItem in orderLine.lineItems" ng-init="lineIdx = $parent.$index">
            <td></td>
            <td>
                {{lineItem.sku.name}}
            </td>
            <td>
                <select ng-show="lineItem.sku.dynamicPricing" ng-model="order.lines[$parent.$index].lineItems[$index].material" ng-options="material as material.name for material in lineItem.sku.materials"></select>
                <select ng-show="lineItem.sku.dynamicPricing" ng-model="order.lines[$parent.$index].lineItems[$index].skuModel" ng-options="skuModel as skuModel.name for skuModel in lineItem.sku.skuModels"></select>
            </td>
            <td ng-if="!lineItem.sku.dynamicPricing">
                {{lineItem.sku.retailPrice}}
            </td>
            <td ng-if="lineItem.sku.dynamicPricing">
                {{subTotal(lineIdx, $index)}}
            </td>
            <td ng-if="!lineItem.sku.dynamicPricing">
                {{lineItem.sku.retailPrice}}
            </td>
            <td ng-if="lineItem.sku.dynamicPricing">
                <!-- {{calculateSubTotal($parent.$index, $index)}} -->
            </td>
        </tr>
    </tbody>
    <tbody>
        <tr>
            <td colspan="3">
                <button class="btn btn-primary btn-xs" ng-click="openProductModal()">
                    <span class="glyphicon glyphicon-flash"></span> Add Product
                </button>
            </td>
        </tr>
        <tr>
            <td colspan="4">Total</td>
            <td><strong>{{total()}}</strong></td>
        </tr>
        <tr>
            <td colspan="4">HST/GST</td>
            <td><strong>21%</strong></td>
        </tr>
    </tbody>
</table>
$scope.selectProduct=函数(行){
$scope.product=product.get({id:row.entity.id});
$scope.product.$promise.then(函数(数据){
var orderLine={id:null,product:data,lineItems:[]};
var item={id:null,sku:null,material:null,skuModel:null};
item.sku=orderLine.product.defaultSku;
对于(变量i=0;i1{{$index}
{{orderLine.product.name}
{{lineItem.sku.name}
{{lineItem.sku.retailPrice}
{{小计(lineIdx,$index)}
{{lineItem.sku.retailPrice}
添加产品
全部的
{{total()}}
HST/GST
21%
console.log(JSON.stringify($scope.order)

{“id”:null,“行”:[{“id”:null,“产品”:{“id”:1,“名称”:“ProductEen”,“defaultSku”:{“id”:1,“名称”:“SkuEen”,“零售价”:9.99,“动态定价”:true,“可用”:true,“可销售”:true,“材料”:[{“id”:1,“名称”:“材料”、“单价”:1.99,“计量单位”:{“id”:1,“符号”:“G”,“名称”:“Gram”},{“id”:1,“名称”:“材料”、“单价”:1.99,“计量单位”:{“id”:1,“符号”:“G”,“名称”:“Gram”},{“id”:1,“名称”:“材料商”,“单价”:1.99,“计量单位”:{“id”:1,“符号”:“G”,“名称”:“Gram”},{“id”:1,“名称”:“材料商”,“单价”:1.99,“计量单位”:{“id”:1,“符号”:“G”,“名称”:“Gram”},{“id”:1,“名称”:“材料商”,“单价”:1.99,“计量单位”:“G”{“id”:“,”名称“:”Gram“},{”id“:5,“名称“:”Material5”,“单价”:1.99,“计量单位”:{”id“:5,“符号“:”M3“,”名称“:”Kubieke Meter“},{”id“:5,“名称“:”Material5”,“单价”:1.99,“计量单位”:{”id“:5,“符号“:”M3“,”名称“:”Kubieke Meter“},{”id“:5,“名称“:”Material5”,“单价“:”1.99,“计量单位”:{”id“:”5,“符号“,”M3“,”Kubieke”},{“id”:5,“名称”:“材料5”,“单价”:1.99,“计量单位”:{“id”:5,“符号”:“M3”,“名称”:“Kubieke流量计”},{“id”:5,“名称”:“材料5”,“单价”:1.99,“计量单位”:{“id”:5,“符号”:“M3”,“名称”:“Kubieke流量计”},{“id”:7,“名称”:“材料7”,“单价”:1.99,“计量单位”:{“id”:5,“符号”:“M3”,“名称”:“Kubieke流量计”},{“材料7”,“单价”:1.99,“计量单位”:{“id”:5,“符号”:“M3”,“名称”:“库比克流量计”},{“id”:7,“名称”:“材料7”,“单价”:1.99,“计量单位”:{“id”:5,“符号”:“M3”,“名称”:“库比克流量计”},{“id”:7,“名称”:“材料7”,“单价”:1.99,“计量单位”:{“id”:5,“符号”:“M3”,“名称”:“库比”