3

In this example

<argument name="data" xsi:type="array">
    <item name="config" xsi:type="array">
        <item name="update_url" xsi:type="url" path="mui/index/render"/>
        <item name="filter_url_params" xsi:type="array">
            <item name="status" xsi:type="string">*</item>
        </item>
    </item>
</argument>

How to filter uicomponent grid with multiple url param values ? Like 1,2 not all the values

1
  • try to add in components xml file <filterUrlParams> <param name="custom_value">*</param> </filterUrlParams>
    – Vishnunath
    Commented Jun 18, 2018 at 16:39

1 Answer 1

3

If you have a requirement as load an admin grid with pre-defined multiple filter values like below:

<argument name="data" xsi:type="array">
    <item name="config" xsi:type="array">
        <item name="update_url" xsi:type="url" path="mui/index/render"/>
        <item name="filter_url_params" xsi:type="array">
            <item name="status" xsi:type="string">1,2</item>
        </item>
    </item>
</argument>

Then you can go in the dataprovider class of the uicomponent grid and make below changes(add below prepareUpdateUrl function code):

protected function prepareUpdateUrl() {
    if (!isset($this->data['config']['filter_url_params'])) {
        return;
    }
    foreach ($this->data['config']['filter_url_params'] as $paramName => $paramValue) {
        if ('*' == $paramValue) {
            $paramValue = $this->request->getParam($paramName);
        }
        if ($paramValue) {
            $this->data['config']['update_url'] = sprintf(
                    '%s%s/%s', $this->data['config']['update_url'], $paramName, $paramValue
            );
            if ($paramName == 'status') {
                $paramValue = explode(',', $paramValue);
                $this->addFilter(
                        $this->filterBuilder->setField($paramName)->setValue($paramValue)->setConditionType('in')->create()
                );
            } else {
                $this->addFilter(
                        $this->filterBuilder->setField($paramName)->setValue($paramValue)->setConditionType('eq')->create()
                );
            }
        }
    }
}

In above code, I am checking paramName as if ($paramName == 'status') then explode this paramValue as $paramValue = explode(',', $paramValue); and adding a new filter as below:

$this->addFilter($this->filterBuilder->setField($paramName)->setValue($paramValue)->setConditionType('in')->create());

And changing the filter condition from eq to in. This solution is very well tested and working live in one of my module, so let me know if you face any issue. Thanks :)

If this scenario didn't work for you. Try this: Use preference to override Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider

Now add this function in your file:

protected function prepareUpdateUrl()
    {
        if (!isset($this->data['config']['filter_url_params'])) {
            return;
        }
        foreach ($this->data['config']['filter_url_params'] as $paramName => $paramValue) {
            if ('*' == $paramValue) {
                $paramValue = $this->request->getParam($paramName);
            }
            if ($paramValue) {
                $this->data['config']['update_url'] = sprintf(
                    '%s%s/%s/',
                    $this->data['config']['update_url'],
                    $paramName,
                    $paramValue
                );

                if ($this->getName() == 'your_data_source_name') {
                    $paramValue = explode(',', $paramValue);
                    $this->addFilter(
                        $this->filterBuilder->setField($paramName)->setValue($paramValue)->setConditionType('in')->create()
                    );
                } else {
                    $this->addFilter(
                        $this->filterBuilder->setField($paramName)->setValue($paramValue)->setConditionType('eq')->create()
                    );
                }
            }
        }
    }

I think this will be effective and we can add multiple conditions in it based on the requirement! Thanks!

7
  • Where to add prepareUpdateUrl() function? Commented Dec 24, 2019 at 6:51
  • add in your dataprovider class of the uicomponent grid, if you are using default one in the uicomponent grid, then create your own dataprovider by extending the default one and extend the prepareUpdateUrl function ...
    – Ashish Raj
    Commented Dec 24, 2019 at 9:11
  • 2
    Can't believe this answer didn't have any votes. It works perfectly.
    – Geat
    Commented Jun 17, 2020 at 19:50
  • 2
    This functionality works fine. Thanks!
    – Jason
    Commented Sep 28, 2022 at 17:08
  • For those who don't know or confused where to use the prepareUpdateUrl method. Use preference to override Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider. Then add that function there. You can differentiate the data source by $this->getName()
    – Jason
    Commented Sep 28, 2022 at 17:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.