Skip to content

Commit

Permalink
Add doc and default export fields
Browse files Browse the repository at this point in the history
  • Loading branch information
fzaninotto committed Apr 3, 2015
1 parent ba30f28 commit fd83c50
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 41 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,21 @@ Add you own batch action directives.

listView.actions('create', '<my-custom-directive selection="selection"></my-custom-directive>');

* `exportFields(Array)`
Set the fields for the CSV export function. By default, ng-admin uses the fields displayed in the datagrid, but you can choose to export a different set of fields.

listView.exportFields([
nga.field('id', 'number'),
nga.field('author'),
nga.field('post_id', 'reference')
.label('Post')
.map(truncate)
.targetEntity(post)
.targetField(nga.field('title').map(truncate))
nga.field('body', 'wysiwyg')
.stripTags(true)
]);

## Fields

A field is the representation of a property of an entity.
Expand Down
6 changes: 4 additions & 2 deletions examples/blog/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
RestangularProvider.addFullRequestInterceptor(function(element, operation, what, url, headers, params) {
if (operation == "getList") {
// custom pagination params
params._start = (params._page - 1) * params._perPage;
params._end = params._page * params._perPage;
if (params._page) {
params._start = (params._page - 1) * params._perPage;
params._end = params._page * params._perPage;
}
delete params._page;
delete params._perPage;
// custom sort params
Expand Down
18 changes: 9 additions & 9 deletions src/javascripts/ng-admin/Crud/button/maExportToCsvButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
define(function () {
'use strict';

function maExportToCsvButton (Papa, $stateParams, $window, notification, entryFormater, RetrieveQueries) {
function maExportToCsvButton ($stateParams, Papa, notification, entryFormater, RetrieveQueries) {
return {
restrict: 'E',
scope: {
entity: '&'
},
template: '<button class="btn btn-default" ng-click="exportToCsv()"><span class="glyphicon glyphicon-download" aria-hidden="true"></span>Export CSV</button>',
link: function($scope) {
$scope.entity = $scope.entity();
var formatEntry = entryFormater.format($scope.entity.listView().exportFields());
template: '<button class="btn btn-default" ng-click="exportToCsv()"><span class="glyphicon glyphicon-download" aria-hidden="true"></span>&nbsp;Export</button>',
link: function(scope) {
scope.entity = scope.entity();
var formatEntry = entryFormater.getFormatter(scope.entity.listView().exportFields());

$scope.exportToCsv = function () {
scope.exportToCsv = function () {

RetrieveQueries.getAll($scope.entity.listView(), -1, true, $stateParams.search, $stateParams.sortField, $stateParams.sortDir).then(function (response) {
RetrieveQueries.getAll(scope.entity.listView(), -1, true, $stateParams.search, $stateParams.sortField, $stateParams.sortDir).then(function (response) {
var results = [], entries = response.entries;
for (var i = entries.length - 1; i >= 0; i--) {
results[i] = formatEntry(entries[i]);
Expand All @@ -25,7 +25,7 @@ define(function () {
var fakeLink = document.createElement('a');

fakeLink.setAttribute('href', 'data:application/octet-stream;charset=utf-8,' + encodeURIComponent(csv));
fakeLink.setAttribute('download', $scope.entity.name() + '.csv');
fakeLink.setAttribute('download', scope.entity.name() + '.csv');
fakeLink.click();
}, function (error) {
notification.log(error.message, {addnCls: 'humane-flatty-error'});
Expand All @@ -35,7 +35,7 @@ define(function () {
};
}

maExportToCsvButton.$inject = ['Papa', '$stateParams', '$window', 'notification', 'EntryFormater', 'RetrieveQueries'];
maExportToCsvButton.$inject = ['$stateParams', 'Papa', 'notification', 'EntryFormater', 'RetrieveQueries'];

return maExportToCsvButton;
});
60 changes: 31 additions & 29 deletions src/javascripts/ng-admin/Crud/misc/EntryFormater.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,53 @@
define(function () {
'use strict';

function EntryFormater() {
function EntryFormatter() {
}

EntryFormater.prototype.format = function format(fields) {
var map = fields.map(function (field) {
switch (field.type()) {
case 'number':
case 'text':
case 'wysiwyg':
case 'string':
case 'boolean':
case 'email':
case 'json':
case 'date':
case 'choice':
case 'choices':
case 'file':
case 'template':
return {name: field.name(), type: 'field'};
case 'reference':
return {name: field.name(), type: 'reference'};
case 'referenced_list':
return;//ignored
}
});
EntryFormatter.prototype.formatField = function formatField(field) {
switch (field.type()) {
case 'number':
case 'text':
case 'wysiwyg':
case 'string':
case 'boolean':
case 'email':
case 'json':
case 'date':
case 'choice':
case 'choices':
case 'file':
case 'template':
return { label: field.label(), name: field.name(), type: 'field' };
case 'reference':
return { label: field.label(), name: field.name(), type: 'reference' };
case 'referenced_list':
return; //ignored
}
}

EntryFormatter.prototype.getFormatter = function getFormatter(fields) {
var formattedFields = fields.map(this.formatField);

return function formatEntry(entry) {
var result = {}, i, length = map.length, field;
var result = {}, i, length = formattedFields.length, field;

for (i = 0; i < length; i++) {
field = map[i];
field = formattedFields[i];
if (!field) {continue;}
if (field.type === 'reference') {
result[field.name] = entry.listValues[field.name];
result[field.label] = entry.listValues[field.name];
} else if (field.type === 'field') {
if ('undefined' === typeof entry.values[field.name]) {continue;}
result[field.name] = entry.values[field.name];
result[field.label] = entry.values[field.name];
}
}

return result;
};
};

EntryFormater.$inject = [];
EntryFormatter.$inject = [];

return EntryFormater;
return EntryFormatter;
});
2 changes: 1 addition & 1 deletion src/javascripts/ng-admin/es6/lib/View/ListView.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class ListView extends View {

exportFields() {
if (!arguments.length) {
return this._exportFields;
return this._exportFields.length == 0 ? this._fields : this._exportFields;
}

this._exportFields = arguments[0];
Expand Down

0 comments on commit fd83c50

Please sign in to comment.