Magento 2:所选类别在编辑表单中不可见


8

使用ui组件以管理形式添加了类别选择器。类别ID作为逗号分隔的值保存到数据库中。现在,我想在我的编辑表单中显示该类别。 在此处输入图片说明

在此处输入图片说明 这是我的example_example_edit.xml文件:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <uiComponent name="example_form"/>
        </referenceContainer>
    </body>
</page>

这是我的example_form.xml

<?xml version="1.0" encoding="UTF-8"?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <argument name="data" xsi:type="array">
        <item name="js_config" xsi:type="array">
            <item name="provider" xsi:type="string">example_form.example_form_data_source</item>
            <item name="deps" xsi:type="string">example_form.example_form_data_source</item>
        </item>
        <item name="label" xsi:type="string" translate="true">example Information</item>
        <item name="config" xsi:type="array">
            <item name="dataScope" xsi:type="string">data</item>
            <item name="namespace" xsi:type="string">example_form</item>
        </item>
        <item name="template" xsi:type="string">templates/form/collapsible</item>
        <item name="buttons" xsi:type="array">
            <item name="save" xsi:type="string">Company\Module\Block\Adminhtml\Example\Edit\Button\Save</item>
            <item name="delete" xsi:type="string">Company\Module\Block\Adminhtml\Example\Edit\Button\Delete</item>
            <item name="back" xsi:type="string">Company\Module\Block\Adminhtml\Example\Edit\Button\Back</item>        
        </item>
    </argument>

    <dataSource name="example_form_data_source">
        <argument name="dataProvider" xsi:type="configurableObject">
            <argument name="class" xsi:type="string">Company\Module\Model\ResourceModel\Example\DataProvider</argument>
            <argument name="name" xsi:type="string">example_form_data_source</argument>
            <argument name="primaryFieldName" xsi:type="string">example_id</argument>
            <argument name="requestFieldName" xsi:type="string">example_id</argument>
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="submit_url" xsi:type="url" path="module/example/save"/>
                </item>
            </argument>
        </argument> 
        <argument name="data" xsi:type="array">
            <item name="js_config" xsi:type="array">
                <item name="component" xsi:type="string">Magento_Ui/js/form/provider</item>
            </item>
        </argument>
    </dataSource>

    <fieldset name="example_details">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="collapsible" xsi:type="boolean">true</item>
                <item name="label" xsi:type="string" translate="true">example [General]</item>
                <item name="sortOrder" xsi:type="number">10</item>
                <item name="openOnShow" xsi:type="boolean">true</item>
            </item>
        </argument>
        <field name="example_id">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="visible" xsi:type="boolean">false</item>
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="formElement" xsi:type="string">input</item>
                    <item name="source" xsi:type="string">label_id</item>
                </item>
            </argument>
        </field>
        <field name="title">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string">Title</item>
                    <item name="visible" xsi:type="boolean">true</item>
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="disabled" xsi:type="boolean">false</item>
                    <item name="formElement" xsi:type="string">input</item>
                    <item name="source" xsi:type="string">title</item>
                    <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="boolean">true</item>
                    </item>
                </item>
            </argument>
        </field>
        <field name="status">
            <argument name="data" xsi:type="array">
                <item name="options" xsi:type="object">Company\Module\Model\Config\Source\Status</item>
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string" translate="true">Status</item>
                    <item name="visible" xsi:type="boolean">true</item>
                    <item name="dataType" xsi:type="string">number</item>
                    <item name="formElement" xsi:type="string">select</item>
                    <item name="source" xsi:type="string">status</item>
                    <item name="dataScope" xsi:type="string">status</item>
                    <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="boolean">true</item>
                    </item>
                </item>
            </argument>
        </field>
<field name="category_ids">
            <argument name="data" xsi:type="array">
                <item name="options" xsi:type="object">Magento\Catalog\Ui\Component\Product\Form\Categories\Options</item>
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string" translate="true">Category</item>
                    <item name="componentType" xsi:type="string">field</item>
                    <item name="formElement" xsi:type="string">select</item>
                    <item name="visible" xsi:type="boolean">true</item>
                    <item name="component" xsi:type="string">Magento_Catalog/js/components/new-category</item>
                    <item name="elementTmpl" xsi:type="string">ui/grid/filters/elements/ui-select</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="multiple" xsi:type="boolean">true</item>
                    <item name="levelsVisibility" xsi:type="number">1</item>
                    <item name="required" xsi:type="boolean">false</item>
                    <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="boolean">false</item>
                    </item>
                    <item name="listens" xsi:type="array">
                        <item name="${ $.namespace }.${ $.namespace }:responseData" xsi:type="string">setParsed</item>
                    </item>
                </item>
            </argument>
        </field>
    </fieldset> 

</form>

这是我的DataProvider.php

<?php
namespace Company\Module\Model\ResourceModel\Example;

use Company\Module\Model\ResourceModel\Example\CollectionFactory;;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;  

class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
{
    /**
     * @var array
     */
    protected $_loadedData;

    protected $storeManager;

    protected $scopeConfig;

    public function __construct(
        $name,
        $primaryFieldName,
        $requestFieldName,
        CollectionFactory $exampleCollectionFactory,
        StoreManagerInterface $storeManager,
        ScopeConfigInterface $scopeConfig,
        array $meta = [],
        array $data = []
    ) {
        $this->storeManager = $storeManager;
        $this->scopeConfig = $scopeConfig;
        $this->collection = $exampleCollectionFactory->create();
        parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
    }


    /**
    * Get data
    *
    * @return array
    */
    public function getData()
    {
        if (isset($this->_loadedData)) {
            return $this->_loadedData;
        }

    return $this->_loadedData;
    }
}

您使用哪个主题和magento 2(哪个版本)
KGR

Magento版本 2.1.2 {Luma theme}
EB

Answers:


5

在这种情况下,您需要修改getData函数。

对于edit category_ids值,必须是一个数组。假设category_ids值以逗号分隔的形式保存在db中,表示为'7,9,22'。因此,当您编辑此值时,应为[7,9,22]。

/ **
 *获取数据
 *
 * @返回数组
 * /
公共函数getData()
{
    如果(isset($ this-> loadedData)){
        返回$ this-> loadedData;
    }

    $ items = $ this-> collection-> getItems();

    foreach($ items as $ item){
        $ data = $ item-> getData();
        $ data ['category_ids'] = explode(',',$ data ['category_ids']);
        $ result ['example_details'] = $ data;
        $ this-> loadedData [$ item-> getId()] = $ result;
    }
    返回$ this-> loadedData;
}


2

在您的example_form.xml中, 用于字段category_ids

改变这个

<item name="formElement" xsi:type="string">select</item>

<item name="formElement" xsi:type="string">multiselect</item>

确保您正在获取字段category_ids的值,以逗号分隔的值由magento负责。


我已将代码从“选择”更改为“多选”。仍然无法正常工作。此外,类别ID也保存为逗号分隔的值。
EB

可能category_ids没有值。请检查您是否在该领域获得价值。数据库中的字段名称是否相同?
Priyank

是的,该字段的价值正在增加。类别也在下拉菜单中选择。但是这些值未显示在文本字段中。
EB

1

getSelected()组件js文件中是否有方法

Magento_Catalog / js / components / new-category

如果不是,那么您将需要创建它并返回带有标签的数组。

getSelected: function () {
    var selected = this.value();
    if (selected && selected.indexOf(',') > -1) // split if contains comma only
        selected = selected.split(',');

    return this.cacheOptions.plain.filter(function (opt) {
        return _.isArray(selected) ?
            _.contains(selected, opt.value) :
        selected == opt.value;
    });
},

看到 vendor/magento/module-ui/view/base/web/js/form/element/ui-select.js


谢谢。有效。现在,我无法添加/删除类别。请看截图。i.prntscr.com/1813b60d309d4a1ab1aac12901b3ae01.png
EB

编辑类别时出现js错误。请检查以下内容:i.prntscr.com/53697deb565d41c2bd482ba15ed5bc43.png
EB

更改var selected = this.value().split(",");并首先检查是否有值,然后按逗号分割。我已经用支票更新了答案。你可以再试一次吗?
米罗斯拉夫·彼得罗夫

我努力了。但这不起作用。i.prntscr.com/186eececf7d546f59f7184ff19cb730a.png
EB

现在呢?我更新了代码
Miroslav Petroff'1

0

您必须从Db获取ID,然后在创建选项时将其选中

$selectedCat = explode(',', $categoryFromDb)
foreach ($category as $opt) {
    $sel = '';
    if (in_array($opt, $selectedCat)) {
    $sel = ' selected="selected" ';
    }
    echo '<option ' . $sel . ' value="' . $opt . '">' . $opt . '</option>';
}

类别字段是使用ui组件创建的。
EB

@EB请输入您的代码,以根据您的代码获得正确的答案。
Ashish Jagnani

请查看更新的代码。
EB
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.