Magento 2: Add Grid Column to Customer Admin Order without Adding Column to Database?

I want to add a column to a customer order grid in magento admin dashboard. But the column value is some kind of process, not a database. Is it possible? But how to do it? Thanks in advance.

+3


source to share


2 answers


You can add a column to your admin grid by adding a file named view/adminhtml/ui_component/sales_order_grid.xml

with the following content to your custom module :

<?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">
    <listingToolbar name="listing_top"/>
    <columns name="sales_order_columns">
        <column name="order_reference" class="Vendor\Example\Ui\Component\Listing\Column\Example">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="visible" xsi:type="boolean">true</item>
                    <item name="label" xsi:type="string" translate="true">Example Column</item>
                </item>
            </argument>
        </column>
    </columns>
</listing>

      

Your Example.php

file should expand Magento\Ui\Component\Listing\Columns\Column

and have a way prepareDataSource()

to fill in the data:



/**
 * @param array $dataSource
 * @return array
 */
public function prepareDataSource(array $dataSource)
{
    if (isset($dataSource['data']['items'])) {
        foreach ($dataSource['data']['items'] as & $item) {
            $item[$this->getData('name')] = 'Something'
        }
    }

    return $dataSource;
}

      

Note that if you want to add sorting and filtering options, you need to add some other settings, but this depends on what data you want to display in the column.

+2


source


It is better to use some extensions like Extended Order Grid . You can easily add all the columns you need to the grid to save time on loading order pages.



0


source







All Articles