3

I need to add a drop down in the admin grid of a custom module. I have set the editor type as select, but how to set the data source of the select to read from another database table?

I need to populate the select with data coming as result of SQL query.

enter image description here

enter image description here

  • 1
    Let me know if you have any issue – Prince Patel Aug 30 '17 at 9:57
15

Add select options and component in column. Full column like this:

<column name="items">
    <argument name="data" xsi:type="array">
        <item name="options" xsi:type="object">{Vendor}\{Module}\Ui\Component\Listing\Column\Myoptions</item>
        <item name="config" xsi:type="array">
            <item name="filter" xsi:type="string">select</item>
            <item name="label" xsi:type="string" translate="true">Items</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 name="validation" xsi:type="array">
                    <item name="required-entry" xsi:type="boolean">false</item>
                </item>
            </item>
        </item>
    </argument>
</column>

Now create Myoptions.php at

app/code/{Vendor}/{Module}/Ui/Component/Listing/Column/Myoptions.php

Now you can load table and pass array data in toOptionArray() like

<?php

namespace Vendor\Module\Ui\Component\Listing\Column;

class Status implements \Magento\Framework\Option\ArrayInterface
{
    //Here you can __construct Model

    public function toOptionArray()
    {
        return [
            ['value' => 0, 'label' => __('Option 1')],
            ['value' => 0, 'label' => __('Option 2')]
        ];
    }
}
  • Happy to help, Happy Coding...:) – Prince Patel Aug 30 '17 at 10:21
  • one more thing , how to pass parameters to Myoptions.php? i need to pass the row entity_id to select items related to that record only – Zorox Aug 30 '17 at 10:22
  • while exporting csv file . I am getting only values but i want label there in csv file , – mohan Sep 24 '18 at 7:21
  • @PrincePatel it helped me to display the status values in the filter but it doesn't render the drop-down within the grid cell. Magento 2.2.5 – Adnan Nov 3 '18 at 2:15
  • @PrincePatel Very well explained answer +1 for that. – Muhammad Hasham Sep 19 at 7:02

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.