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 = `