1

I have two columns in Admin grid. For ex. 1. First Name, 2. Last Name

<column name="firstname">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="filter" xsi:type="string">text</item>
            <item name="label" translate="true" xsi:type="string">First Name</item>
            <item name="editor" xsi:type="array">
                <item name="editorType" xsi:type="string">text</item>
            </item>
        </item>
    </argument>
</column>
<column name="lastname">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="filter" xsi:type="string">text</item>
            <item name="label" translate="true" xsi:type="string">Last Name</item>
            <item name="editor" xsi:type="array">
                <item name="editorType" xsi:type="string">text</item>
            </item>
        </item>
    </argument>
</column>

So How can I merge these two columns into one like 1. First Name Last Name

1 Answer 1

5

You need to renderer field in UI grid.

Add <Vendor>\<Module>\Ui\Component\Listing\Column\Firstname class in field like this

<column name="firstname" class="Vendor\Module\Ui\Component\Listing\Column\Firstname">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="filter" xsi:type="string">text</item>
            <item name="label" translate="true" xsi:type="string">Name</item>
        </item>
    </argument>
</column>

Now create Firstname.php at

<Vendor>\<Module>\Ui\Component\Listing\Column\Firstname.php

<?php

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

use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Ui\Component\Listing\Columns\Column;

class Firstname extends Column
{
    public function prepareDataSource(array $dataSource)
    {   
        if(isset($dataSource['data']['items'])) {
            foreach($dataSource['data']['items'] as &$items) {
                $firstname = $items['firstname'];
                $lastname = $items['lastname'];

                $items['firstname'] = $firstname." ".$lastname;           
            }
        }

        return $dataSource;
    }
}
7
  • You don't need the constructor here. You aren't doing anything with it apart from re-instantiating parent class properties. Commented Dec 8, 2017 at 9:03
  • Hello, I have tried an it is working fine. But somhow i can not filter that coloumn. Please check this screenshot. It shows me an error. awesomescreenshot.com/image/… Commented Mar 22, 2022 at 17:14
  • @SunnyRahevar, have found any solutions for sorting, i'm facing same issue. Commented Mar 23, 2022 at 9:39
  • Hello @JimitBhavsar, check this magento.stackexchange.com/questions/354187/… Commented Mar 23, 2022 at 11:20
  • @SunnyRahevar, Thanks. Commented Mar 23, 2022 at 11:46

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.