diff --git a/examples/blog/config.js b/examples/blog/config.js index 842a9291..cd802b99 100644 --- a/examples/blog/config.js +++ b/examples/blog/config.js @@ -66,6 +66,13 @@ var tag = nga.entity('tags') .readOnly(); // a readOnly entity has disabled creation, edition, and deletion views + var subCategories = [ + { category: 'tech', label: 'Computers', value: 'computers' }, + { category: 'tech', label: 'Gadgets', value: 'gadgets' }, + { category: 'lifestyle', label: 'Travel', value: 'travel' }, + { category: 'lifestyle', label: 'Fitness', value: 'fitness' } + ]; + // set the application entities admin .addEntity(post) @@ -87,6 +94,13 @@ .targetEntity(tag) // the tag entity is defined later in this file .targetField(nga.field('name')) // the field to be displayed in this list ]) + .filters([ + nga.field('category', 'choice').choices([ + { label: 'Tech', value: 'tech' }, + { label: 'Lifestyle', value: 'lifestyle' } + ]).label('Category'), + nga.field('subcategory', 'choice').choices(subCategories).label('Subcategory') + ]) .listActions(['show', 'edit', 'delete']); post.creationView() @@ -99,13 +113,6 @@ nga.field('published_at', 'date') // Date field type translates to a datepicker ]); - var subCategories = [ - { category: 'tech', label: 'Computers', value: 'computers' }, - { category: 'tech', label: 'Gadgets', value: 'gadgets' }, - { category: 'lifestyle', label: 'Travel', value: 'travel' }, - { category: 'lifestyle', label: 'Fitness', value: 'fitness' } - ]; - post.editionView() .title('Edit post "{{ entry.values.title }}"') // title() accepts a template string, which has access to the entry .actions(['list', 'show', 'delete']) // choose which buttons appear in the top action bar. Show is disabled by default @@ -119,7 +126,7 @@ nga.field('subcategory', 'choice') .choices(function(entry) { // choices also accepts a function to return a list of choices based on the current entry return subCategories.filter(function (c) { - return c.category === entry.values.category + return c.category === entry.values.category; }); }), nga.field('tags', 'reference_many') // ReferenceMany translates to a select multiple diff --git a/src/javascripts/ng-admin/Crud/field/maChoiceField.js b/src/javascripts/ng-admin/Crud/field/maChoiceField.js index 4478c613..4707c158 100644 --- a/src/javascripts/ng-admin/Crud/field/maChoiceField.js +++ b/src/javascripts/ng-admin/Crud/field/maChoiceField.js @@ -27,6 +27,20 @@ function maChoiceField($compile) { } }); + scope.$watch('field()', function (field) { + if(!field.choices) { + return; + } + var choices; + if (typeof field.choices === 'function') { + choices = field.choices(); + } + if(typeof choices === 'function') { + choices = choices(scope.entry); + } + scope.choices = choices; + }); + var refreshAttributes = ''; if (field.type().indexOf('reference') === 0 && field.remoteComplete()) { scope.refreshDelay = field.remoteCompleteOptions().refreshDelay; diff --git a/src/javascripts/ng-admin/Crud/filter/maFilter.js b/src/javascripts/ng-admin/Crud/filter/maFilter.js index bcbee482..45f124d9 100644 --- a/src/javascripts/ng-admin/Crud/filter/maFilter.js +++ b/src/javascripts/ng-admin/Crud/filter/maFilter.js @@ -11,7 +11,7 @@ function maFilterDirective(FieldViewConfiguration) { var template = `
-
+
diff --git a/src/javascripts/test/e2e/filterViewSpec.js b/src/javascripts/test/e2e/filterViewSpec.js index 20bb955a..330b72ae 100644 --- a/src/javascripts/test/e2e/filterViewSpec.js +++ b/src/javascripts/test/e2e/filterViewSpec.js @@ -148,6 +148,58 @@ describe('List filter', function () { }); }); + describe('filter choice conflict', function () { + + beforeEach(function() { + if (hasToLoad) { + browser.get(browser.baseUrl + '#/posts/list'); + } + hasToLoad = true; + }); + + var addFilter = function (name) { + return function () { + return element(by.css('ma-filter-button')).click() + .then(function () { + return element(by.linkText(name)).click(); + }); + }; + }; + + var removeFilter = function (name) { + return function () { + return element(by.css('.filter.' + name + ' .remove_filter')).click(); + }; + }; + + var getChoices = function (name) { + return function () { + return element(by.css('.filter.' + name)).click() + .then(function () { + return element.all(by.css('.filter.' + name + ' .ui-select-choices-row')).getText(); + }); + }; + }; + + it('should not mix filter choices when adding two filter and then deleting the first', function () { + addFilter('Category')() + .then(getChoices('category')) + .then(function(categoryChoices) { + return expect(categoryChoices).toEqual(['Tech', 'Lifestyle']); + }) + .then(addFilter('Subcategory')) + .then(getChoices('subcategory')) + .then(function(subCategoryChoices) { + return expect(subCategoryChoices).toEqual(['Computers', 'Gadgets', 'Travel', 'Fitness']); + }) + .then(removeFilter('category')) + .then(getChoices('subcategory')) + .then(function (subCategoryChoices) { + return expect(subCategoryChoices).toEqual(['Computers', 'Gadgets', 'Travel', 'Fitness']); + }); + }); + }); + describe('interaction with pagination', function() { it('should reset page number', function () { // Filter globally for 'I'