Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFR] Fix maExportToCsvButton does not return entries references and empty data when filters contains boolean field #461

Merged
merged 1 commit into from
May 26, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 68 additions & 16 deletions src/javascripts/ng-admin/Crud/button/maExportToCsvButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ define(function () {
scope: {
entity: '&',
label: '@',
datastore: '&'
datastore: '&',
search: '&'
},
template: '<button ng-if="has_export" class="btn btn-default" ng-click="exportToCsv()"><span class="glyphicon glyphicon-download" aria-hidden="true"></span>&nbsp;{{ ::label }}</button>',
link: function(scope) {
Expand All @@ -25,27 +26,78 @@ define(function () {
exportFields = listView.fields();
}
exportView.fields(exportFields);
exportView.filters(listView.filters());
}
scope.has_export = exportView.fields().length > 0;
var formatEntry = entryFormatter.getFormatter(exportView.fields());

scope.exportToCsv = function () {
var rawEntries;
var nonOptimizedReferencedData;
var optimizedReferencedData;

ReadQueries.getAll(exportView, -1, true, $stateParams.search, $stateParams.sortField, $stateParams.sortDir).then(function (response) {
var results = [];
var entries = scope.datastore.mapEntries(scope.entity.name(), exportView.identifier(), exportView.fields(), response.data);
for (var i = entries.length - 1; i >= 0; i--) {
results[i] = formatEntry(entries[i]);
}
var csv = Papa.unparse(results);
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.click();
}, function (error) {
notification.log(error.message, {addnCls: 'humane-flatty-error'});
});
ReadQueries.getAll(exportView, -1, scope.search(), $stateParams.sortField, $stateParams.sortDir)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we use $stateParams.search instead of scope.search(), it's not the right value (but it should be the same value as in ListController...).

.then(function (response) {
rawEntries = response.data;

return rawEntries;
}, function (error) {
notification.log(error.message, {addnCls: 'humane-flatty-error'});
})
.then(function (rawEntries) {
nonOptimizedReferencedData = ReadQueries.getFilteredReferenceData(exportView.getNonOptimizedReferences(), rawEntries);

return rawEntries;
})
.then(function (rawEntries) {
optimizedReferencedData = ReadQueries.getOptimizedReferencedData(exportView.getOptimizedReferences(), rawEntries);

return rawEntries;
})
.then(function (rawEntries) {
var references = exportView.getReferences(),
referencedData = angular.extend(nonOptimizedReferencedData, optimizedReferencedData),
referencedEntries;

for (var name in referencedData) {
referencedEntries = scope.datastore.mapEntries(
references[name].targetEntity().name(),
references[name].targetEntity().identifier(),
[references[name].targetField()],
referencedData[name]
);

scope.datastore.setEntries(
references[name].targetEntity().uniqueId + '_values',
referencedEntries
);
}

return rawEntries;
})
.then(function (rawEntries) {
var entries = scope.datastore.mapEntries(
exportView.entity.name(),
exportView.identifier(),
exportView.getFields(),
rawEntries
);

// shortcut to diplay collection of entry with included referenced values
scope.datastore.fillReferencesValuesFromCollection(entries, exportView.getReferences(), true);

var results = [];
for (var i = entries.length - 1; i >= 0; i--) {
results[i] = formatEntry(entries[i]);
}
var csv = Papa.unparse(results);
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.click();
})
;
};
},
};
Expand Down
6 changes: 3 additions & 3 deletions src/javascripts/ng-admin/Crud/list/ListController.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ define(function () {
this.actions = view.actions();
this.batchActions = view.batchActions();
this.loadingPage = false;
this.search = $stateParams.search;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This $stateParams.search is ok.

this.filters = view.filters();
this.hasFilters = Object.keys(this.filters).length > 0;
this.dataStore = dataStore;
Expand All @@ -40,13 +41,12 @@ define(function () {
}

var progression = this.progression,
self = this,
filters = this.$stateParams.search;
self = this;

progression.start();

this.ReadQueries
.getAll(this.view, page, true, filters, this.sortField, this.sortDir)
.getAll(this.view, page, true, this.search, this.sortField, this.sortDir)
.then(function (nextData) {
progression.done();
self.entries = self.entries.concat(nextData.entries);
Expand Down
4 changes: 2 additions & 2 deletions src/javascripts/ng-admin/Crud/list/list.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<div class="row list-header">
<div class="col-lg-12">
<ma-view-actions override="::listController.actions" selection="listController.selection" batch-buttons="::listController.batchActions" entity="::listController.entity" datastore="::listController.dataStore">
<ma-view-actions override="::listController.actions" selection="listController.selection" batch-buttons="::listController.batchActions" entity="::listController.entity" datastore="::listController.dataStore" search="::listController.search">
<ma-view-batch-actions buttons="::batchButtons()" selection="selection" entity="::entity"></ma-view-batch-actions>
<ma-create-button ng-if="::entity.creationView().enabled" entity="::entity"></ma-create-button>
<ma-export-to-csv-button entity="::entity" datastore="::datastore"></ma-export-to-csv-button>
<ma-export-to-csv-button entity="::entity" search="::search" datastore="::datastore"></ma-export-to-csv-button>
</ma-view-actions>

<div class="page-header">
Expand Down
3 changes: 2 additions & 1 deletion src/javascripts/ng-admin/Crud/misc/ViewActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ define(function (require) {
'entity': '=',
'selection': '=',
batchButtons: '&',
datastore: '='
datastore: '=',
search: '='
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not used

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is... Angularjs black magic!

},
template: viewActionsTemplate,
link: function($scope, element, attrs, controller, transcludeFn) {
Expand Down
2 changes: 1 addition & 1 deletion src/javascripts/ng-admin/Crud/misc/view-actions.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
<ma-show-button ng-switch-when="show" entry="entry" entity="entity"></ma-show-button>
<ma-edit-button ng-switch-when="edit" entry="entry" entity="entity"></ma-edit-button>
<ma-delete-button ng-switch-when="delete" entry="entry" entity="entity"></ma-delete-button>
<ma-export-to-csv-button ng-switch-when="export" entry="entry" entity="entity"></ma-export-to-csv-button>
<ma-export-to-csv-button ng-switch-when="export" datastore="datastore" entity="entity" search="search"></ma-export-to-csv-button>
<span ng-switch-default><span compile="button"></span></span>
</span>