Umbraco 表单中下拉菜单的条件逻辑

问题描述 投票:0回答:1

有没有办法在表单的下拉列表选项上添加条件逻辑?根据另一个表单字段,我可以添加条件逻辑来显示或隐藏下拉字段本身,但有没有办法通过 umbraco 将该逻辑应用于下拉字段选项。我正在使用 Umbraco 13。

示例:下拉字段 1 有选项 A、B 和 C,如果在下拉字段 1 中选择了 A,则显示下拉字段 2 的选项 E、F,否则显示 G、H

umbraco umbraco-forms
1个回答
0
投票

您可以创建自己的自定义 Umbraco 表单字段类型来实现这一目标。但是,通常最好使用“字段上内置的 Umbraco Forms 条件逻辑”来显示或隐藏其他字段,正如您在问题中所描述的那样。此选项将使您免于创建自己的自定义字段。 详情:

示例自定义字段类型 - 您可以按照此逻辑创建自己的自定义下拉字段类型(答案类型)。

using Umbraco.Cms.Core.Composing; using Umbraco.Forms.Core.Attributes; using Umbraco.Forms.Core.Enums; using Umbraco.Forms.Core.Providers; namespace MyProject; public class SliderFieldType : Core.FieldType { public SliderFieldType() { Id = new Guid("6dff0075-598c-4345-89d7-e0db8684c819"); Name = "Slider"; Alias = "slider"; Description = "Render a UUI Slider field."; Icon = "icon-autofill"; DataType = FieldDataType.String; SortOrder = 10; FieldTypeViewName = "FieldType.Slider.cshtml"; EditView = "My.PropertyEditorUi.InputNumber"; PreviewView = "My.FieldPreview.Slider"; } [Setting("Minimum", Description = "Minimum value", View = "Umb.PropertyEditorUi.Integer", DisplayOrder = 10)] public virtual string? Min { get; set; } = "1"; [Setting("Maximum", Description = "Maximum value", View = "Umb.PropertyEditorUi.Integer", DisplayOrder = 20)] public virtual string? Max { get; set; } = "1"; [Setting("Step", Description = "Step size", View = "Umb.PropertyEditorUi.Integer", DisplayOrder = 30)] public virtual string? Step { get; set; } = "1"; [Setting("Default Value", Description = "Default value", View = "Umb.PropertyEditorUi.Integer", DisplayOrder = 40)] public virtual string? DefaultValue { get; set; } = "1"; [Setting("Hide step values", Description = "Hides the numbers representing the value of each steps. Dots will still be visible", View = "Umb.PropertyEditorUi.Toggle", DisplayOrder = 50)] public virtual string? HideStepValues { get; set; } [Setting("Background color", Description = "Background color for the input field", View = "My.PropertyEditorUi.InputColor", DisplayOrder = 60)] public virtual string? BgColor { get; set; } = "1"; }

将新字段注册为依赖项:

using Umbraco.Cms.Core.Composing; using Umbraco.Cms.Core.DependencyInjection; using Umbraco.Forms.Core.Providers; namespace MyProject; public class Startup : IComposer { public void Compose(IUmbracoBuilder builder) { builder.WithCollectionBuilder<FieldCollectionBuilder>() .Add<SliderFieldType>(); } }

新字段类型的部分视图:

@using Umbraco.Forms.Web @model Umbraco.Forms.Web.Models.FieldViewModel @{ var min = Model.GetSettingValue<int>("Min", 1); var max = Model.GetSettingValue<int>("Max", 10); var step = Model.GetSettingValue<int>("Step", 1); var bgColor = Model.GetSettingValue<string>("BgColor", "#fff"); } <div>This is a custom "slider" field type. We'll just use an input to mock this up.</div> <input name="@Model.Name" style="background-color: @bgColor" id="@Model.Id" class="text @Html.GetFormFieldClass(Model.FieldTypeName)" value="@Model.ValueAsHtmlString" type="number" min="@min" max="@max" step="@step" />

	
© www.soinside.com 2019 - 2024. All rights reserved.