1

Filter & Sorting Not Working for custom column course_category_name in ui_component grid & custom column data render from another table through multiple table joins in Magento 2

Our DataProvider code is given below. If anyone has any solution, please give it to me.

collection = $collectionFactory->create(); $this->categoryCollection = $categoryCollectionFactory->create(); $this->addFieldStrategies = $addFieldStrategies; $this->addFilterStrategies = $addFilterStrategies; $this->localeCurrency = $localeCurrency; $this->storeManager = $storeManager; $this->context = $context; } /** * Get data * * @return array */ public function getData() { if (!$this->getCollection()->isLoaded()) { $this->getCollection()->load(); } $categoryResourceModel = $this->categoryCollection->getResource(); $this->getCollection()->getSelect()->join( ['xcc' => $categoryResourceModel->getTable('xteam_category_courses')], "main_table.course_id = xcc.course_id", ['xcc.course_category_id'] )->join( ['xccat' => $categoryResourceModel->getTable('xteam_course_categories')], 'xcc.course_category_id = xccat.course_category_id', [ 'course_category_id' => new Zend_Db_Expr('GROUP_CONCAT(DISTINCT xcc.course_category_id ORDER BY xcc.course_category_id)'), 'course_category_name' => new Zend_Db_Expr('GROUP_CONCAT(DISTINCT xccat.course_category_name ORDER BY xcc.course_category_id)') ] )->group('main_table.course_id'); $items = $this->getCollection()->getData(); // echo ""; print_r($items->getData()); exit; return [ 'totalRecords' => $this->getCollection()->getSize(), 'items' => array_values($items), ]; } }

1 Answer 1

0

I got the solution for Filter & Sorting for custom column ui_component grid.

We need to add this code in DataProvider File then filter & sorting working perfect.

public function addFilter(\Magento\Framework\Api\Filter $filter)
{
    if (isset($this->addFilterStrategies[$filter->getField()])) {
        $this->addFilterStrategies[$filter->getField()]
            ->addFilter(
                $this->getCollection(),
                $filter->getField(),
                [$filter->getConditionType() =>
                 $filter->getValue()]
            );
    } else {
        parent::addFilter($filter);
    }
}


public function addField($field, $alias = null)
{
    if (isset($this->addFieldStrategies[$field])) {
        $this->addFieldStrategies[$field]->addField(
        $this->getCollection(), $field, $alias);
    } else {
        parent::addField($field, $alias);
    }
}

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.