cancel
Showing results for 
Search instead for 
Did you mean: 

Custom column in admin grid is not showing up

Custom column in admin grid is not showing up

Hi Tech guys,

 

I am new to Magento 2 framework and trying to add custom column is Sales order grid in admin. For that i created sales_order_grid.xml file in mynamespace/module/view/Adminhtml/ui_component and added my column name there and there corresponding php class. And then i created that php class inside mynamespace/module/Ui/component/Listing/Column. Then i compiled the code using 

php bin/magento setup:di:compile and flushed the cache. But still that custom column is not showing up in the admin panel.

 

Please help me if i am missing something??

2 REPLIES 2

Re: Custom column in admin grid is not showing up

Hi @purushottam_kum ,

 

Check Magento_Signifyd module how they add:

 

vendor/magento/module-signifyd/view/adminhtml/ui_component/sales_order_grid.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <columns name="sales_order_columns">
        <column name="signifyd_guarantee_status" component="Magento_Ui/js/grid/columns/select">
            <settings>
                <filter>select</filter>
                <options class="Magento\Signifyd\Ui\Component\Listing\Column\Guarantee\Options"/>
                <visible>true</visible>
                <dataType>select</dataType>
                <label translate="true">Signifyd Guarantee Decision</label>
            </settings>
        </column>
    </columns>
</listing>

The corresponding table is sales_order_grid if you add another table column then need to join with this table.

 

You can check https://magento.stackexchange.com/questions/262770/magento-2-add-billing-and-shipping-company-column...

 

-----
If Issue Solved, Click Kudos and Accept As solutions.
Sohel Rana, 7x Magento 2, 2x Magento 1 Certified

Re: Custom column in admin grid is not showing up

Hi Sohel,

 

Thanks for the response. I did similar to this only. But still it's not working. For showing custom column in sales order grid. only these 2 things is sufficient i guess. Sales_order_grid.xml and new column class under namespace/module/Ui/Component/Listing/Column. Correct me if i am wrong??

<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
*/
-->
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<columns name="sales_order_columns">
<column name="track_device" class="Namespace\Module\Ui\Component\Listing\Column\CustomerTrackDevice">
<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">Customer Tracked Device</item>
</item>
</argument>
</column>
</columns>
</listing>

above is my sales_order_grid.xml file for my module.

and below is my class for new column Custom Track Device

class CustomerTrackDevice extends Column
{
protected $_orderRepository;

public function __construct(ContextInterface $context, UiComponentFactory $uiComponentFactory, OrderRepositoryInterface $orderRepository, array $components = [], array $data = [])
{
$this->_orderRepository = $orderRepository;
parent::__construct($context, $uiComponentFactory, $components, $data);
}

public function prepareDataSource(array $dataSource)
{
if (isset($dataSource['data']['items'])) {
foreach ($dataSource['data']['items'] as & $item) {

$order = $this->_orderRepository->get($item["entity_id"]);
/// $tracked_device = $order->getData("track_device");

// $this->getData('name') returns the name of the column so in this case it would return tracked_device
$item[$this->getData('name')] = 'puru';
}
}

return $dataSource;
}

}