2017-03-23 6 views
1

フィールドの機能を持つ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'}, 
       ] 
    }; 

}); 

ありがとう!

答えて

1

これはそれを行う必要があります:コントローラから

angularjs ui-grid custom column total

関連するコード:

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です。 (以下応答/ plunkerリンク/コメントに基づいて)

更新

新しい画面レイアウト:

angularjs ui-grid custom column total

コントローラから該当コード:

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

(以下のコメントに基づいて)再び更新

新しい画面レイアウト:

angularjs ui-grid custom column total

コントローラから関連するコード:

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

ご質問がありましたら、お気軽にお問い合わせください。

+0

ありがとうTim!今あなたはそれをどのようにダイナミックにしますか? https://plnkr.co/edit/vhstPeg2BYz1oWGGwido?p=preview – UCDaCode

+0

同じチェックボックスを複数回クリックすると、総額が増えます。 – UCDaCode

+1

うわー!あなたは高速です!もう2つの事をもう一度: 1.もう1つの列を追加したい。私が列yからzに、そしてその逆に切り替えると、動的合計が正しくありません。 2. qty列を編集すると、動的合計をどのように更新できますか? PS ご協力いただき誠にありがとうございます。 =) – UCDaCode

関連する問題