4

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

2 Answers 2

7

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!

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

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')]];
   }
}
11
  • In which file @rakesh Commented Apr 2, 2019 at 11:03
  • its not working rakesh Commented Apr 2, 2019 at 12:12
  • you have my code use and replace it? Commented Apr 2, 2019 at 12:13
  • S I did it, its again show status '0' or '1' Commented Apr 2, 2019 at 12:15
  • prnt.sc/n6dyv9 i used it but it replicate same prnt.sc/n6dzm6 Commented Apr 2, 2019 at 12:22

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.