I have added one customer address custom attribute. it is showing in the customer address form as a dropdown field. Now I want to show that as a column in customer address grid in admin with value.
I have added this code:
<column name="addresstype" class="xxx\xxx\Customer\Ui\Component\Listing\Address\Column\Addresstype" sortOrder="95">
 <settings>
 <filter>text</filter>
 <label translate="true">addresstype</label>
 <editor>
 <editorType>text</editorType>
 </editor>
 </settings>
 </column>
The column is added but the value is not showing in this. how to get the value which was set in form? I tried in the following way, but getting an error:
xxx/xxx/Customer/Ui/Component/Listing/Address/Column/Addresstype.php
 public function prepareDataSource(array $dataSource): array
{
 if (isset($dataSource['data']['items'])) {
 foreach ($dataSource['data']['items'] as &$item) {
 if ($item['addresstype'] == 1) {
 $item['addresstype'] = 'Yes';
 } else {
 $item['addresstype'] = 'No';
 }
 }
 }
 return $dataSource;
}
But i ma getting error :
Undefined index: addresstype in /var/www/html/zest-anchor/app/code/Zest/Integration/Customer/Ui/Component/Listing/Address/Column/Addresstype.php on line 75 {"exception":"[object] (Exception(code: 0): Notice: Undefined index: addresstype in /var/www/html/zest-anchor/app/code/Zest/Integration/Customer/Ui/Component/Listing/Address/Column/Addresstype.php on line 75 at /var/www/html/zest-anchor/vendor/magento/framework/App/ErrorHandler.php:61)"} []
- 
 Have you got any solution?Manisha Vasani– Manisha Vasani2021年08月26日 10:21:55 +00:00Commented Aug 26, 2021 at 10:21
1 Answer 1
To get your value in the admin grid, the code should be
in your ui_component xml file
<column name="addresstype" component="Magento_Ui/js/grid/columns/select" sortOrder="95">
 <settings>
 <addField>true</addField>
 <options class="Zest\Integration\Model\Customer\Attribute\Source\AddressType"/>
 <filter>select</filter>
 <sortable>false</sortable>
 <dataType>select</dataType>
 <label translate="true">Address Type</label>
 </settings>
 </column>
You dont need a source class as its a customer address attribute, check your customer address attribute settings, make sure you have set Yes for Add to Column Options and Use in Filter Options
(削除) In your customer address type source file (削除ここまで)
(削除) Zest\Integration\Model\Customer\Attribute\Source\AddressType.php (削除ここまで)
<?php
namespace Zest\Integration\Model\Customer\Attribute\Source;
use Magento\Eav\Model\Entity\Attribute\Source\AbstractSource;
use Magento\Eav\Model\Entity\Attribute\Source\SourceInterface;
use Magento\Framework\Data\OptionSourceInterface;
/**
 * returns customer's address type source
 *
 */
class AddressType extends AbstractSource implements SourceInterface, OptionSourceInterface
{
 
 
 /**
 * Retrieve option array
 *
 * @return string[]
 */
 public static function getOptionArray()
 {
 return [ 0 => __('No'), 1 => __('Yes')];
 }
 /**
 * Retrieve option array with empty value
 *
 * @return string[]
 */
 public function getAllOptions()
 {
 $result = [];
 foreach (self::getOptionArray() as $index => $value) {
 $result[] = ['value' => $index, 'label' => $value];
 }
 return $result;
 }
 /**
 * Retrieve option text by option value
 *
 * @param string $optionId
 * @return string
 */
 public function getOptionText($optionId)
 {
 $options = self::getOptionArray();
 return isset($options[$optionId]) ? $options[$optionId] : null;
 }
}
To get your option values in the grid for select type columns , you need an attribute source class like the one above
To modify the value in the grid you need a renderer class like the one that you've added in the question.
- 
 but how to get value from renderer class. like if i added this code.. only yes value is visible... its not change if ($item['addresstype'] == 1) { $item['addresstype'] = 'Yes'; } else { $item['addresstype'] = 'No'; }sam– sam2021年02月23日 13:49:15 +00:00Commented Feb 23, 2021 at 13:49
- 
 if i use your above code.. i am getting error: Column not found: 1054 Unknown column 'main_table.addresstype' in 'field list', query was: SELECTmain_table.addresstypeFROMcustomer_address_entityASmain_tableWHERE (parent_id= '103674') LIMIT 20 {"exception":"[object] (Zend_Db_Statement_Exception(code: 42): SQLSTATE[42S22]: Column not found: 1054 Unknown column 'main_table.addresstype' in 'field list', query was: SELECTmain_table.addresstypeFROMcustomer_address_entityASmain_tableWHERE (parent_id= '103674') LIMIT 20sam– sam2021年02月23日 14:00:45 +00:00Commented Feb 23, 2021 at 14:00
- 
 Hi, in your case, you don't need a source class, as its a customer address attribute and it shows up in theaddress edit formalready. How did you add the address attribute. through backend or through setup class ? if from backend, check your attribute settings, make sure you have setYesforAdd to Column OptionsandUse in Filter OptionsHaijerome– Haijerome2021年02月23日 14:22:54 +00:00Commented Feb 23, 2021 at 14:22
- 
 @Haijerome I have added Add to Column Options = true and use in filter = true, still customer address attribute value is not visible in customer grid, however customer attribute working fine, I am using magento 2.2.11. any idea?Manisha Vasani– Manisha Vasani2021年08月24日 14:18:03 +00:00Commented Aug 24, 2021 at 14:18
Explore related questions
See similar questions with these tags.