首页 文章

Magento2 - 在Admin中显示多选类别属性选项时出现问题

提问于
浏览
1

我正在尝试从Installer脚本创建多选类别属性 . 该属性已创建 . 但我在Magento 2 - Manage Category页面中的选项值中遇到了问题 . 它只显示空白文本区域 .

我使用下面的安装程序脚本创建了这个:

/** @var EavSetup $eavSetup */
    $eavSetup = $this->_eavSetupFactory->create(['setup' => $setup]);

    /**
     * Add attributes to the eav/attribute for Category
     */

    $eavSetup->addAttribute(
        \Magento\Catalog\Model\Category::ENTITY,
        'class',
        [
            'backend'      => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
            'type'         => 'varchar',
            'label'        => 'Class',
            'group'        => 'General Information',
            'input'        => 'multiselect',
            'source'       => '',
            'global'       => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
            'visible'      => true,
            'required'     => false,
            'user_defined' => true,
            'sort_order'   => 100,
            'option'     => [
                'value' => [
                    'SET' => ['SET'],
                    'HE'  => ['HE'],
                    'HBR' => ['HBR'],
                ]
            ],
        ]
    );

在数据库中,我可以看到,属性是在eav表中创建的,选项和值也在那里创建 .

为了解决这个问题,我在我的自定义模块中添加了xml,它位于xml(.. \ view \ adminhtml \ ui_component \ category_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">
    <fieldset name="content">
        <field name="class">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="dataType" xsi:type="string">string</item>
                    <item name="formElement" xsi:type="string">multiselect</item>
                    <item name="sortOrder" xsi:type="number">70</item>
                    <item name="label" xsi:type="string" translate="true">Select Class</item>
                </item>
            </argument>
        </field>
    </fieldset>
</form>

但仍然在管理类别页面中的选项值中出现问题 . Textarea没有选择 . 我试图缓存:flush,static-content:deploy,setup:upgrade,indexer:reindex但仍有问题 . 我正在使用Magento2.1.1 CE . 我需要做什么吗?
enter image description here
谢谢

2 回答

  • 0

    您必须使用以下代码更正 option 键中的阵列配置:

    'option' => [
        'values' => ['SET', 'HE', 'HBR'],
    ],
    

    values 键用于添加选项,而 value 键则用于更新已存在的选项,因此请小心使用它们 .

    有关完整说明,请参阅 Magento\Eav\Setup\EavSetup 类中 addAttributeOption 方法的代码 .

  • 0
    source is blank thats why it showing blank in multi select use   
     'source'        => 'modulename/modelname',
    
    and in source file use 
    
    public function getAllOptions()
        {
            if (is_null($this->_options)) {
                $this->_options = array(array(
                    'label' => Mage::helper('catalog')->__('Label'),
                    'value' => 'value'
                ));
              }
            return $this->_options;
        }
    

相关问题