3

I am working on admin custom module. My module table join with customer table.

Now I want to display User Name (customer firstname and lastname) values in single column in my custom module UI grid.

How can it will be done? Any one have idea?

Thanks

6
  • follow this link magento.stackexchange.com/questions/159658/… Commented Jun 9, 2017 at 13:24
  • Do you need a filter for this column? Commented Jun 9, 2017 at 13:47
  • First need to display in gird. Commented Jun 9, 2017 at 13:57
  • You can use custom column render, but in this case filter can not work properly without modifications in collection. Commented Jun 9, 2017 at 14:08
  • How to render custome column in grid? Commented Jun 9, 2017 at 16:12

2 Answers 2

6

Little late on the answer, but hopefully helpful to those that finds this question.

Vendor/Module/view/adminhtml/ui_component/customer_listing.xml

<column name="fullname" class="Vendor\Module\Ui\Component\Listing\Column\FullName">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <!-- You can change this accordingly -->
            <item name="sortable" xsi:type="boolean">false</item>
            <item name="dataType" xsi:type="string">text</item>
            <item name="bodyTmpl" xsi:type="string">ui/grid/cells/html</item>
            <item name="label" xsi:type="string" translate="true">Full Name</item>
        </item>
    </argument>
</column>

Vendor/Module/Ui/Component/Listing/Column/Fullname.php

namespace Vendor\Module\Ui\Component\Listing\Column;

class FullName extends \Magento\Ui\Component\Listing\Columns\Column
{
    /**
     * @param \Magento\Framework\View\Element\UiComponent\ContextInterface $context
     * @param \Magento\Framework\View\Element\UiComponentFactory $uiComponentFactory
     * @param array $components = []
     * @param array $data = []
     */
    public function __construct(
        \Magento\Framework\View\Element\UiComponent\ContextInterface $context,
        \Magento\Framework\View\Element\UiComponentFactory $uiComponentFactory,
        array $components = [],
        array $data = []
    ){
        parent::__construct($context, $uiComponentFactory, $components, $data);
    }

    public function prepareDataSource(array $dataSource)
    {
        if(isset($dataSource['data']['items'])){
            foreach($dataSource['data']['items'] as &$item){
                $item['fullname'] = $item['firstname'] . ' ' . $item['lastname'];

            }
        }

        return $dataSource;
    }
}

What is going on is that you are looping through all of the records for the grid listing, and applying data to the fullname column of the grid with the data firstname and lastname fields of your model.

1
  • what to do for filter working Commented Nov 8, 2021 at 13:54
0

Suppose your UI component column like:


<column name="customer_name" class="Vendor\Module\Ui\Component\Listing\Column\Customer">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="sortable" xsi:type="boolean">false</item>
            <item name="dataType" xsi:type="string">text</item>
            <item name="bodyTmpl" xsi:type="string">ui/grid/cells/html</item>
            <item name="label" xsi:type="string" translate="true">Customer Name</item>
            <item name="sortOrder" xsi:type="number">20</item>
        </item>
    </argument>
</column>

Now create renderer class Customer[Vendor\Module\Ui\Component\Listing\Column\Customer]


namespace Vendor\Module\Ui\Component\Listing\Column;

class Customer extends \Magento\Ui\Component\Listing\Columns\Column
{

    /**
     * Get data
     *
     * @param array $item
     * @return string
     */
    protected function prepareItem(array $item)
    {
        // make your logic and return string

        return $customerName;
    }
}

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.