0

In magento 2, I am working on extension manage customer attributes. I want to show custom customer attribute added to show on customer grid.

I set attribute values like is_used_in_grid=1, is_visible_in_grid=1 , is_filterable_in_grid=1 and is_searchable_in_grid=1 but still attribute is not displaying on customer grid.

I found one solution here How do you add a custom attribute to the customer grid in adminhtml (Magento2)

This solution uses Ui component, I want to show custom customer attributes dynamicaly on customer grid. How can I do this?

2

First you have to create ui_component file in custom extension.

KTPL/Customerattribute/view/adminhtml/ui_component/customer_listing.xml

<?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="customer_columns" class="Magento\Customer\Ui\Component\Listing\Columns">
        <column name="nick_name" class="KTPL\Customerattribute\Ui\Component\Listing\Column\Nickname">
            <argument name="data" xsi:type="array">
                    <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">text</item>
                    <item name="editor" xsi:type="string">text</item>
                    <item name="label" xsi:type="string" translate="true">Nick Name</item>
                    <item name="sortOrder" xsi:type="number">40</item>
                    </item>
            </argument>
        </column>
    </columns>
</listing>

second step : KTPL/Customerattribute/Ui/Component/Listing/Column/Nickname.php

<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace KTPL\Customerattribute\Ui\Component\Listing\Column;

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

class Nickname extends Column
{
    /**
     * 
     * @param ContextInterface   $context           
     * @param UiComponentFactory $uiComponentFactory   
     * @param array              $components        
     * @param array              $data              
     */
    public function __construct(
        ContextInterface $context,
        UiComponentFactory $uiComponentFactory,
        array $components = [],
        array $data = []
    ) {
        parent::__construct($context, $uiComponentFactory, $components, $data);
    }

    /**
     * Prepare Data Source
     *
     * @param array $dataSource
     * @return array
     */
    public function prepareDataSource(array $dataSource)
    {
        if (isset($dataSource['data']['items'])) {
            foreach ($dataSource['data']['items'] as & $item) 
            {

                $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
                $customer = $objectManager->create('Magento\Customer\Model\Customer')->load($item['entity_id']);

                $item[$this->getData('name')] = $customer->getNickName();
            }
        }
        return $dataSource;
    }
}

After applying this you need flush cache and setup:upgrade.

  • @ Praksash Patel - I am managing attributes from back end using extension manage customer attributes. I want dynamic solution. so that I do this from backend. – PMB Feb 28 '17 at 14:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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