0

I added product name column in my module ui grid. In my database, it's save product id.

I display successfully product name in column by product id. But, filter not working when I search by product name.

I just want to add custom field in collection when I do filter.

Current grid collection array :

Array
(
    [0] => Array
        (
            [id] => 1
            [rule_title] => {"1":"Rule 1","2":"Rule 1"}
            [status] => 1
        )

    [1] => Array
        (
            [id] => 2
            [rule_title] => {"1":"Rule 2","2":"Rule 2"}
            [status] => 1
        )
)

Want array like this :

Array
(
    [0] => Array
        (
            [id] => 1
            [rule_title] => {"1":"Rule 1","2":"Rule 1"}
            [status] => 1
            [rule_name] => Rule 1
        )

    [1] => Array
        (
            [id] => 2
            [rule_title] => {"1":"Rule 2","2":"Rule 2"}
            [status] => 1
            [rule_name] => Rule 2
        )
)

How to do that?

11
  • can you please add your code in question. Nov 20, 2019 at 8:41
  • I tried to follow this code : magento.stackexchange.com/a/226087/77631 Nov 20, 2019 at 8:43
  • But, where should I need to use _renderFiltersBefore() this function? I added in app/code/Vendor/Module/Model/ResourceModel/Page/Collection.php file. Nov 20, 2019 at 8:44
  • yes, you can write in the model and if possible create a Separate Grid collection file and add filter there. Nov 20, 2019 at 8:54
  • But, I don't know how to do that? can you please help me? Nov 20, 2019 at 8:54

1 Answer 1

2

I also face same issue and solved by this below code :

app/code/Vendor/Module/Model/ResourceModel/Page/Grid/Collection.php

protected function _renderFiltersBefore() {
    $filters = $this->request->getParam('filters');
    if (isset($filters['your_field'])) {
        $match = sprintf('%%%s%%', substr($this->serializer->serialize(['your key' => $filters['your_field']]), 1, -1));
        $this->addFieldToFilter('your_field', ['like' => $match]);
    }
    parent::_renderFiltersBefore();
}

You need to inject this below class in your construct of Collection.php file

\Magento\Framework\App\RequestInterface $request
\Magento\Framework\Serialize\Serializer\Json $serializer

Your data is like :

{"1":"Rule 1","2":"Rule 1"}

Then, you can do like this

$match = sprintf('%%%s%%', substr($this->serializer->serialize(['1' => "Rule 1"]), 1, -1));

Hope, It will helpful for you.

1
  • Thanks.. !! It's working like charm.. !! Nov 20, 2019 at 11:58

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.