diff --git a/examples/blog/config.js b/examples/blog/config.js index 11488953..3c90c9b9 100644 --- a/examples/blog/config.js +++ b/examples/blog/config.js @@ -33,7 +33,7 @@ return { params: params }; }); - RestangularProvider.addResponseInterceptor(function(data, operation, what, url, response, deferred) { + RestangularProvider.addResponseInterceptor(function(data, operation, what, url, response) { if (operation === "getList") { var headers = response.headers(); if (headers['content-range']) { @@ -101,7 +101,7 @@ .targetEntity(tag) // the tag entity is defined later in this file .targetField(nga.field('name')) // the field to be displayed in this list .cssClasses('hidden-xs') - .singleApiCall(ids => { return {'id': ids }}) + .singleApiCall(ids => { return {'id': ids }; }) ]) .filters([ nga.field('category', 'choice').choices([ @@ -193,13 +193,15 @@ .targetEntity(post) .targetField(nga.field('title').map(truncate)) .cssClasses('hidden-xs') - .singleApiCall(ids => { return {'id': ids }}) + .singleApiCall(ids => { return {'id': ids }; }) ]) .filters([ nga.field('q') .label('') .pinned(true) - .template('
'), + .template('
') + .transform(v => v && v.toUpperCase()) // transform the entered value before sending it as a query parameter + .map(v => v && v.toLowerCase()), // map the query parameter to a displayed value in the filter form nga.field('created_at', 'date') .label('Posted') .attributes({'placeholder': 'Filter by date'}), @@ -279,7 +281,7 @@ nga.field('published', 'boolean').validation({ required: true // as this boolean is required, ng-admin will use a checkbox instead of a dropdown }) - ]) + ]); tag.showView() .fields([ @@ -410,7 +412,7 @@ template: '

View post

', link: function (scope) { scope.displayPost = function () { - $location.path('/posts/show/' + scope.entry().values.post_id); + $location.path('/posts/show/' + scope.entry().values.post_id); // jshint ignore:line }; } }; diff --git a/src/javascripts/ng-admin/Crud/list/ListLayoutController.js b/src/javascripts/ng-admin/Crud/list/ListLayoutController.js index d029d582..3f907222 100644 --- a/src/javascripts/ng-admin/Crud/list/ListLayoutController.js +++ b/src/javascripts/ng-admin/Crud/list/ListLayoutController.js @@ -1,3 +1,5 @@ +/* globals _ */ + var ListLayoutController = function ($scope, $stateParams, $state, $location, $timeout, view, dataStore) { this.$scope = $scope; this.$state = $state; @@ -9,7 +11,8 @@ var ListLayoutController = function ($scope, $stateParams, $state, $location, $t this.actions = view.actions(); this.batchActions = view.batchActions(); this.loadingPage = false; - this.search = $location.search().search ? JSON.parse($location.search().search) : {}; + this.filters = view.filters(); + this.search = getCurrentSearchParam($location, this.filters); // since search isn't a $stateParam of the listLayout state, // the controller doesn't change when the search changes // so we must update filter values manually when the location changes @@ -17,7 +20,7 @@ var ListLayoutController = function ($scope, $stateParams, $state, $location, $t () => $location.search() && $location.search().search , (newval, oldval) => { if (newval === oldval) return; - this.search = $location.search().search ? JSON.parse($location.search().search) : {}; + this.search = getCurrentSearchParam($location, this.filters); this.enabledFilters = this.getEnabledFilters(); } ); @@ -46,25 +49,35 @@ var ListLayoutController = function ($scope, $stateParams, $state, $location, $t $scope.$on('$destroy', this.destroy.bind(this)); }; +function getCurrentSearchParam(location, filters) { + let search = location.search().search ? JSON.parse(location.search().search) : {}; + filters.map(filter => { + if (search[filter.name()]) { + search[filter.name()] = filter.getMappedValue(search[filter.name()]); + } + }); + return search; +} + ListLayoutController.prototype.enableFilter = function (filter) { let defaultValue = filter.defaultValue(); if (defaultValue !== null) { this.search[filter.name()] = defaultValue; } - this.enabledFilters.push(filter) + this.enabledFilters.push(filter); this.focusedFilterId = filter.name(); this.$timeout(() => { let el = window.document.getElementById(this.focusedFilterId); - el && el.focus && el.focus(); + if (el && el.focus) el.focus(); }, 200, false); -} +}; ListLayoutController.prototype.getEnabledFilters = function () { return this.filters.filter(filter => { if (filter.pinned()) return true; - return this.search && (filter.name() in this.search) + return this.search && (filter.name() in this.search); }); -} +}; ListLayoutController.prototype.updateFilters = function () { var values = {}, @@ -82,7 +95,7 @@ ListLayoutController.prototype.updateFilters = function () { if ((field.type() === 'boolean' && this.search[fieldName]) || // for boolean false is the same as null (field.type() !== 'boolean' && this.search[fieldName] !== null)) { - values[fieldName] = this.search[fieldName]; + values[fieldName] = field.getTransformedValue(this.search[fieldName]); } } this.$stateParams.search = values;