Friday, May 17, 2013

magento display multi select attribute in admin grid with filter



I have created a custom module by name supplier , and displayed several supplier on the product page in admin like in the image above, Now the multiselected values were not appearing properly in the grid page.

It was appearing empty for multiselected values. although i managed to display single selected values properly by the post:

http://blog.chapagain.com.np/magento-how-to-search-or-filter-by-multiselect-attribute-in-admin-grid/

But still there was problem with multi selected values, so i defined "renderer"
$this->addColumn('supplier', array(
            'header'    => Mage::helper('catalog')->__('supplier'),
            'width'     => '180px',
            'index'     => 'supplier',
            'type'  => 'options',
            'options' => $item_types,
             'filter_condition_callback' => array($this, '_filterSupplierCallback'),
            'renderer'=>new Mage_Adminhtml_Block_Catalog_Product_Columns_Supplier()        ));

IN THE RENDERER I USED BELOW CODE TO RETURN THE LIST OF MULTI SELECTED SUPPLIER:

public function render(Varien_Object $row)
    {
        $suppliers=explode(',',$row->getSupplier());
        $suppstr="";
        if(count($suppliers)>0)
        {
            $suppstr="<ul>";
            foreach($suppliers as $value)
            {
                $productModel = Mage::getModel('catalog/product');
                $attr = $productModel->getResource()->getAttribute("supplier");
                if ($attr->usesSource()) {
                $suppstr   .= "<li>".$attr->getSource()->getOptionText("$value")."</li>";
                }

            }
            $suppstr   .= "</ul>";
  
        }
          return $suppstr;   
       
    }



And successfully displayed multiselected options in the grid.

1 comment: