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
for rendering the stores