I am working with an Angular UI grid where I need to display data from a nested JSON object. The JSON contains a lot of information, but I only want to show a selected set of properties on the grid. Instead of putting the entire json.Products
into the grid, I am looking for a more efficient solution.
HTML:
<div ng-app="exampleApp">
<section style="width:75%; margin-left:auto; margin-right:auto;" ng-controller="GridController">
<div id="grid1" ui-grid="gridOptions" class="grid"></div>
</section>
</div>
JS:
var exampleApp = angular.module('exampleApp', ['ui.grid']);
exampleApp.controller('GridController', ['$scope', function ($scope) {
$scope.gridOptions = {
paginationPageSizes: [15, 25, 50, 75],
paginationPageSize: 15,
enableSorting: true,
showGridFooter: true,
columnDefs: [
{ field: 'Products.ProductCode', name: 'Products.ProductCode', width: '200', displayName: 'PRODUCT CODE'},
{ field: 'Products.ProductName', name: 'Products.ProductName', displayName: 'PRODUCT NAME'}
],
onRegisterApi: function(gridApi) {
$scope.gridApi = gridApi;
}
};
var sResult = JSON.stringify(gridData);
var parsedResult = JSON.parse(sResult);
$scope.AllProductData = parsedResult;
$scope.productData = parsedResult.Products;
$scope.gridOptions.data = $scope.AllProductData;
console.log("AllProductData: " + JSON.stringify($scope.AllProductData));
}]);
var gridData = [{
"Products": [{
"RecordType": "012d0000000xBIOAA2",
"QuantityFlag": "Green",
"QuantityAvailable": null,
"ProductName": "10 Pin Connector, Digital Board",
"ProductId": "01td0000001skXZAAY",
"ProductCode": "7149",
"PricebookEntryId": "01ud0000005tOgzAAE",
"IsSelected": false,
"IsCommonItem": false,
"Active": true
}, {
"RecordType": "012d0000000xBIOAA2",
"QuantityFlag": "Green",
"QuantityAvailable": null,
"ProductName": "10 Prepaid worm",
"ProductId": "01td0000001sks9AAA",
"ProductCode": "805514-PPD",
"PricebookEntryId": "01ud0000005tLZHAA2",
"IsSelected": false,
"IsCommonItem": false,
"Active": true
}, {
"RecordType": "012d0000000xBIOAA2",
"QuantityFlag": "Red",
"QuantityAvailable": 0,
"ProductName": "10x1 ITALIAN DIAGNOSTIC KIT",
"ProductId": "01td0000001sl03AAA",
"ProductCode": "902232-ITA",
"PricebookEntryId": "01ud0000005tP99AAE",
"IsSelected": false,
"IsCommonItem": false,
"Active": true
}, {
"RecordType": "012d0000000xBIOAA2",
"QuantityFlag": "Red",
"QuantityAvailable": 0,
"ProductName": "10x1 SPAIN DIAGNOSTIC KIT",
"ProductId": "01td0000001sl0KAAQ",
"ProductCode": "902232-SPN",
"PricebookEntryId": "01ud0000005tPENAA2",
"IsSelected": false,
"IsCommonItem": false,
"Active": true
}],
"CustomerId": "006q0000007KyVnAAK",
"AccountId": "1035620"
}];
Refer to FIDDLE
Check out the Updated Fiddle which demonstrates the grid working with the account ID but not the nested products. This fiddle explains why I prefer not to use something like
$scope.gridOptions.data = grid[0].products;
If there is a solution that allows me to change the property products.IsSelected to True without looping and preserving the JSON structure, please advise. I aim to ensure that gridData.Products still references the original gridData.