Sonataadminbundle: Allow Sonata Autocomplete to use advanced filters

Created on 20 Aug 2020  路  6Comments  路  Source: sonata-project/SonataAdminBundle

It would be great to add option to pass select advanced filter option with autocomplete ajax requests (get-autocomplete-items).

Currently selected advanced filter is not passed to ajax controller causing it to not filter according to setup.

In this case I have selected (not equals), but it still works as "equals"
https://ss.codeone.pl/ss-2020-08-20_14-09-55-1597925395.png

feature

Most helpful comment

The third example could be a great addition to our docs, wdyt @sonata-project/contributors ? IMO it could fit under the cookbook section. It can also be improved to accept multiple conditions (similar to what you can do with the github search box)

All 6 comments

Hmm, it looks like you don't know how it works. not equals applies to data table results, but not for autocomplete results.

But if you want configure this behavior you need determine callback
image
Also ajax request dont contain selected equal type. Im just use EqualOperatorType::TYPE_NOT_EQUAL in anyway for demonstrate callback usage.

From me: I will try to test this and get a bug: Filter will return all results when filter property is integer (eg. id) and non integer value in searchText (cast problem, maybe needs is_numeric check. if fail must no results).

@kirya-dev you are totally right - I must have had brain damage that day and confused advanced filters with what I had in mind :)

Either way - let me rephrase, what I would like to achieve:

  • have filter called "user"
  • add autocomplete selection of users
  • add additional subfilter to allow me select which property should be searched (username, email, id, hash)

Of course I can achieve the same by adding multiple filters:

  • User by email
  • User by username
  • User by ID

but from usability - it would be better in my opinion to have one filter with ability to select property being searched.

Does this explaination clarifies it?

First solution override callback. So

$datagridMapper->add(
    'user',
    ModelAutocompleteFilter::class,
    ['show_filter' => true,],
    null,
    [
        'property' => 'username',
        'minimum_input_length' => 1,
        'callback' => static function (AdminInterface $admin, string $property, $searchText) {
            $datagrid = $admin->getDatagrid();

            if (is_numeric($searchText)) {
                $property = 'id';
            } elseif (str_contains($searchText, '@')) {
                $property = 'email';
            } else {
                $property = 'username';
            }

            $datagrid->setValue($datagrid->getFilter($property)->getFormName(), null, $searchText);
        }
    ]
);

Second solution is create custom template for autocomplete with radio button to select field for search. Also override callback.

Third solution is use some prefix:
For ex:
email: [email protected]
id: 123
username: kirya-dev

Code:

        $datagridMapper->add(
            'user',
            ModelAutocompleteFilter::class,
            ['show_filter' => true,],
            null,
            [
                'property' => 'username',
                'minimum_input_length' => 1,
                'callback' => static function (AdminInterface $admin, string $property, $searchText) {
                    $datagrid = $admin->getDatagrid();

                    $searchTextParts = explode(':', $searchText);
                    if (count($searchTextParts) === 2 && in_array($searchTextParts[0], ['id', 'email', 'username'])) {
                        [$property, $searchText] = $searchTextParts;
                    }

                    $datagrid->setValue($datagrid->getFilter($property)->getFormName(), null, $searchText);
                }
            ]
        );

Result:
image
image

@kirya-dev great idea 馃

Close issue?

The third example could be a great addition to our docs, wdyt @sonata-project/contributors ? IMO it could fit under the cookbook section. It can also be improved to accept multiple conditions (similar to what you can do with the github search box)

Was this page helpful?
0 / 5 - 0 ratings