I need to add new column to sales and customer admin grid in Magento2 and also as all we know it uses UI Component, then how to extend grid with UI as well as non UI ex newsletters modules
3 Answers
To add new column in orders grid you need to specify update for this grid. create a new file Vendor/Module/view/adminhtml/ui_component/sales_order_grid.xml in your module with following content:
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Ui/etc/ui_configuration.xsd">
<columns name="sales_order_columns">
<column name="your_column_name" [class="You may specify your own renderer"]>
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="visible" xsi:type="boolean">false</item>
<item name="label" xsi:type="string" translate="true">Label</item>
</item>
</argument>
</column>
</columns>
but make sure that requested field exists in corresponding dataprovider
-
:- thanks its working fine but i need add custom field after specific column then how to it and also if i need to join my custom table with sales grid table then how to do it Commented Nov 11, 2015 at 5:03
to set position you can use
<item name="sortOrder" xsi:type="number">75</item>
that is
<column name="title">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="filter" xsi:type="string">text</item>
<item name="add_field" xsi:type="boolean">true</item>
<item name="label" xsi:type="string" translate="true">Titles</item>
<item name="sortOrder" xsi:type="number">75</item>
</item>
</argument>
</column>
and one more thing below things has to clear to test else your chnage will not effect.
- some entry will happen in ui_bookmark so empty this table else delete your entry (there will be 2 entry so delete both)
- clear cache event
- it not effecting then clear browser cache als
to add custom filed to core admin grid in UI Component then we need to follow below steps but addColumnAfter is not working correct me where i made mistakes .
1. create a xml file in
app\code\Sugarcode\Test\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="../../../../Ui/etc/ui_configuration.xsd">
<columns name="sales_order_columns">
<column name="title">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="filter" xsi:type="string">text</item>
<item name="add_field" xsi:type="boolean">true</item>
<item name="label" xsi:type="string" translate="true">Titles</item>
<item name="sortOrder" xsi:type="number">75</item>
</item>
</argument>
</column>
</columns>
</listing>
to add custom filed to collection i used events if any better solution please suggest us app\code\Sugarcode\Test\etc\adminhtml\events.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/Event/etc/events.xsd">
<event name="sales_order_grid_collection_load_before">
<observer name="sales_order_grid_test" instance="Sugarcode\Test\Model\Observer" method="addTest" />
</event>
</config>
app\code\Sugarcode\Test\Model\Observer.php
namespace Sugarcode\Test\Model; class Observer { public function addTest(\Magento\Framework\Event\Observer $observer) { $obj=$observer->getEvent()->getOrderGridCollection(); $obj->getSelect()->joinLeft( ['testt' => 'testtable'], "(main_table.entity_id = testt.id)", [ 'testt.title as title' ] ); //$this->printlogquery(true); //return $obj; } }
-
Are you sure that
sales_order_grid_collection_load_before
is a valid dispatched event?– LucScuCommented Apr 29, 2016 at 10:17 -
from here github.com/magento/magento2/issues/1650 it seems replaced with different logic.– LucScuCommented Apr 29, 2016 at 10:46
-
sales_order_grid_collection is not available in magento2 it seems Commented Jun 21, 2016 at 5:47