Estou usando o código abaixo para adicionar uma coluna na grade do cliente em 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 é o atributo do cliente criado usando o código abaixo.
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();
}
- Se eu não usar o Magecoder \ Magcustomer \ Ui \ Component \ Listing \ Column \ Show é aprovado , a coluna personalizada ficará em branco.
Mas, se eu usar, preciso buscar o modelo do cliente na função prepareDataSource porque a variável $ dataSource não contém o valor da coluna personalizada. É possível mostrar valores sem buscar dados novamente?
- Além disso, quando o filtro é usado, um erro SQL é gerado.
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')