Skip to content
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

Allow Sonata Autocomplete to use advanced filters #6300

Closed
fliespl opened this issue Aug 20, 2020 · 6 comments
Closed

Allow Sonata Autocomplete to use advanced filters #6300

fliespl opened this issue Aug 20, 2020 · 6 comments
Labels

Comments

@fliespl
Copy link

fliespl commented Aug 20, 2020

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

@kirya-dev
Copy link
Contributor

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).

@fliespl
Copy link
Author

fliespl commented Aug 23, 2020

@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?

@kirya-dev
Copy link
Contributor

kirya-dev commented Aug 23, 2020

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

@fliespl
Copy link
Author

fliespl commented Aug 24, 2020

@kirya-dev great idea 🥇

@kirya-dev
Copy link
Contributor

Close issue?

@fliespl fliespl closed this as completed Aug 24, 2020
@jordisala1991
Copy link
Member

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants