2

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

marked as duplicate by Marius Nov 23 '15 at 11:37

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • This is a duplicate of a question that is younger? – 11mb Mar 24 '16 at 13:07
4

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 – Pradeep Kumar Nov 11 '15 at 5:03
0

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>
  1. 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? – LucScu Apr 29 '16 at 10:17
  • from here github.com/magento/magento2/issues/1650 it seems replaced with different logic. – LucScu Apr 29 '16 at 10:46
  • sales_order_grid_collection is not available in magento2 it seems – saravanavelu Jun 21 '16 at 5:47

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