Javascript 在ng网格中添加具有计算字段的新行

Javascript 在ng网格中添加具有计算字段的新行,javascript,angularjs,angular-ui,ng-grid,Javascript,Angularjs,Angular Ui,Ng Grid,我面临的问题是在ng网格中添加具有计算字段的新行 Html代码 <div class="costsheetGrid" ng-controller="costsheetGridController"> <p> <a class="btn btn-lg btn-primary" ng-click="addAnotherRow()" href="javascript:void(0);">Add Another Row</

我面临的问题是在ng网格中添加具有计算字段的新行

Html代码

<div class="costsheetGrid" ng-controller="costsheetGridController">
        <p>
            <a class="btn btn-lg btn-primary" ng-click="addAnotherRow()" href="javascript:void(0);">Add Another Row</a>
        </p>
        <div class="gridStyle" ng-grid="gridOptions">
        </div>
    </div> 
当我更改网格第一行中的消耗量或转换数量并计算实际消耗量时,此代码非常有效。当我单击“添加另一行”按钮时,它会添加一个新行,但当我在第二行中插入消耗量或转换量时,它无法计算实际消耗量


那么,我怎样才能做到这一点呢

您仅为初始数据的每一行附加
getActualConsumption
函数。创建新行时,还应添加该函数

$scope.addAnotherRow = function () {
            $scope.gridData.push({
                'Consumption': '',
                'ConversionQuantity': '',
                 getActualConsumption: function () {
                    return this.Consumption / this.ConversionQuantity;
                }
            });
        };
请注意我刚刚复制了您的
getActualConsumption
的实现-您可能应该进行某种验证,以确保不会因为按零跳转并确保两个值都是实际数字而出错

$scope.addAnotherRow = function () {
            $scope.gridData.push({
                'Consumption': '',
                'ConversionQuantity': '',
                 getActualConsumption: function () {
                    return this.Consumption / this.ConversionQuantity;
                }
            });
        };