How to use dynamic option value from admin grid dropdown filter section in Magento 2

This post will helpful for how to use dynamic option values into admin grid. Basically admin grid script will return the value what is available from database column value based on the index field.
Ex: From below script, index field is having data base table column called “mobile_type“.
In database table consider mobile_type colunm value is 1 and 2 … N.
Here instead of showing 1 and 2 .. N as dropdown option value we can return as the text.

Script: 1
 Refer below is my sample file path.
 File path: Jute\Ecommerce\Block\Adminhtml\Grid.php
 protected function _prepareColumns() {
 $this->addColumn(
 'mobile_type', [
 'header' => __('Mobile Type'),
 'index' => 'mobile_type',
 'width' => '50px',
 'type' => 'options',
 'options' => Mobiletype::getAvailableMobileType(), //dropdown result
 ] );
 }

Script: 2

Below is my renderer class which will convert numeric result into string result. (as per our requirement we can modify)

File path: Jute\Ecommerce\Block\Adminhtml\Mobiletype.php
<?php
 namespace Jute\Ecommerce\Block\Adminhtml\Helper\Renderer;
class Mobiletype extends \Magento\Backend\Block\Widget\Grid\Column\Renderer\AbstractRenderer
 {
protected $_storeManager;
public function __construct(
 \Magento\Backend\Block\Context $context,
 \Magento\Store\Model\StoreManagerInterface $storeManager,
 array $data = []
 ) {
 parent::__construct($context, $data);
 $this->_storeManager = $storeManager;
 }
public function render(\Magento\Framework\DataObject $row) {
 return [
 array("1" => "Mobile 1" , "2" => "Mobile 2")
 ]
 }
 }

Leave a Reply