Today we discussed How to create Magento 2 Create custom renderer in admin grid. Sometime we need get some other values than current values. For example get product name instead of product id. For that we need to create a custom renderer for gird that will show the product name instead of product id so here we start. First we need to add column class that we need to renderer.
Step 1:
Create a ui class of column.
<column name="vendor_id" class="QaisarSatti\HelloWorld\Ui\Component\Listing\Column\Testing">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="filter" xsi:type="string">text</item>
<item name="label" xsi:type="string" translate="true">Vendor</item>
</item>
</argument>
</column>
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="filter" xsi:type="string">text</item>
<item name="label" xsi:type="string" translate="true">Vendor</item>
</item>
</argument>
</column>
Step 2:
Create a renderer file for that particular column.
PME\PoManager\Ui\Component\Listing\Column\Vendor
<?php
namespace QaisarSatti\HelloWorld\Ui\Component\Listing\Column;
use Magento\Framework\Escaper;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Ui\Component\Listing\Columns\Column;
class Testing extends Column
{
protected $escaper;
protected $systemStore;
protected $productloader;
public function __construct(
ContextInterface $context,
UiComponentFactory $uiComponentFactory,
\Magento\Catalog\Model\ProductFactory $productloader,
Escaper $escaper,
array $components = [],
array $data = []
) {
$this->escaper = $escaper;
$this->productloader = $productloader;
parent::__construct($context, $uiComponentFactory, $components, $data);
}
/**
* Prepare Data Source
*
* @param array $dataSource
* @return array
*/
public function prepareDataSource(array $dataSource)
{
if (isset($dataSource['data']['items'])) {
foreach ($dataSource['data']['items'] as & $item) {
$product = $this->productloader>create()->load((int)$item[$this->getData('name')]);
$item[$this->getData('name')] =$product->getName();
}
}
return $dataSource;
}
}
namespace QaisarSatti\HelloWorld\Ui\Component\Listing\Column;
use Magento\Framework\Escaper;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Ui\Component\Listing\Columns\Column;
class Testing extends Column
{
protected $escaper;
protected $systemStore;
protected $productloader;
public function __construct(
ContextInterface $context,
UiComponentFactory $uiComponentFactory,
\Magento\Catalog\Model\ProductFactory $productloader,
Escaper $escaper,
array $components = [],
array $data = []
) {
$this->escaper = $escaper;
$this->productloader = $productloader;
parent::__construct($context, $uiComponentFactory, $components, $data);
}
/**
* Prepare Data Source
*
* @param array $dataSource
* @return array
*/
public function prepareDataSource(array $dataSource)
{
if (isset($dataSource['data']['items'])) {
foreach ($dataSource['data']['items'] as & $item) {
$product = $this->productloader>create()->load((int)$item[$this->getData('name')]);
$item[$this->getData('name')] =$product->getName();
}
}
return $dataSource;
}
}