-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy path405_exporting_all_data_complex.ngdoc
110 lines (100 loc) · 3.86 KB
/
405_exporting_all_data_complex.ngdoc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
@ngdoc overview
@name Tutorial: 405 Exporting All Data With External Pagination
@description
When using built in pagination, the data is fully loaded before export.
For external pagination, use the `exportAllDataPromise` function to load all grid data. If you get all the
data (for the purposes of exporting), then it makes sense to turn off external pagination and external sorting,
as all the data is now present within AngularJS.
For better performance with the following example, you can choose to load the ui-grid.core.js and specific feature files instead:
<pre>
<script src="/release/ui-grid.core.min.js"></script>
<script src="/release/ui-grid.exporter.min.js"></script>
<script src="/release/ui-grid.pagination.min.js"></script>
<script src="/release/ui-grid.selection.min.js"></script>
</pre>
@example
This shows combined external pagination and sorting.
<example module="app">
<file name="app.js">
var app = angular.module('app', ['ngTouch', 'ui.grid', 'ui.grid.pagination', 'ui.grid.selection', 'ui.grid.exporter']);
app.controller('MainCtrl', [
'$scope', '$http', 'uiGridConstants', function($scope, $http, uiGridConstants) {
var paginationOptions = {
sort: null
};
$scope.gridOptions = {
paginationPageSizes: [25, 50, 75],
paginationPageSize: 25,
useExternalPagination: true,
useExternalSorting: true,
enableGridMenu: true,
columnDefs: [
{ name: 'name' },
{ name: 'gender', enableSorting: false },
{ name: 'company', enableSorting: false }
],
exporterAllDataFn: function() {
return getPage(1, $scope.gridOptions.totalItems, paginationOptions.sort)
.then(function(allData) {
$scope.gridOptions.useExternalPagination = false;
$scope.gridOptions.useExternalSorting = false;
getPage = null;
return allData;
});
},
onRegisterApi: function(gridApi) {
$scope.gridApi = gridApi;
$scope.gridApi.core.on.sortChanged($scope, function(grid, sortColumns) {
if(getPage) {
if (sortColumns.length > 0) {
paginationOptions.sort = sortColumns[0].sort.direction;
} else {
paginationOptions.sort = null;
}
getPage(grid.options.paginationCurrentPage, grid.options.paginationPageSize, paginationOptions.sort)
}
});
gridApi.pagination.on.paginationChanged($scope, function (newPage, pageSize) {
if(getPage) {
getPage(newPage, pageSize, paginationOptions.sort);
}
});
}
};
var getPage = function(curPage, pageSize, sort) {
var url;
switch(sort) {
case uiGridConstants.ASC:
url = '/data/100_ASC.json';
break;
case uiGridConstants.DESC:
url = '/data/100_DESC.json';
break;
default:
url = '/data/100.json';
break;
}
var _scope = $scope;
return $http.get(url)
.then(function (response) {
var firstRow = (curPage - 1) * pageSize;
$scope.gridOptions.totalItems = 100;
$scope.gridOptions.data = response.data.slice(firstRow, firstRow + pageSize);
return response.data.slice(firstRow, firstRow + pageSize);
});
};
getPage(1, $scope.gridOptions.paginationPageSize);
}
]);
</file>
<file name="index.html">
<div ng-controller="MainCtrl">
<div ui-grid="gridOptions" ui-grid-pagination ui-grid-selection ui-grid-exporter class="grid"></div>
</div>
</file>
<file name="main.css">
.grid {
width: 600px;
}
</file>
</example>