Reputation: 71
Is possible to make operation inside the field? I need to multiply CostoUnitario with Qta, and display it in the TOTALE column
This is my array.In the ng grid, the user will insert values (I setted 4 field just to try). Everything works fine elsewhere, I can get the result I want, but I need the field Totale to show the multiplication between CostoUnitario and Qta. (It's an invoice.)
$scope.myData = [{Numero:'',Descrizione:'',CostoUnitario:'10',Qta:'1',Totale:''},
{Numero:'',Descrizione:'',CostoUnitario:'90',Qta:'1',Totale:''},
{Numero:'',Descrizione:'',CostoUnitario:'',Qta:'',Totale:''},
{Numero:'',Descrizione:'',CostoUnitario:'',Qta:'',Totale:''},
{Numero:'',Descrizione:'',CostoUnitario:'',Qta:'',Totale:''},
{Numero:'',Descrizione:'',CostoUnitario:'',Qta:'',Totale:''}
];
$scope.gridOptions = {
data: 'myData',
enableCellSelection: true,
enableCellEdit: true,
enableRowSelection: false,
columnDefs: [{field: 'Numero', displayName: 'Numero', enableCellEdit: true},
{field:'Descrizione', displayName:'Descrizione'},
{field: 'CostoUnitario', displayName: 'Costo Unitario', enableCellEdit: true},
{field: 'Qta', displayName: 'Qta', enableCellEdit: true},
{field: 'CostoUnitario*Qta', displayName: 'Totale', enableCellEdit: false}]
Upvotes: 0
Views: 1290
Reputation: 4318
You're on the right track. I attached a function to each row in the data and referenced it in field
key for the Totale
column.
Here's the plnkr
columnDefs: [{field: 'Numero', displayName: 'Numero', enableCellEdit: true},
{field:'Descrizione', displayName:'Descrizione'},
{field: 'CostoUnitario', displayName: 'Costo Unitario', enableCellEdit: true},
{field: 'Qta', displayName: 'Qta', enableCellEdit: true},
{field: 'getTotale()', displayName: 'Totale', enableCellEdit: false}
]
...
angular.forEach($scope.myData, function(row) {
row.getTotale = function() {
return row.CostoUnitario * row.Qta;
};
});
Upvotes: 1