Magento – How to Filter Admin Grid by Store Views

adminfiltergridmagento-1.9store-view

I have a grid with custom collection.Store view filter is not working in my grid. Here is my code

<?php

class Company_Banner_Block_Adminhtml_Homepage_Grid extends Mage_Adminhtml_Block_Widget_Grid {

    public function __construct() {       
        parent::__construct();
        $this->setId("homepageGrid");
        $this->setDefaultSort("order");
        $this->setDefaultDir("DESC");
        $this->setSaveParametersInSession(true);
    }

    public function initForm()
    {
        return $this;
    }

    protected function _prepareCollection()
    {
        $collection = Mage::getModel("company_banner/homepage")
            ->getCollection();

        $collection->getSelect();

        foreach ($collection as $view) {
            if ( $view->getStores() && $view->getStores() != 0 ) {
                $view->setStores(explode(',',$view->getStores()));
            } else {
                $view->setStores(array('0'));
            }
        }
        $this->setCollection($collection);
        return parent::_prepareCollection();
    }

    protected function _prepareColumns()
    {
        $this->addColumn("entity_id", array(
            "header" => Mage::helper("company_banner")->__("ID"),
            "align" => "right",
            "width" => "50px",
            "type" => "number",
            "index" => "entity_id",
        ))

        ->addColumn("title", array(
            "header" => Mage::helper("company_banner")->__("Title"),
            "align" => "left",
            "type" => "text",
            "index" => "title",
        ))
        ->addColumn("subtitle", array(
            "header" => Mage::helper("company_banner")->__("Subtitle"),
            "align" => "left",
            "type" => "text",
            "index" => "subtitle",
        ));


        $this->addColumn('store_id', array(
            'header'        => Mage::helper('company_banner')->__('Store View'),
            'index'         => 'stores',
            'type'          => 'store',
            'store_all'     => true,
            'store_view'    => true,
            'sortable'      => false,
            'filter_condition_callback' => array($this, '_filterStoreCondition'),

        ));



        return parent::_prepareColumns();
    }



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

         $this->getCollection()->addFieldToFilter('stores', array('finset' => $value));
    }

}

Filter is working,When I remove the following section from the _prepareCollection function. Now the All store Views value is blank.

foreach ($collection as $view) {
            if ( $view->getStores() && $view->getStores() != 0 ) {
                $view->setStores(explode(',',$view->getStores()));
            } else {
                $view->setStores(array('0'));
            }
        }

please check the screenshot http://prntscr.com/dpc49y

Best Answer

Reference: How to add the store view to my module admin grid

for store grind column use below code. store is database coulmn name

 $this->addColumn('store', array( 
                    'header' => 'Website', 
                    'index' => 'store', 
                    'type' => 'store', 
                    'width' => '100px', 
                    'store_view'=> true, 
                    'display_deleted' => false, 
                    'renderer' => 'Namespace_Modulename_Block_Adminhtml_Store',
                    )); 

for rendering the stores

<?php 

class Namespcae_Modulename_Block_Adminhtml_Store
    extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
    public function render(Varien_Object $row) 
    {    $store=explode(',',$row->getStore());

            $data="";
         if($row->getStore()!="" and $row->getStore()==0)
         { 
            $allstore=Mage::app()->getStores();
            foreach($allstore as $astore)
            {
                $data.=$astore->getName().'<br />';
            }

             } else {
                $data="";
                $a=0;
                foreach ($store as $sto)
                {
                     $data= $data.Mage::getModel('core/store')->load($sto[$a])->getName().'<br>';
                 $a+1;}
             }

        return $data;
    }
} 
Related Topic