8

I have created a InStock column in admin grid and table.

In table, Instock column values are 0 (or) 1 based on productstock. In admin grid, the Instock column values are dispalyed 0 (or)1 .

But i need to displayed that Yes instead of 1 and No instead of 0.

How to do this?

Thanks.

2
  • Did you create the grid by using ui component or block?
    – Shireen N
    Commented Mar 15, 2018 at 8:42
  • uiComponent ...........
    – Mahi M
    Commented Mar 15, 2018 at 8:47

2 Answers 2

29

Method 1: If you have boolean value 0 and 1 use default option class Magento\Config\Model\Config\Source\Yesno

app/code/Vendor/Theme/view/adminhtml/ui_component/your_ui_grid.xml

<column name="instock">
    <argument name="data" xsi:type="array">
        <item name="options" xsi:type="object">Magento\Config\Model\Config\Source\Yesno</item>
        <item name="config" xsi:type="array">
            <item name="sortOrder" xsi:type="number">60</item>
            <item name="filter" xsi:type="string">select</item>
            <item name="label" translate="true" xsi:type="string">InStock</item>
            <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/select</item>
            <item name="dataType" xsi:type="string">select</item>
            <item name="editor" xsi:type="array">
                <item name="editorType" xsi:type="string">select</item>
            </item>
        </item>
    </argument>
</column>

Method 2: If you have dynamic options value, use custom options class Vendor\Module\Ui\Component\Listing\Column\Status

app/code/Vendor/Theme/view/adminhtml/ui_component/your_ui_grid.xml

<column name="instock">
    <argument name="data" xsi:type="array">
        <item name="options" xsi:type="object">Vendor\Module\Ui\Component\Listing\Column\InStock</item>
        <item name="config" xsi:type="array">
            <item name="sortOrder" xsi:type="number">60</item>
            <item name="filter" xsi:type="string">select</item>
            <item name="label" translate="true" xsi:type="string">InStock</item>
            <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/select</item>
            <item name="dataType" xsi:type="string">select</item>
            <item name="editor" xsi:type="array">
                <item name="editorType" xsi:type="string">select</item>
            </item>
        </item>
    </argument>
</column>

Now Create InStock.php at

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

<?php

namespace Vendor\Module\Ui\Component\Listing\Column;

class InStock implements \Magento\Framework\Option\ArrayInterface
{
    /**
     * Options getter
     *
     * @return array
     */
    public function toOptionArray()
    {
        return [
            ['value' => 1, 'label' => __('Male')],
            ['value' => 2, 'label' => __('Female')],
            ['value' => 3, 'label' => __('Other')]
        ];
    }
}

Method 3: If you want to change whole column value to custom value or update existing value then use DataSource method

Pass Vendor\Module\Ui\Component\Listing\Column\InStock class in column

app/code/Vendor/Theme/view/adminhtml/ui_component/your_ui_grid.xml

<column name="instock" class="Vendor\Module\Ui\Component\Listing\Column\InStock">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="sortOrder" xsi:type="number">60</item>
            <item name="label" translate="true" xsi:type="string">InStock</item>
            <item name="dataType" xsi:type="string">select</item>
        </item>
    </argument>
</column>

Now create InStock.php at

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

<?php

namespace Vendor\Module\Ui\Component\Listing\Column;

use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Ui\Component\Listing\Columns\Column;

class InStock extends Column
{
    public function __construct(
        ContextInterface $context,
        UiComponentFactory $uiComponentFactory,
        array $components = [],
        array $data = []
    ) {
        parent::__construct($context, $uiComponentFactory, $components, $data);
    }

    public function prepareDataSource(array $dataSource)
    {
        if (isset($dataSource['data']['items'])) {
            foreach ($dataSource['data']['items'] as &$items) {
                // $items['instock'] is column value
                if ($items['instock'] == 1) {
                    $items['instock'] = $items['instock'].'Custom Value';
                }
            }
        }
        return $dataSource;
    }
}
7
  • I got the Instock values from observer file and store in table.And then displayed in admin grid.
    – Mahi M
    Commented Mar 15, 2018 at 8:45
  • Read my answer I give an answer to show yes instead of 1 and No instead of 2. Just pass Vendor\Module\Ui\Component\Listing\Column\Status to options of grid column and return an array from InStock.php Commented Mar 15, 2018 at 8:49
  • @PrincePatel If we need to apply some logic on value , then how we will get the column value in toOptionArray method? any idea.
    – Verdu
    Commented Mar 15, 2018 at 11:09
  • @Verdu Check my updated answer. If you want to get column value then use Method 2 You can get column value $items['my_column_name'] in prepareDataSource() function Commented Mar 15, 2018 at 11:30
  • @PrincePatel The column value is successfully received in the prepareDataSource() ,but the desired value does not display in the grid? what am I missing?
    – Verdu
    Commented Mar 15, 2018 at 12:01
1

The question has already been answered, but adding in case someone needs to do the same thing in Magento version >= 2.3:

<column name="instock" component="Magento_Ui/js/grid/columns/select">
    <settings>
        <options class="Magento\Config\Model\Config\Source\Yesno" />
        <filter>select</filter>
        <dataType>select</dataType>
        <label translate="true">InStock</label>
    </settings>
</column>

Column docs

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.