I'm able to load custom options into the select but I don't know how to pre-select some of these options at page load
My UI field:
<field name="products">
<argument name="data" xsi:type="array">
<item name="options" xsi:type="object">vendor\Module\Model\Attribute\Source\SourceName</item>
<item name="config" xsi:type="array">
<item name="label" xsi:type="string" translate="true">Products</item>
<item name="componentType" xsi:type="string">field</item>
<item name="formElement" xsi:type="string">select</item>
<item name="component" xsi:type="string">Magento_Ui/js/form/element/ui-select</item>
<item name="elementTmpl" xsi:type="string">ui/grid/filters/elements/ui-select</item>
<item name="dataScope" xsi:type="string">products</item>
<item name="filterOptions" xsi:type="boolean">true</item>
<item name="showCheckbox" xsi:type="boolean">true</item>
<item name="disableLabel" xsi:type="boolean">true</item>
<item name="chipsEnabled" xsi:type="boolean">true</item>
<item name="multiple" xsi:type="boolean">true</item>
<item name="levelsVisibility" xsi:type="number">1</item>
<item name="sortOrder" xsi:type="number">10</item>
<item name="required" xsi:type="boolean">true</item>
</item>
</argument>
</field>
My options source class:
<?php
namespace vendor\Module\Model\Attribute\Source;
class SourceName implements \Magento\Framework\Option\ArrayInterface
{
protected $productCollection;
public function __construct(
\Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection
) {
$this->productCollection = $productCollection;
}
public function toOptionArray()
{
// Load the products as options
$products = $this->productCollection->load();
$options = [];
/* @todo: add query to load selected options */
foreach ($products as $product){
$options[] = [
"value" => $product->getId(),
"label" => $product->getSku()
];
}
return $options;
}
}
Preview:
Maybe using dataProviders? Can you provide some example to help me pre-select options ? Thanks
-
How you achieve dropdown with select in product edit page?chirag dodia– chirag dodia2018年09月14日 07:54:33 +00:00Commented Sep 14, 2018 at 7:54
-
@chiragdodia it's the ui-select component of Magento, my above code shows how to achieve it. showCheckbox = true will show the checkboxesManuel Di Iorio– Manuel Di Iorio2018年09月14日 16:51:15 +00:00Commented Sep 14, 2018 at 16:51
4 Answers 4
Use default argument in config
<item name="default" xsi:type="string">1</item>
Final Code:
<field name="storeview">
<argument name="data" xsi:type="array">
...
<item name="config" xsi:type="array">
...
<item name="default" xsi:type="string">1</item>
...
</item>
</argument>
</field>
-
1I need to dinamically preselect multiple values of this multi-selectManuel Di Iorio– Manuel Di Iorio2018年05月25日 12:01:08 +00:00Commented May 25, 2018 at 12:01
-
I did not need to have this dynamically, just needed to set it in the XML, and this worked for me - thanks!quickshiftin– quickshiftin2020年01月17日 14:40:59 +00:00Commented Jan 17, 2020 at 14:40
Solved by injecting the data into the form dataProvider:
$this->loadedData[$model->getId()]["products"] = ["1", "3"]; // array of ID strings
-
could you please share full code, I am also not able to populate pre selected values?akgola– akgola2020年12月09日 08:28:32 +00:00Commented Dec 9, 2020 at 8:28
-
could you please share full code, I am also not able to populate pre selected valuesDevidas– Devidas2021年03月03日 12:10:41 +00:00Commented Mar 3, 2021 at 12:10
-
I don't work anymore on this company project and haven't got access anymore to the code, I apologize.Manuel Di Iorio– Manuel Di Iorio2021年03月03日 14:57:53 +00:00Commented Mar 3, 2021 at 14:57
enter image description here
Your loop working like this
public function toOptionArray()
{
$options = [];
$options[] =
[
'label' => 'New Tab',
'value' => '_blank'
];
$options[] =
[ 'label' => 'Same Tab',
'value' => '_self'
];
$options[] =
[ 'label' => 'test',
'value' => 'test'
]
};
And Please add component like below
<field name="urltarget">
<argument name="data" xsi:type="array">
<item name="options" xsi:type="object">Vendor\Module\Model\Targetoptions</item>
<item name="config" xsi:type="array">
<item name="dataType" xsi:type="string">text</item>
<item name="label" xsi:type="string" translate="true">Slide Url Open In</item>
<item name="formElement" xsi:type="string">select</item>
<item name="source" xsi:type="string">sliders</item>
<item name="sortOrder" xsi:type="number">5</item>
<item name="dataScope" xsi:type="string">urltarget</item>
</item>
</argument>
</field>
Magento 2 UI component form set default value for fields. Add the below code xml in your field config
<item name="default" xsi:type="string">1</item>
Like as:-
<field name="fieldname">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="default" xsi:type="number"1</item>
</item>
</argument>
</field>