Domanda

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

È stato utile?

Soluzione

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' => 1, 'label' => __('Option 2')]
        ];
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a magento.stackexchange
scroll top