Skip to content

Instantly share code, notes, and snippets.

@evgv
Last active September 27, 2018 08:10
Show Gist options
  • Save evgv/87d13ebb5aa137e4b72663102fee630a to your computer and use it in GitHub Desktop.
Save evgv/87d13ebb5aa137e4b72663102fee630a to your computer and use it in GitHub Desktop.
Magento 2. Display yes/no in grid instead 0/1

Magento 2. Display yes/no in grid instead 0/1

Need to create source class(Which must implement Magento\Framework\Data\OptionSourceInterface) for this which defined

<?php

namespace Vendor\Module\Model\Source;

use Magento\Framework\Data\OptionSourceInterface;

/**
 * Class Foo
 */
class Foo implements OptionSourceInterface
{

    const YES = 1;
    const NO  = 0;

    public static function getOptionArray()
    {
        return [
            self::YES => __('Yes'),
            self::NO => __('No')
        ];
    }

    /**
     * Get options
     *
     * @return array
     */
    public function toOptionArray()
    {
        $res = [];
        
        foreach (self::getOptionArray() as $index => $value) {
            $res[] = ['value' => $index, 'label' => $value];
        }
        
        return $res;
    }
}

And change your ui_component

<?xml version="1.0"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <columns name="{{COLUMN_NAME}}">
        <column name="is_prebook">
            <argument name="data" xsi:type="array">
                <item name="options" xsi:type="object">Vendor\Module\Model\Source\Foo</item>
                <item name="config" xsi:type="array">                    
                    <item name="filter" xsi:type="string">select</item>
                    <item name="bodyTmpl" xsi:type="string">ui/grid/cells/html</item>
                    <item name="sortable" xsi:type="boolean">true</item>
                    <item name="dataType" xsi:type="string">select</item>
                    <item name="sortOrder" xsi:type="number">40</item>
                    <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/select</item>
                    <item name="editor" xsi:type="string">select</item>
                    <item name="label" xsi:type="string" translate="true">{{LABEL}}</item>
                </item>
            </argument>
        </column>
    </columns>
</listing>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment