1

How to use filter condition call back in Magento 2 in PHP Grid as we where using in Magento 1 previously like :

'filter_condition_callback' => array($this, '_customFilterCallBack')
2

Try this:

$this->addColumn(
    'position',
    [
        'header' => __({{label}}),
        'name' => 'position',
        'type' => {{field type}},                
        'index' => 'position',               
        'filter_condition_callback' => [$this, '_customFilterCallBack']
    ]
);   



protected function _customFilterCallBack($collection, $column)
{
//Put your logic here..!!

}
1

The best example you can find for filter call back is in magento core

you can see here magento uses filter call back vendor/magento/module-cms/Block/Adminhtml/Page/Grid.php

I can shortly give you what code is there and you can see rest of the things in that file

protected function _prepareColumns()
{
    $this->addColumn(
        'store_id',
        [
            'header' => __('Store View'),
            'index' => 'store_id',
            'type' => 'store',
            'store_all' => true,
            'store_view' => true,
            'sortable' => false,
            'filter_condition_callback' => [$this, '_filterStoreCondition']
        ]
    );
}

protected function _filterStoreCondition($collection, \Magento\Framework\DataObject $column)
{
    if (!($value = $column->getFilter()->getValue())) {
        return;
    }

    $this->getCollection()->addStoreFilter($value);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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