Magento 2 – How to Apply Custom Function on Admin Grid Column

adminhtmlgridmagento2uicomponent

I want to apply custom function on grid column.
I tried to apply from adminhtml/ui_component/CustomModule_order_lis.xml also tried to modified Collection.php .

Like below image, I am getting order status (ready_dispatch, etc) from table column, now want apply CheckOrderStatus() function to change css, color, text & other activity on basis of order status.

$this->CheckOrderStatus('ready_dispatch');

<column name="seller_order_confirm">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="sorting" xsi:type="string">asc</item>
                    <item name="label" xsi:type="string" translate="true">Seller Order Status</item>
                    <item name="sortOrder" xsi:type="number">13</item>
                </item>
            </argument>
        </column>

enter image description here

I have already search about this but not found anything regrading same.
Hope this question not duplicate. Thanks in Advance.

Best Answer

Use custom Render

<column name="product_id" class="SalesIgniter\Common\Ui\Component\Listing\Column\Product">
        <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">Product</item>          
            </item>                                                                           
        </argument>                                                                           
    </column>

You can manipulate value in class

namespace SalesIgniter\Common\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;

/**
 * Shows product name in admin grids instead of product id
 */
class Product extends Column
{
    /**
     * Escaper
     *
     * @var \Magento\Framework\Escaper
     */
    protected $escaper;

    /**
     * System store
     *
     * @var SystemStore
     */
    protected $systemStore;

    protected $productFactory;

    /**
     * Constructor
     *
     * @param ContextInterface $context
     * @param UiComponentFactory $uiComponentFactory
     * @param SystemStore $systemStore
     * @param Escaper $escaper
     * @param array $components
     * @param array $data
     */
    public function __construct(
        ContextInterface $context,
        UiComponentFactory $uiComponentFactory,
        Escaper $escaper,
        \Magento\Catalog\Model\ProductFactory $productFactory,
        array $components = [],
        array $data = []
    ) {
        $this->productFactory = $productFactory;
        $this->escaper = $escaper;
        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->productFactory->create()->load((int)$item[$this->getData('name')]);
                $item[$this->getData('name')] = $product->getName();
            }
        }

        return $dataSource;
    }
}

source

Related Topic