New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement "Lookup" form field decorator #550

Open
romaninsh opened this Issue Aug 27, 2018 · 2 comments

Comments

Projects
None yet
4 participants
@romaninsh
Copy link
Member

romaninsh commented Aug 27, 2018

This is a replacement for AutoComplete decorator which adds a pretty cool support for filtering.

Basic Usage:

$lookup = $form->add('city_id', ['Lookup']);
$lookup->setModel(new City($app->db));
$lookup->addFilter('country');

Would display this:

screen shot 2018-08-27 at 19 30 48

We assume that City model has 'country' field, which could be imported:

// in City Model
$this->hasOne('country_id', new Country())->addTitle();

Multiple Filters

It's possible to define multiple filters for a lookup:

$lookup = $form->add('country_id', ['Lookup']);
$lookup->setModel(new Country($app->db));
$lookup->addFilter('currency');
$lookup->addFilter('is_eu', 'EU?');  // second argument is label
$lookup->addFilter('language');

screen shot 2018-08-27 at 19 33 43

Filters will filter themselves out too. If you select "Yes" under EU, then you will only see Languages from the EU countries. Filters will also have option 'All' on top of them to reset a filter.

Quirk: selecting filters will only affect drop-down options. They will not reset current values. If you select "Thailand" and after that select 'Is EU' this will not reset "Thailand". That is so that implementation is simple and quick.

Further enhancements

Non-title field

When calling setModel, the second argument should be able to specify field to display in the drop-down:

$lookup->setModel(new Country($app->db), '[name]');

Counts

When displaying drop-down values, display number of results for each value, e.g.:

Is EU ▼
  [ Yes (28) ]
  [ No (167) ]
   ¯¯¯¯¯¯¯¯¯¯

Callbacks

Specify manual condition setting callback:

$lookup->addFilter('gdp', 'GDP', [
'> 10000' => function($m) { $m->addCondition('gdp', '>', 10000; },
'< 10000' => function($m) { $m->addCondition('gdp', '<', 10000; },
]);

Calllback second argument

When specifying callback, second argument should contain selected value:

$yr = function($m, $v) {
  $m->addCondition($m->expr('year([date]) = []', [$v]));
}
$lookup->addFilter('date', 'Year', [ '2000'=>$yr, '2001'=>$yr ]);
$lookup->addFilter('date', 'Month', [ 'Jan'=>$m, ... ]);

This implies that we can have multiple filters with same field.

@skondakov

This comment has been minimized.

Copy link
Contributor

skondakov commented Aug 28, 2018

This looks an interesting addon. I did not have the use for such component yet. I did integrate filtering in our ECommerce platform, but customers prefer the filtering to be done with checkboxes and do they are able to filter on more than one entry from filter category.

But this seems helpful for some basic filtering though.

@gowrav-vishwakarma

This comment has been minimized.

Copy link
Member

gowrav-vishwakarma commented Aug 28, 2018

Looks really helpful !!!

@ibelar ibelar referenced this issue Aug 29, 2018

Merged

Feature/lookup field #551

3 of 4 tasks complete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment