magentofiltergridadminhtml

Grid column filter not working with two models in magento custom module


I'm working with custom module for the magento backend, in this filter not working when using filter calback! Can anyone suggest me? Thanks!

I have tried some codes like this,

Grid.php

protected function _prepareCollection()
    {
     $collection = Mage::getModel('listings/listings')->getCollection();
     $this->setCollection($collection);
       return parent::_prepareCollection();
    }
protected function _prepareColumns()
    {     
        $this->addColumn("item_id", array(
            "header" => Mage::helper("linkmanagement")->__("ID"),
            "align" => "center",
            "type" => "number",
            "index" => "item_id",

        ));

        $this->addColumn("title", array(
            "header" => Mage::helper("linkmanagement")->__("Title"),
            "index" => "title"
        ));

        $this->addColumn("cat_ids", array(
            "header" => Mage::helper("linkmanagement")->__("Cat ID"),
            "align" => "center",
            "index" => "cat_ids",
            "renderer" => 'Sathish_Linkmanagement_Block_Adminhtml_Linkmanagement_Renderer_Categories',
            'filter_condition_callback' => array($this, '_categoriesFilter')
        ));

        $this->addColumn("url_key", array(
            "header" => Mage::helper("linkmanagement")->__("URL"),
            "index" => "url_key",
            "width" => "200px",
        ));

        $this->addColumn('status',
            array(
                'header' => Mage::helper('linkmanagement')->__('Status'),
                'index' => 'status',
                'type' => 'options',
                'options' => array('1' => Mage::helper('linkmanagement')->__('Active'),
                    '0' => Mage::helper('linkmanagement')->__('Inactive')),
            )
        );

        return parent::_prepareColumns();
    }

callback function

protected function _categoriesFilter($collection, $column)
    {
        if (!$value = $column->getFilter()->getValue()) {
            return $this;
        }

        $this->getCollection()->getSelect()->where(
            "cat_ids ?"
            , "%$value%");
        return $this;
    }

Categories.php

class Sathish_Linkmanagement_Block_Adminhtml_Linkmanagement_Renderer_Categories extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{

    public function render(Varien_Object $row)
    {
        $value =  $row->getData($this->getColumn()->getIndex());
        // $value = 38,92
        $value = explode(',',$value);
        $collection = Mage::getModel("categories/categories")->getCollection()->addFieldToFilter('category_id',$value)->getData();
        foreach($collection as $col){
            $result[] = $col['title'];
        }
        $result = implode(',', $result);// $result = schools,colleges
        return $result;
    }
}

Note: Renderer working fine., only filter callback function is not working!!!


Solution

  • I checked your example with my own renderer and tried to check your callback.

    Replace in your callback method

    $this->getCollection()->getSelect()
    

    with

    $collection
    

    Here are my pieces of advice:


    Magento filter assumes not 100% similarity. I mean that if you want to apply filter you should use construction:

    where("cat_ids LIKE ?", "%$value%");
    

    I still don't understand answer on question below:

    Define not working. Is the callback ever being called? Is the callback code having no affect on the results? – Lee Saferite 1 hour ago

    Put Mage::log('blabla', false, 'grid.log', true); at the beginning of the callback method. Then check this log file. If it's not empty - your method calls successfully.


    If your method calls - try to

    Mage::log($collection->getSelectSQL(1), false, 'grid.log', true);
    

    before and after filter applying. And try to run these queries in phpmyadmin. Check the result


    Try to apply these changes inside Mage_Adminhtml_Block_Sales_Order_Grid class

    Here what I did:

        $this->addColumn("cat_ids", array(
            "header" => Mage::helper('sales')->__('Ship to Name'),
            "align" => "center",
            "index" => "grand_total",
            "renderer" => "My_Class_.....",
            "filter_condition_callback" => array($this, '_categoriesFilter')
        ));
    

    ....

    protected function _categoriesFilter($collection, $column)
    {
        if (!$value = $column->getFilter()->getValue()) {
            return $this;
        }
    
        $collection->getSelect()->where(
            "status LIKE ?", "%$value%"
        );
    
        return $this;
    }
    

    ...

    public function render(Varien_Object $row)
    {
        $value =  $row->getData('increment_id') . $row->getData('status');
        return $value;
    }