Skip to content

Commit

Permalink
Merge pull request #691 from marmelab/default_filter_fix
Browse files Browse the repository at this point in the history
[RFR] Fix default filter glitch
  • Loading branch information
fzaninotto committed Sep 16, 2015
2 parents b36634e + 2b3a3ba commit 2ebcec2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
21 changes: 14 additions & 7 deletions src/javascripts/ng-admin/Crud/list/ListLayoutController.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ var ListLayoutController = function ($scope, $stateParams, $state, $location, $t
// the controller doesn't change when the search changes
// so we must update filter values manually when the location changes
$scope.$watch(
() => $location.search() && $location.search().search,
newValues => this.search = $location.search().search ? JSON.parse($location.search().search) : {}
() => $location.search() && $location.search().search ,
(newval, oldval) => {
if (newval === oldval) return;
this.search = $location.search().search ? JSON.parse($location.search().search) : {};
this.enabledFilters = this.getEnabledFilters();
}
);
// apply filters when filter values change
$scope.$watch(
Expand All @@ -28,10 +32,7 @@ var ListLayoutController = function ($scope, $stateParams, $state, $location, $t
true
);
this.filters = view.filters();
this.enabledFilters = this.filters.filter(filter => {
if (filter.pinned()) return true;
return this.search && (filter.name() in this.search)
});
this.enabledFilters = this.getEnabledFilters();
this.hasFilters = Object.keys(this.filters).length > 0;
this.focusedFilterId = null;
this.enableFilter = this.enableFilter.bind(this);
Expand All @@ -58,6 +59,13 @@ ListLayoutController.prototype.enableFilter = function (filter) {
}, 200, false);
}

ListLayoutController.prototype.getEnabledFilters = function () {
return this.filters.filter(filter => {
if (filter.pinned()) return true;
return this.search && (filter.name() in this.search)
});
}

ListLayoutController.prototype.updateFilters = function () {
var values = {},
filters = this.enabledFilters,
Expand All @@ -77,7 +85,6 @@ ListLayoutController.prototype.updateFilters = function () {
values[fieldName] = this.search[fieldName];
}
}

this.$stateParams.search = values;
this.$stateParams.page = 1;
this.$state.go('list', this.$stateParams);
Expand Down
26 changes: 13 additions & 13 deletions src/javascripts/ng-admin/Crud/routing.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ function routing($stateProvider) {
}
})
.state('list', {
url: '?{search:json}&page&sortField&sortDir',
url: '?{search:json}&{page:int}&sortField&sortDir',
params: {
page: null,
search: null,
page: { value: 1, squash: true },
search: { value: {}, squash: true },
sortField: null,
sortDir: null
},
Expand Down Expand Up @@ -156,8 +156,8 @@ function routing($stateProvider) {
params: {
entity: null,
id: null,
page: null,
search: null,
page: { value: 1, squash: true },
search: { value: {}, squash: true },
sortField: null,
sortDir: null
},
Expand Down Expand Up @@ -233,8 +233,8 @@ function routing($stateProvider) {
controllerAs: 'formController',
templateProvider: templateProvider('CreateView', createTemplate),
params: {
page: null,
search: null,
page: { value: 1, squash: true },
search: { value: {}, squash: true },
sortField: null,
sortDir: null
},
Expand Down Expand Up @@ -283,8 +283,8 @@ function routing($stateProvider) {
params: {
entity: null,
id: null,
page: null,
search: null,
page: { value: 1, squash: true },
search: { value: {}, squash: true },
sortField: null,
sortDir: null
},
Expand Down Expand Up @@ -387,8 +387,8 @@ function routing($stateProvider) {
controllerAs: 'deleteController',
templateProvider: templateProvider('DeleteView', deleteTemplate),
params: {
page: null,
search: null,
page: { value: 1, squash: true },
search: { value: {}, squash: true },
sortField: null,
sortDir: null
},
Expand Down Expand Up @@ -417,8 +417,8 @@ function routing($stateProvider) {
params: {
entity: null,
ids: [],
page: null,
search: null,
page: { value: 1, squash: true },
search: { value: {}, squash: true },
sortField: null,
sortDir: null
},
Expand Down
2 changes: 1 addition & 1 deletion src/javascripts/test/e2e/ListViewSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('ListView', function () {
var listUrl;

beforeEach(function() {
listUrl = encodeURI(browser.baseUrl + '/#/comments/list?search={"post_id":"9"}&page=1');
listUrl = encodeURI(browser.baseUrl + '/#/comments/list?search={"post_id":"9"}');
browser.get(listUrl);
});

Expand Down

0 comments on commit 2ebcec2

Please sign in to comment.