1

I have created a custom module for this. but its showing Id in database and grid also, in form only its showing label.

how i can get website label instead of website id in grid. please help. This is my prepare form code:

$fieldset->addField(
                'website',
                'select',
                [
                    'name' => 'website',
                    'label' => __('Associate to Website'),
                    'title' => __('Associate to Website'),
                    'required' => true,
                    'values' => $this->_systemStore->getWebsiteValuesForForm(),

            ]
        );

This is a my grid column code:

<column name="website">
           <argument name="data" xsi:type="array">
               <item name="config" xsi:type="array">
                   <item name="filter" xsi:type="string">text</item>
                   <item name="label" xsi:type="string" translate="true">Website</item>
               </item>
           </argument>
       </column>
1
  • show your listing xml code which you have used Commented Jul 18, 2018 at 6:59

1 Answer 1

2

Add below code to listing ui xml

<column name="website_id" class="Vendor\Module\Ui\Component\Listing\Column\Websites">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">text</item>
                    <item name="sorting" xsi:type="string">asc</item>
                    <item name="sortOrder" xsi:type="number">6</item>
                    <item name="label" translate="true" xsi:type="string">Website ID</item>
                </item>
            </argument>
        </column>

app/code/Vendor/Module/Ui/Component/Listing/Column/Websites.php

<?php

namespace Vendor\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;
use Magento\Framework\UrlInterface;
use Magento\Cms\Block\Adminhtml\Page\Grid\Renderer\Action\UrlBuilder;
use Magento\Framework\Pricing\PriceCurrencyInterface;
use Magento\Store\Model\StoreManagerInterface;
/**
 * Class ProductActions
 */
class Websites extends Column
{

    /**
     * @var UrlInterface
     */
    protected $urlBuilder;
    /**
     * @var PriceCurrencyInterface
     */
    protected $priceFormatter;
    /**
     * @var actionUrlBuilder
     */
    protected $actionUrlBuilder;
    /**
     * @var $_storeManager
     */
    protected $_storeManager;


    /**
     * @param ContextInterface $context
     * @param UiComponentFactory $uiComponentFactory
     * @param UrlInterface $urlBuilder
     * @param array $components
     * @param array $data
     */
    public function __construct(
        ContextInterface $context,
        UiComponentFactory $uiComponentFactory,
        UrlInterface $urlBuilder,
        UrlBuilder $actionUrlBuilder,
        PriceCurrencyInterface $priceFormatter,
        StoreManagerInterface $storeManager,
        array $components = [],
        array $data = []
    ) {
        $this->urlBuilder = $urlBuilder;
        $this->actionUrlBuilder = $actionUrlBuilder;
        $this->priceFormatter = $priceFormatter;
        $this->_storeManager = $storeManager;
        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) {
                $Websites = $this->_storeManager->getWebsites();
                $WebsiteName="-";
                foreach ($Websites as $website) {
                    if($website->getId()==$item['website_id']){
                        $WebsiteName = $website->getName();
                    }
                }
                $item[$this->getData('name')] = $WebsiteName;
            }
        }
        return $dataSource;
    }
}
8
  • Not working. screencast.com/t/pYucxm0NvhkD
    – Ashna
    Commented Jul 18, 2018 at 8:00
  • 1
    change website_id by your field name. Commented Jul 18, 2018 at 8:02
  • I want to display attribute options dropdown in admin form and grid, please help me
    – Ashna
    Commented Jul 23, 2018 at 8:37
  • I have already Accepted
    – Ashna
    Commented Jul 23, 2018 at 8:48
  • Now i am trying to add attribute option dropdown in admin form and grid, please help me on this
    – Ashna
    Commented Jul 23, 2018 at 8:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.