Skip to content

Commit 6038c23

Browse files
committed
Add permanent filters
Closes #548
1 parent 86b5260 commit 6038c23

File tree

4 files changed

+72
-20
lines changed

4 files changed

+72
-20
lines changed

README.md

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -346,25 +346,39 @@ Enable or disable lazy loading.
346346
* `filters()[field1, field2, ...])`
347347
Add filters to the list. Each field maps a property in the API endpoint result.
348348

349-
listView.filters([
349+
customers.listView().filters([
350350
nga.field('first_name'),
351351
nga.field('last_name'),
352352
nga.field('age', 'number')
353353
]);
354354

355-
Filters appear when the user clicks on the "Add filter" button at the top of the list. You can also set a filter field as "pinned", to be sure it's always displayed.
355+
Filters appear when the user clicks on the "Add filter" button at the top of the list. Once the user fills the filter widgets, the list is immediately refreshed based on the filter values, with unerlying API requests looking like:
356+
357+
GET /customers?first_name=XXX&last_name=XXX&age=XXX
358+
359+
You can also set a filter field as "pinned", to make it always visible.
356360

357361
listView.filters([
358362
nga.field('q').label('Search').pinned(true)
359363
]);
360364

361-
Filter fields can be of any type, including `reference` and `template`. this allows to define custom filters with ease.
365+
Filter fields can be of any type, including `reference` and `template`. This allows to define custom filters with ease.
362366

363367
listView.filters([
364368
nga.field('q', 'template').label('')
365369
.template('<div class="input-group"><input type="text" ng-model="value" placeholder="Search" class="form-control"></input><span class="input-group-addon"><i class="glyphicon glyphicon-search"></i></span></div>'),
366370
]);
367371

372+
Note that you can use `map()` and `transform()` on filter fields (see [General Field Settings](#general-field-settings)).
373+
374+
* `permanentFilters({ field1: value, field2: value, ...})`
375+
Add permanent filters to the results list.
376+
377+
posts.listView().permanentFilters({
378+
published: true
379+
});
380+
// calls to the API will be GET /posts?published=true
381+
368382
* `listActions(String|Array)`
369383
Add an action column with action buttons on each line. You can pass a list of button names among 'show', 'edit', and 'delete'.
370384

@@ -745,15 +759,29 @@ Set the default field for list sorting. Defaults to 'id'
745759
* `sortDir(String)`
746760
Set the default direction for list sorting. Defaults to 'DESC'
747761

748-
* `filters({ field1: value, field2: value, ...})`
749-
Add filters to the referenced results list. It may be either an object or a function with a single parameter: the current search string.
762+
* `permanentFilters({ field1: value, field2: value, ...})`
763+
Add filters to the referenced results list. This can be very useful to restrict the list of possible values displayed in a dropdown list:
750764

751-
myView.fields([
765+
comments.editionView().fields([
766+
nga.field('id'),
752767
nga.field('post_id', 'reference')
753-
.targetEntity(post) // Select a target Entity
754-
.targetField(nga.field('title')) // Select a label Field
755-
.filters(function(search) {
756-
// will send `GET /posts?title=foo%` query
768+
.targetEntity(post)
769+
.targetField(nga.field('title'))
770+
.permanentFilters({
771+
published: true
772+
});
773+
]);
774+
775+
The parameter can be be either an object or a function with a single parameter: the current search string typed by the user in the autocompletion input.
776+
777+
comments.editionView().fields([
778+
nga.field('id'),
779+
nga.field('post_id', 'reference')
780+
.targetEntity(post)
781+
.targetField(nga.field('title'))
782+
.permanentFilters(function(search) {
783+
// when the user types 'foo' in the autocompletion input
784+
// fetch the results as `GET /posts?title=foo%`
757785
return {
758786
title: search + '%'
759787
};

UPGRADE-0.8.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,33 @@ nga.field('last_name')
9696
return value.toUpperCase();
9797
});
9898
```
99+
100+
## `ReferenceField.filters()` has been renamed to `ReferenceField.permanentFilters()`
101+
102+
When displaying a reference widget in the edition view, you can filter the list of possible values displayed in the dropdown using the `filters()` function. In 0.8, this function has been renamed to `permanentFilters()`:
103+
104+
``` diff
105+
nga.entity('comments').fields([
106+
nga.field('id'),
107+
nga.field('post_id', 'reference')
108+
- .filters({ published: true })
109+
+ .permanentFilters({ published: true })
110+
]);
111+
```
112+
113+
Just like the previous `filters()` feature, `permanentFilters()` also accepts a function, receiving the string typed by the user in the autocomplete field:
114+
115+
``` diff
116+
nga.entity('comments').fields([
117+
nga.field('id'),
118+
nga.field('post_id', 'reference')
119+
- .filters(function(search) {
120+
+ .permanentFilters(function(search) {
121+
return search ? { q: search } : null;
122+
});
123+
]);
124+
```
125+
126+
`filters()` will remain available until the next version, although it logs a deprecation warning in the console.
127+
128+
**Tip**: `permanentFilters()` now also works on the `listView`, which allows you to define a pre-filtered datagrid.

examples/blog/config.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@
116116
nga.field('tags', 'reference_many') // ReferenceMany translates to a select multiple
117117
.targetEntity(tag)
118118
.targetField(nga.field('name'))
119-
.filters(function(search) {
119+
.permanentFilters(function(search) {
120120
return search ? { q: search } : null;
121121
})
122122
.remoteComplete(true, { refreshDelay: 300 })
@@ -194,14 +194,8 @@
194194
nga.field('post_id', 'reference')
195195
.label('Post')
196196
.map(truncate)
197-
.filters(function(search) {
198-
if (!search) {
199-
return;
200-
}
201-
202-
return {
203-
q: search // Full-text search
204-
};
197+
.permanentFilters(function(search) {
198+
return search ? { q: search } : null; // Full-text search
205199
})
206200
.targetEntity(post)
207201
.targetField(nga.field('title'))

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"npm": "^2.10.0"
1515
},
1616
"devDependencies": {
17-
"admin-config": "^0.2.7",
17+
"admin-config": "^0.2.10",
1818
"angular": "~1.3.15",
1919
"angular-bootstrap": "^0.12.0",
2020
"angular-mocks": "1.3.14",

0 commit comments

Comments
 (0)