3

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.

1 Answer 1

4

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

2
  • I found error: Notice: Undefined index: in /var/www/html/demo/app/code/Magecoder/Customerref/Ui/Component/Listing/Column/Productid.php on line 62 Commented Dec 29, 2016 at 12:14
  • I have tried above code but i full error $product->getName() = null unable to get productName in backend in magento2
    – Unknown
    Commented Mar 19, 2019 at 9:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.