cancel
Showing results for 
Search instead for 
Did you mean: 

add custom column to customer list grid

add custom column to customer list grid

hi i am reading this magento2 stackexchange

 

How to Join custom table to customer grid collection and show new column in Grid in Magento2 

 

i could add and show custom column on customer list grid. but even i change custom value on mysql directory, the change does not come up.

 

table:customer_flag

fk:customer_entity_id

custom_column: flag

 

my Collection.php is like this

 

<?php
/**
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace NAMESPACE\MODUENAME\Model\ResourceModel\Customer\Grid;

use Magento\Customer\Ui\Component\DataProvider\Document;
use Magento\Framework\Data\Collection\Db\FetchStrategyInterface as FetchStrategy;
use Magento\Framework\Data\Collection\EntityFactoryInterface as EntityFactory;
use Magento\Framework\Event\ManagerInterface as EventManager;
use Psr\Log\LoggerInterface as Logger;

class Collection extends \Magento\Customer\Model\ResourceModel\Grid\Collection
{
    /**
     * @inheritdoc
     */
    protected $document = Document::class;

    /**
     * Initialize dependencies.
     *
     * @param EntityFactory $entityFactory
     * @param Logger $logger
     * @param FetchStrategy $fetchStrategy
     * @param EventManager $eventManager
     * @param string $mainTable
     * @param string $resourceModel
     */
    public function __construct(
        EntityFactory $entityFactory,
        Logger $logger,
        FetchStrategy $fetchStrategy,
        EventManager $eventManager,
        $mainTable = 'customer_grid_flat',
        $resourceModel = '\Magento\Customer\Model\ResourceModel\Customer'
    ) {
        parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $mainTable, $resourceModel);
    }


   protected function _initSelect()
    {
        parent::_initSelect();

        $this->getSelect()->joinLeft(
                ['secondTable' => $this->getTable('customer_flag')],
                'main_table.entity_id = secondTable.customer_entity_id',
                ['flag as custom_column']
            );
    } 
     /**
     * Modify for website_id code same in location type and location list
     */
    public function addFieldToFilter($field, $condition = null)
    {
        if ($field === 'custom_column') {
            $field = 'secondTable.flag';
        }

        return parent::addFieldToFilter($field, $condition);
    }
}

I THINK... there is customer_grid_flat table as main table but no custom flag on the main table. do i have to add column to customer_grid_flat?

1 REPLY 1

Re: add custom column to customer list grid

Thanks but it seems like the collection.php is not called during loading customer page.

how can i fix it?

 

if the function 'initSelect' is called, the problem should be solved.