I am using below code to add a column in customer grid in admin.
/app/code/Namespace/Module/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">
<columns name="customer_columns" class="Magento\Customer\Ui\Component\Listing\Columns">
<column name="magcustomer_customer_approve" class="Namespace\Module\Ui\Component\Listing\Column\Showisapproved">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="filter" xsi:type="string">select</item>
<item name="editor" xsi:type="string">select</item>
<item name="component" xsi:type="string">Magento_Ui/js/grid/columns/select</item>
<item name="dataType" xsi:type="string">select</item>
<item name="label" xsi:type="string" translate="true">Is Approved</item>
<item name="sortOrder" xsi:type="number">51</item>
</item>
</argument>
</column>
</columns>
</listing>
/app/code/Namespace/Module/Ui/Component/Listing/Column/Showisapproved.php
<?php
namespace Namespace\Module\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 Showisapproved 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) {
$item[$this->getData('name')] = 0;//Value which you want to display
}
}
return $dataSource;
}
}
magcustomer_customer_approve is customer attribute created using below code.
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
/** @var CustomerSetup $customerSetup */
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
$attributeSetId = $customerEntity->getDefaultAttributeSetId();
/** @var $attributeSet AttributeSet */
$attributeSet = $this->attributeSetFactory->create();
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
$customerSetup->addAttribute(Customer::ENTITY, 'magcustomer_customer_approve', [
'type' => 'int',
'label' => 'Is Approved',
'input' => 'select',
'required' => false,
'visible' => true,
'user_defined' => true,
'sort_order' => 1001,
'position' => 1001,
'system' => 0,
'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
'adminhtml_only'=>1,
'default'=>0
]);
$attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'magcustomer_customer_approve')
->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms' => ['adminhtml_customer'],
]);
$attribute->save();
}
- If I don't use Magecoder\Magcustomer\Ui\Component\Listing\Column\Showisapproved, the custom column comes blank.
But if I use then I need to fetch using customer model in function prepareDataSource because variable $dataSource does not contain value of custom column. Is it possible to show values without fetching data again?
- Also when filter is used a SQL error is generated.
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'magcustomer_customer_approve' in 'where clause' , query was: SELECT COUNT(*) FROM `customer_grid_flat` AS `main_table` WHERE (`magcustomer_customer_approve ` = '1') AND (`magcustomer_customer_approve` = '1') AND (`magcustomer_customer_approve` = '1') AND ( `magcustomer_customer_approve` = '1') AND (`magcustomer_customer_approve` = '1')
Best Answer
Solution for Magento 2.1
Add the attribute to the customer grid index:
etc/indexer.xml
Note that the "indexer" and "fieldset" elements only have the "id" and "name" attributes respectively. The structure is merged into the existing indexer.
Mark the attribute as "used in grid"
In the installer:
otherwise you will get another SQL error during indexing, because the indexer looks for the column in the
customer_entity
main table: