2

shippingbar_post_listing.xml

<column name="status">
        <argument name="data" xsi:type="array">
            <item name="options" xsi:type="object">CM\Shippingbar\Model\Status</item>
            <item name="config" xsi:type="array">
                <item name="filter" xsi:type="string">text</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">true</item>
                    </item>
                </item>
                <item name="label" xsi:type="string" translate="true">Status</item>
            </item>
        </argument>
    </column>

CM\Shippingbar\Model\Status.php

  <?php

namespace CM\Shippingbar\Model;

use Magento\Framework\Data\OptionSourceInterface;

/**
 * ProductStatus Class
 */
class Status implements OptionSourceInterface
{
 /**
     * @var \CM\Shippingbar\Model\Productlabel
     */
    protected $model;


    /**
     * Constructor
     *
     * @param \CM\Shippingbar\Model\ModelFile $model
     */
   protected function _construct(\CM\Shippingbar\Model\Shippingbar $model)
    {
$this->model = $model;   
 }

    /**
     * Get options
     *
     * @return array
     */
    public function toOptionArray()
    {
        $options[] = ['label' => '', 'value' => ''];
        $availableOptions =$this->model->getAvailableStatuses();
        foreach ($availableOptions as $key => $value) {
            $options[] = [
                'label' => $value,
                'value' => $key,
            ];
        }
        return $options;
    }

}

CM\Shippingbar\Model\Shippingbar.php

 <?php
namespace CM\Shippingbar\Model;

class Shippingbar  extends \Magento\Framework\Model\AbstractModel 
{
    const STATUS_ENABLED = 1;
    const STATUS_DISABLED = 0;



    protected function _construct()
    {
        $this->_init('CM\Shippingbar\Model\ResourceModel\Shippingbar');
    }


     public function getAvailableStatuses()
    {
        return [self::STATUS_ENABLED => __('Enabled'), self::STATUS_DISABLED => __('Disabled')];
    }
  }

enter image description here

1

In shippingbar_post_listing.xml file.

<column name="status">
    <argument name="data" xsi:type="array">
        <item name="options" xsi:type="object">CM\Shippingbar\Ui\Component\Listing\Column\Status</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">Status</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>

CM\Shippingbar\Ui\Component\Listing\Column\Status.php

<?php

namespace CM\Shippingbar\Ui\Component\Listing\Column;

class Status implements \Magento\Framework\Option\ArrayInterface
{
/**
 * Options getter
 *
 * @return array
 */
   public function toOptionArray()
   {
       return [['value' => 1, 'label' => __('Enable')], ['value' => 0, 'label' => __('Disable')]];
   }
}

I hope it helps!

  • Error comes : ( ! ) Fatal error: Uncaught Error: Call to a member function getAvailableStatuses() on null in – divya sekar Apr 2 '19 at 11:43
  • Have you add getAvailableStatuses() in your model file? – Chirag Patel Apr 2 '19 at 11:45
  • s i added in my model file – divya sekar Apr 2 '19 at 11:46
  • can you share your updated code in your question? – Chirag Patel Apr 2 '19 at 11:48
  • 1
    You can try with this magento.stackexchange.com/questions/217444/… It might work for you. – Chirag Patel Apr 2 '19 at 12:59
1

try this way... and shippingbar_post_listing.xml in my code replace

<column name="status">
        <argument name="data" xsi:type="array">
            <item name="options" xsi:type="object">CM\Shippingbar\Ui\Component\Listing\Column\Status</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">Status</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>

and CM\Shippingbar\Ui\Component\Listing\Column\Status.php code this way..

<?php

namespace CM\Shippingbar\Ui\Component\Listing\Column;

class Status implements \Magento\Framework\Option\ArrayInterface
{
/**
 * Options getter
 *
 * @return array
 */
   public function toOptionArray()
   {
       return [['value' => 1, 'label' => __('Enable')], ['value' => 0, 'label' => __('Disable')]];
   }
}

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.