首页 > 解决方案 > 自定义修饰符中选择的默认值

问题描述

我有一个从如下定义的表单中选择的字段。如您所见,选择有两个值,是和否。我希望为此选择设置一个默认值。

"children" => [
    "bc_offer_is_duration_count_fixed" => [
        "arguments" => [
            "data" => [
                "config" => [
                    "dataType" => "select",
                    "formElement" => "select",
                    "visible" => "1",
                    "required" => "1",
                    "validation" => [
                        'required-entry' => "1"
                    ],
                    "default" => null,
                    "label" => __('Is duration count fixed'),
                    "scopeLabel" => __('[GLOBAL]'),
                    "code" => "bc_offer_offer_durations",
                    "source" => "content",
                    "globalScope" => true,
                    "sortOrder" => 10,
                    "componentType" => "field",
                    "component" => "Project_OfferProducts/js/form/element/offer-is-duration-count-fixed",
                    'options' => [['label' => __('Yes'), 'value' => '1'], ['label' => __('No'), 'value' => '0']]
                ]
            ]
        ]
    ]
]

modifyData它是此处定义的类的方法的一部分

use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\AbstractModifier as CatalogAbstractModifier;
abstract class AbstractModifier extends CatalogAbstractModifier

显然我已经尝试设置"default" => "Yes""default" => "1""default" => 1

我也有一个offer-js-duration-count-fixed.js包含该内容的文件

define([
'Magento_Ui/js/form/element/select',
'Project_OfferProducts/js/model/offer-configuration/context'
], function (Select, context) {
'use strict';

return Select.extend({
    updatingDurationFromField: false,
    initialize: function () {
        this._super();
        context.isDurationCountFixed.subscribe(function(newValue){
            this.refreshInField(newValue);
        }.bind(this));
        this.value.subscribe(this.refreshInGrid.bind(this));
        if(context.isDurationCountFixed())
        {
            this.value(1);
        }
        else
        {
            this.value(0);
        }
        return this;
    },
    refreshInGrid: function(){
        this.updatingDurationFromField = true;
        context.isDurationCountFixed(this.value());
        this.updatingDurationFromField = false;
    },
    refreshInField: function(newValue){
        if(!this.updatingDurationFromField)
        {
            this.value(context.isDurationCountFixed());
        }
    }
});});

我在 Magento 2 下。

标签: magentomagento2

解决方案


正是我的想法

改变这个

'options' => [['label' => __('Yes'), 'value' => '1'], ['label' => __('No'), 'value' => '0']]

'options' => [['label' => __('Yes'), 'value' => 1], ['label' => __('No'), 'value' => 0]]

和改变

"default" => null,

"default" => "1",

推荐阅读