Skip to content

Commit

Permalink
Merge pull request #482 from marmelab/fix_sort
Browse files Browse the repository at this point in the history
[RFR] Fix sort for ReferenceListField and dashboard
  • Loading branch information
ThieryMichel committed Jun 1, 2015
2 parents 8ff3b7c + 14ee188 commit 9932690
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 8 deletions.
7 changes: 5 additions & 2 deletions examples/blog/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
// customize entities and views

post.dashboardView() // customize the dashboard panel for this entity
.name('posts')
.title('Recent posts')
.order(1) // display the post panel first in the dashboard
.perPage(5) // limit the panel to the 5 latest posts
Expand Down Expand Up @@ -128,9 +129,11 @@
.targetEntity(comment)
.targetReferenceField('post_id')
.targetFields([
nga.field('id'),
nga.field('created_at').label('Posted'),
nga.field('body').label('Comment')
]),
])
.sortField('created_at')
.sortDir('DESC'),
nga.field('', 'template').label('')
.template('<span class="pull-right"><ma-filtered-list-button entity-name="comments" filter="{ post_id: entry.values.id }" size="sm"></ma-filtered-list-button></span>')
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ define(function(require) {
'fields="::field.targetFields()" ' +
'list-actions="::field.listActions()" ' +
'entity="::field.targetEntity()" ' +
'sort-field="::field.sortField()" ' +
'sort-field="::field.getSortFieldName()" ' +
'sort-dir="::field.sortDir()">' +
'</ma-datagrid>';
}
Expand All @@ -23,7 +23,7 @@ define(function(require) {
'fields="::field.targetFields()" ' +
'list-actions="::field.listActions()" ' +
'entity="::field.targetEntity()" ' +
'sort-field="::field.sortField()" ' +
'sort-field="::field.getSortFieldName()" ' +
'sort-dir="::field.sortDir()">' +
'</ma-datagrid>';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ define(function (require) {
this.PanelBuilder = PanelBuilder;

this.$scope.edit = this.edit.bind(this);

var searchParams = this.$location.search();
this.sortField = 'sortField' in searchParams ? searchParams.sortField : null;
this.sortDir = 'sortDir' in searchParams ? searchParams.sortDir : null;

this.retrievePanels();

$scope.$on('$destroy', this.destroy.bind(this));
Expand All @@ -28,7 +33,7 @@ define(function (require) {
var self = this;
this.panels = [];

this.PanelBuilder.getPanelsData().then(function (panels) {
this.PanelBuilder.getPanelsData(this.sortField, this.sortDir).then(function (panels) {
self.panels = panels;
});
};
Expand Down
12 changes: 10 additions & 2 deletions src/javascripts/ng-admin/Main/component/service/PanelBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,25 @@ function PanelBuilder($q, $location, ReadQueries, Configuration, AdminDescriptio
*
* @returns {promise}
*/
PanelBuilder.prototype.getPanelsData = function () {
PanelBuilder.prototype.getPanelsData = function (sortField, sortDir) {
var dashboardViews = this.Configuration.getViewsOfType('DashboardView'),
dataStore = this.dataStore,
promises = [],
dashboardView,
dashboardSortField,
dashboardSortDir,
self = this,
i;

for (i in dashboardViews) {
dashboardView = dashboardViews[i];
promises.push(self.ReadQueries.getAll(dashboardView, 1, {}, dashboardView.getSortFieldName(), dashboardView.sortDir()));
dashboardSortField = dashboardView.getSortFieldName();
dashboardSortDir = dashboardView.sortDir();
if (sortField && sortField.split('.')[0] === dashboardView.name()) {
dashboardSortField = sortField;
dashboardSortDir = sortDir;
}
promises.push(self.ReadQueries.getAll(dashboardView, 1, {}, dashboardSortField, dashboardSortDir));
}

return this.$q.all(promises).then(function (responses) {
Expand Down
2 changes: 1 addition & 1 deletion src/javascripts/test/e2e/ShowViewSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('ShowView', function () {
$$('.ng-admin-field-comments th').then(function (inputs) {
expect(inputs.length).toBe(2);

expect(inputs[0].getAttribute('class')).toBe('ng-scope ng-admin-column-id');
expect(inputs[0].getAttribute('class')).toBe('ng-scope ng-admin-column-created_at');
expect(inputs[1].getAttribute('class')).toBe('ng-scope ng-admin-column-body');
});
});
Expand Down

0 comments on commit 9932690

Please sign in to comment.