0

I ma using magento 2.1.0, and i am trying to save city option in my grid.

City dropdown ui file:

<field name="city">
            <argument name="data" xsi:type="array">
            <item name="options" xsi:type="object">xxx\xxx\Model\City\CityOption</item>
            <item name="config" xsi:type="array">
                <item name="dataType" xsi:type="string">text</item>
                <item name="label" xsi:type="string" translate="true">city</item>
                <item name="formElement" xsi:type="string">select</item>
                <item name="source" xsi:type="string">page</item>
                <item name="dataScope" xsi:type="string">city_id</item>
                <item name="caption" xsi:type="string" translate="true">-- Please Select --</item>
            </item>
            </argument>
        </field>

city column in grid:

<column name="city_id">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="filter" xsi:type="string">text</item>
                <item name="label" translate="true" xsi:type="string">city</item>
                <item name="editor" xsi:type="array">
                    <item name="editorType" xsi:type="string">text</item>
                    <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="boolean">false</item>
                    </item>
                </item>
            </item>
        </argument>
    </column>

this is the retrriving data in city dropdown

 public function toOptionArray()
{
$options = array();
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$model = $objectManager->create('\xxx\xxx\Model\City')->getCollection();
foreach($model as $mdl)
     {

        $options[] =  array('label' => $mdl->getCity() , 'value' => $mdl->getCityId());

    }

    return $options;
}

now, through this i am able to save city in database as their id, but same id also display in grid column, i want to show city name in grid.

  • You need to use renderer in your code. Refer to magento.stackexchange.com/a/150288/2443 – Jaimin Sutariya Jan 30 '17 at 5:56
  • Can you please little modify that code, because they use default country option, my city is in my custome module so. what to replace countryinformation and their name and code???? – sam Jan 30 '17 at 6:10
0

You have to use custom column class to modify id to string.

SourceInterface:

<?php
namespace ProjectName\ModuleName\Model\Source;

use Magento\Framework\Option\ArrayInterface;

interface SourceInterface extends ArrayInterface
{
    public function toArray();

    public function toOptionArray();

    public function getLabel($value);

    public function getValue($label);
}

Column option class:

<?php
namespace ProjectName\ModuleName\Model\Source;

class City implements SourceInterface
{
    protected $options;

    public function __construct()
    {
        $this->options = [
            '1' => 'City 1',
            '2' => 'City 2',
            '3' => 'City 3',
        ];
    }

    public function toArray()
    {
        return $this->options;
    }

    public function toOptionArray()
    {
        $options = [];
        foreach ($this->options as $value => $label) {
            $options[] = [
                'value' => $value,
                'label' => $label,
            ];
        }

        return $options;
    }

    public function getLabel($value)
    {
        return array_key_exists($value, $this->options) ? $this->options[$value] : false;
    }

    public function getValue($label)
    {
        return array_search($label, $this->options);
    }
}

Column in grid:

<column name="city_id" class="ProjectName\ModuleName\Ui\Component\Listing\Column\City">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="filter" xsi:type="string">text</item>
            <item name="label" translate="true" xsi:type="string">city</item>
            <item name="editor" xsi:type="array">
                <item name="editorType" xsi:type="string">text</item>
                <item name="validation" xsi:type="array">
                    <item name="required-entry" xsi:type="boolean">false</item>
                </item>
            </item>
        </item>
    </argument>
</column>

Class ProjectName\ModuleName\Ui\Component\Column\City:

<?php
namespace ProjectName\ModuleName\Ui\Component\Column;

use Magento\Ui\Component\Listing\Columns\Column;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use ProjectName\ModuleName\Model\Source\City as CitySource;

class City extends Column
{
    protected $citySource;

    public function __construct(
        ContextInterface $context,
        UiComponentFactory $uiComponentFactory,
        CitySource $citySource,
        array $components = [],
        array $data = []
    ) {
        parent::__construct($context, $uiComponentFactory, $components, $data);
        $this->citySource = $citySource;
    }

    public function prepareDataSource(array $dataSource)
    {
        $dataSource = parent::prepareDataSource($dataSource);
        if (empty($dataSource['data']['items'])) {
            return $dataSource;
        }

        foreach ($dataSource['data']['items'] as &$item) {
            if (isset($item['city_id']) && !empty($item['city_id'])) {
                $item['city_id'] = $this->citySource->getLabel($item['city_id']);
            }
        }

        return $dataSource;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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