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 Apr 2, 2019 at 11:43
  • Have you add getAvailableStatuses() in your model file? Apr 2, 2019 at 11:45
  • s i added in my model file Apr 2, 2019 at 11:46
  • can you share your updated code in your question? Apr 2, 2019 at 11:48
  • 1
    You can try with this magento.stackexchange.com/questions/217444/… It might work for you. Apr 2, 2019 at 12:59
2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.