Add a custom attribute in grid customer magento 2
-
06-02-2021 - |
Pergunta
I have created an attribute type: select but i can't display it in the grid customer. the code of my customer_listing.xml
<column name="customer_status">
<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">Customer Status</item>
<item name="sortOrder" xsi:type="number">190</item>
</item>
</argument>
</column>
the code of the UpgradeData.php
$attributeCode7 = "customer_status";
$customerSetup->addAttribute(Customer::ENTITY, $attributeCode7, [
'type' => 'varchar',
'label' => 'Customer Status',
'input' => 'select',
'source' => 'Stack\Customer\Model\Attribute\Source\CustomerStatus',
'required' => true,
'visible' => true,
'user_defined' => true,
'sort_order' => 1000,
'position' => 1000,
'system' => false,
'is_used_in_grid' => true,
'is_visible_in_grid' => true,
]);
$attribute7 = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, $attributeCode7)
->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms' => ['adminhtml_customer', 'customer_account_create', 'customer_account_edit']
]);
$attribute7->save();
also i created indexer.xml under etc of my module here is the code:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Indexer/etc/indexer.xsd">
<indexer id="customer_grid">
<fieldset name="customer">
<field name="customer_status" xsi:type="filterable" dataType="int"/>
</fieldset>
</indexer>
please help me and thanks in advance
Solução
You don't need customer_listing.xml
or indexer.xml
to show customer attribute in gird. these line will do it for you
'is_used_in_grid' => true,
'is_visible_in_grid' => true,
Just run the following command. This will show your column in grid.
php bin/magento indexer:reindex
Make sure Customer Grid index rebuilt Customer Grid index has been rebuilt successfully
New column of customer_status
will be created customer_grid_flat
table
Licenciado em: CC-BY-SA com atribuição
Não afiliado a magento.stackexchange