6

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.

  • Did you create the grid by using ui component or block? – Shireen N Mar 15 '18 at 8:42
  • uiComponent ........... – Mano M Mar 15 '18 at 8:47
11

Method 1: If you have fixed value like 0 and 1

Pass Vendor\Module\Ui\Component\Listing\Column\Status in options at your form's instock column

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' => __('Yes')], ['value' => 0, 'label' => __('No')]];
    }
}

Method 2: If you have dynamic values in the column or if you want to set value based on column value then

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 name
                if ($items['instock'] == 1) {
                    $items['instock'] = 'Yes';
                } else {
                    $items['instock'] = 'No';
                }
            }
        }
        return $dataSource;
    }
}
  • I got the Instock values from observer file and store in table.And then displayed in admin grid. – Mano M Mar 15 '18 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 – Prince Patel Mar 15 '18 at 8:49
  • one more doubt .......Can u ask that – Mano M Mar 15 '18 at 8:58
  • You should ask if it's related to this question. Or it is better to post another question. – Prince Patel Mar 15 '18 at 9:00
  • If you know that........can u help me.....magento.stackexchange.com/questions/217405/… – Mano M Mar 15 '18 at 9:03

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.