Skip to content

Instantly share code, notes, and snippets.

@tams89
Created March 20, 2016 17:33
Show Gist options
  • Save tams89/bc1fad0e576a95c3bb96 to your computer and use it in GitHub Desktop.
Save tams89/bc1fad0e576a95c3bb96 to your computer and use it in GitHub Desktop.
expandle row ui-grid, columns specific to row element.
[
{
"HoursToRecharge": 2,
"IsElectric": true,
"Manufacturer": "Tesla",
"Type": "Domain.EcoCar"
},
{
"HoursToRecharge": 5,
"IsElectric": true,
"Manufacturer": "Honda",
"Type": "Domain.EcoCar"
},
{
"HoursToRecharge": 0,
"IsElectric": false,
"Manufacturer": "Ford",
"Type": "Domain.EcoCar"
},
{
"HasFlappyPaddle": true,
"HasManualGearBox": false,
"Manufacturer": "Ferrari",
"Type": "Domain.SportsCar"
},
{
"HasFlappyPaddle": true,
"HasManualGearBox": true,
"Manufacturer": "Lambo",
"Type": "Domain.SportsCar"
},
{
"HasFlappyPaddle": false,
"HasManualGearBox": true,
"Manufacturer": "Jaguar",
"Type": "Domain.SportsCar"
},
{
"Manufacturer": "Mercedes",
"Type": "Domain.Car"
}
]
<div ui-grid="row.entity.subGridOptions" class="subGrid"></div>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>UI-Grid</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular-animate.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular-route.min.js"></script>
<script src="https://cdn.rawgit.com/angular-ui/bower-ui-grid/master/ui-grid.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.rawgit.com/angular-ui/bower-ui-grid/master/ui-grid.min.css">
<style>
.btn {
margin: 1%;
}
.grid {
margin: 1%;
height: 95vh;
}
.subGrid {
}
</style>
</head>
<body ng-app="app">
<div ng-controller="MainCtrl">
<div ui-grid="gridOptions" class="grid" ui-grid-expandable></div>
</div>
<script type="text/javascript">
var app = angular.module('app', ['ngAnimate', 'ui.grid', 'ui.grid.expandable']);
app.controller('MainCtrl', [
'$scope',
'$http',
function (
$scope,
$http
) {
$scope.columns = [
{
field: 'Manufacturer'
}];
$scope.gridOptions = {
enableSorting: true,
columnDefs: $scope.columns,
expandableRowTemplate: 'expandableRowTemplate.html',
expandableRowHeight: 70
};
$http.get('data.json')
.success(function (data) {
for (var i = 0; i < data.length; i++) {
// For each type of object add specific columns or empty array where no columns required.
if (data[i].Type === "Domain.EcoCar") {
// Create property called subGridOptions and assign data.
data[i].subGridOptions = {
columnDefs: [
{
field: "IsElectric"
},
{
field: "HoursToRecharge"
}],
data: [data[i]]
}
}
else if (data[i].Type === "Domain.SportsCar") {
data[i].subGridOptions = {
columnDefs: [
{
field: "HasFlappyPaddle"
},
{
field: "HasManualGearBox"
}],
data: [data[i]]
}
}
else if (data[i].Type === "Domain.Car") {
data[i].subGridOptions = {
data: []
}
}
}
// Data for main grid.
$scope.gridOptions.data = data;
});
}]);
</script>
</body>
</html>
@tams89
Copy link
Author

tams89 commented Mar 20, 2016

Simple example for a expandable ui-grid row, with columns that are specific to each parent row.

@tams89
Copy link
Author

tams89 commented Mar 20, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment