I want to show store view in my custom module grid and it is showing but there is an issue which I want to remove ie. Multiple store view is not showing in grid. Reference image is given below :
Single store is easily showing on grid.
Here is the grid file code
<?php
namespace Custom\Slider\Block\Adminhtml\Slide;
class Grid extends \Magento\Backend\Block\Widget\Grid\Extended
{
/**
* @var \Magento\Framework\Module\Manager
*/
protected $moduleManager;
/**
* @var \Custom\Slider\Model\slideFactory
*/
protected $_slideFactory;
/**
* @var \Custom\Slider\Model\Status
*/
protected $_status;
/**
* @param \Magento\Backend\Block\Template\Context $context
* @param \Magento\Backend\Helper\Data $backendHelper
* @param \Custom\Slider\Model\slideFactory $slideFactory
* @param \Custom\Slider\Model\Status $status
* @param \Magento\Framework\Module\Manager $moduleManager
* @param array $data
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Backend\Helper\Data $backendHelper,
\Custom\Slider\Model\SlideFactory $SlideFactory,
\Custom\Slider\Model\Status $status,
\Magento\Store\Model\System\Store $systemStore,
\Magento\Framework\Module\Manager $moduleManager,
array $data = []
) {
$this->_slideFactory = $SlideFactory;
$this->_status = $status;
$this->_systemStore = $systemStore;
$this->moduleManager = $moduleManager;
parent::__construct($context, $backendHelper, $data);
}
/**
* @return void
*/
protected function _construct()
{
parent::_construct();
$this->setId('postGrid');
$this->setDefaultSort('slide_id');
$this->setDefaultDir('DESC');
$this->setSaveParametersInSession(true);
$this->setUseAjax(false);
$this->setVarNameFilter('post_filter');
}
/**
* @return $this
*/
protected function _prepareCollection()
{
$collection = $this->_slideFactory->create()->getCollection();
$this->setCollection($collection);
parent::_prepareCollection();
return $this;
}
/**
* @return $this
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
protected function _prepareColumns()
{
$this->addColumn(
'slide_id',
[
'header' => __('ID'),
'type' => 'number',
'index' => 'slide_id',
'header_css_class' => 'col-id',
'column_css_class' => 'col-id'
]
);
$this->addColumn(
'slide_name',
[
'header' => __('Name'),
'index' => 'slide_name',
]
);
$this->addColumn(
'slide_status',
[
'header' => __('Status'),
'index' => 'slide_status',
'type' => 'options',
'options' => \Custom\Slider\Block\Adminhtml\Slide\Grid::getOptionArray2()
]
);
$this->addColumn(
'slide_url',
[
'header' => __('Url'),
'index' => 'slide_url',
]
);
$this->addColumn(
'slide_store_view',
[
'header' => __('Store View'),
'index' => 'slide_store_view',
'type' => 'store',
'store_all' => true,
'store_view' => true,
'sortable' => true,
//'filter_condition_callback' => [$this, '_filterStoreCondition']
]
);
$this->addColumn(
'slide_order',
[
'header' => __('Slide Short Order'),
'index' => 'slide_order'
]
);
$this->addColumn(
'slide_use_type',
[
'header' => __('Slide Type'),
'index' => 'slide_use_type',
'type' => 'options',
'options' => \Custom\Slider\Block\Adminhtml\Slide\Grid::getOptionArray8()
]
);
$this->addColumn(
'edit',
[
'header' => __('Edit'),
'type' => 'action',
'getter' => 'getId',
'actions' => [
[
'caption' => __('Edit'),
'url' => [
'base' => '*/*/edit'
],
'field' => 'slide_id'
]
],
'filter' => false,
'sortable' => false,
'index' => 'stores',
'header_css_class' => 'col-action',
'column_css_class' => 'col-action'
]
);
$this->addExportType($this->getUrl('slider/*/exportCsv', ['_current' => true]),__('CSV'));
$this->addExportType($this->getUrl('slider/*/exportExcel', ['_current' => true]),__('Excel XML'));
$block = $this->getLayout()->getBlock('grid.bottom.links');
if ($block) {
$this->setChild('grid.bottom.links', $block);
}
return parent::_prepareColumns();
}
/**
* @return $this
*/
protected function _prepareMassaction()
{
$this->setMassactionIdField('slide_id');
//$this->getMassactionBlock()->setTemplate('Custom_Slider::slide/grid/massaction_extended.phtml');
$this->getMassactionBlock()->setFormFieldName('slide');
$this->getMassactionBlock()->addItem(
'delete',
[
'label' => __('Delete'),
'url' => $this->getUrl('slider/*/massDelete'),
'confirm' => __('Are you sure?')
]
);
$statuses = $this->_status->getOptionArray();
$this->getMassactionBlock()->addItem(
'status',
[
'label' => __('Change status'),
'url' => $this->getUrl('slider/*/massStatus', ['_current' => true]),
'additional' => [
'visibility' => [
'name' => 'status',
'type' => 'select',
'class' => 'required-entry',
'label' => __('Status'),
'values' => $statuses
]
]
]
);
return $this;
}
/**
* @return string
*/
protected function _filterStoreCondition($collection, \Magento\Framework\DataObject $column)
{
if (!($value = $column->getFilter()->getValue())) {
return;
}
$this->getCollection()->addStoreFilter($value);
}
/**
* @return string
*/
public function getGridUrl()
{
return $this->getUrl('slider/*/index', ['_current' => true]);
}
/**
* @param \Custom\Slider\Model\slide|\Magento\Framework\Object $row
* @return string
*/
public function getRowUrl($row)
{
return $this->getUrl(
'slider/*/edit',
['slide_id' => $row->getId()]
);
}
static public function getOptionArray2()
{
$data_array=array();
$data_array[0]='Disabled';
$data_array[1]='Enabled';
return($data_array);
}
static public function getValueArray2()
{
$data_array=array();
foreach(\Custom\Slider\Block\Adminhtml\Slide\Grid::getOptionArray2() as $k=>$v){
$data_array[]=array('value'=>$k,'label'=>$v);
}
return($data_array);
}
static public function getOptionArray8()
{
$data_array=array();
$data_array['normal']='Normal';
$data_array['hotspot']='Hotspot';
$data_array['video']='Video';
return($data_array);
}
static public function getValueArray8()
{
$data_array=array();
foreach(\Custom\Slider\Block\Adminhtml\Slide\Grid::getOptionArray8() as $k=>$v){
$data_array[]=array('value'=>$k,'label'=>$v);
}
return($data_array);
}
}
Best Answer
I have resolved this issue ( Show multi-store view on grid) by using the renderer.
Output :
For filter
I have added
_filterStoreCondition
method as defined in callbackfilter_condition_callback
.Source : A2bizz blog