首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自定义字段OrchardCMS

自定义字段OrchardCMS
EN

Stack Overflow用户
提问于 2016-01-24 16:32:12
回答 1查看 99关注 0票数 1

我使用以下方法创建自定义下拉字段,以将其插入到我的自定义表单中。现在我需要的是配置从管理面板的过滤器purpose.Like从管理面板这个字段,我将能够在文本框中编写一个过滤器,该过滤器将与ajax调用作为一个参数,从数据库中检索过滤数据。

我非常感谢你在这方面的帮助。

MyModule/Fields/MyCustomField.cs:

代码语言:javascript
复制
public class MyCustomField : ContentField {
    public string SelectedValue {
        get { return Storage.Get<string>(); }
        set { Storage.Set(value); }
    }
}

MyModule/Drivers/MyCustomFieldDriver.cs:

代码语言:javascript
复制
public class MyCustomFieldDriver : ContentFieldDriver<MyCustomField> {

    // EditorTemplates/Fields/MyCustom.cshtml
    private const string TemplateName = "Fields/MyCustom";

    private static string GetPrefix(ContentField field, ContentPart part) {
        // handles spaces in field names
        return (part.PartDefinition.Name + "." + field.Name)
               .Replace(" ", "_");
    }

    protected override DriverResult Display(ContentPart part, MyCustomField field, string displayType, dynamic shapeHelper) {
        return ContentShape("Fields_MyCustom",
            field.Name,
            f => f.Name(field.Name)
                .SelectedValue(field.SelectedValue));
    }

    protected override DriverResult Editor(ContentPart part, MyCustomField field, dynamic shapeHelper) {
        return ContentShape("Fields_MyCustom_Edit", () => shapeHelper.EditorTemplate(
            TemplateName: TemplateName,
            Model: field,
            Prefix: GetPrefix(field, part)));
    }

    protected override DriverResult Editor(ContentPart part, MyCustomField field, IUpdateModel updater, dynamic shapeHelper) {
        updater.TryUpdateModel(field, GetPrefix(field, part), null, null);
        return Editor(part, field, shapeHelper);
    }
}

MyModule/Views/Fields/MyCustom.cshtml:

代码语言:javascript
复制
@{ 
    var selectedValue = Model.SelectedValue;
}

<h1>@selectedValue</h1>
MyModule/Views/EditorTemplates/Fields/MyCustom.cshtml:

@model MyModule.Fields.MyCustomField

<select id="@Html.IdFor(m => m.SelectedValue)" name="@Html.NameFor(m => m.SelectedValue)"></select>

@using (Script.Foot()) {
    Script.Require("jQuery");

    <script>

        $(function () {
            // your own url ofcourse
            var url = 'http://jsonplaceholder.typicode.com/users',
                dd = $("#@Html.IdFor(m => m.SelectedValue)");

            $.getJSON(url, function (data) {
                $.each(data, function () {
                    dd.append("<option value='" + this.name + "'>" + this.name + "</option>");
                });
            });
        });
    </script>
}

MyModule/Placement.info:

代码语言:javascript
复制
<Placement>
  <Place Fields_MyCustom_Edit="Content:3" />
  <Place Fields_MyCustom="Content:3" />
</Placement>
EN

回答 1

Stack Overflow用户

发布于 2016-01-25 03:32:09

在OrchardCMS中,我们可以创建自定义字段。每个字段都应该由管理员处理,以使其通用,为此,orchard提供了用户设置选项,使事情变得如此容易。因此,我们可以说,在orchardCMS中,当我们创建任何自定义字段时,我们也有一个设置部分。我们要做的就是创建一个setting文件夹,在该文件夹中我们需要创建一个setting类,比如DropDownFieldSetting.cs,在这里我们需要编写管理员需要将其视为设置的公共属性。设置/DropDownFieldSettings.cs

代码语言:javascript
复制
   public class DropDownFieldSettings
    {
            public string Filter { get; set; }
    }

和设置文件夹settings/DropDownFieldEditorEvents.cs下的另一个类

代码语言:javascript
复制
public class DropDownFieldEditorEvents : ContentDefinitionEditorEventsBase 
    {
        public override IEnumerable<TemplateViewModel>
          PartFieldEditor(ContentPartFieldDefinition definition)
        {
            if (definition.FieldDefinition.Name == "DropDownCustomField")
            {
                var model = definition.Settings.GetModel<DropDownFieldSettings>();
                yield return DefinitionTemplate(model);
            }
        }

        public override IEnumerable<TemplateViewModel> PartFieldEditorUpdate(ContentPartFieldDefinitionBuilder builder, IUpdateModel updateModel)
        {

            var model = new DropDownFieldSettings();
            if (updateModel.TryUpdateModel(model, "DropDownFieldSettings", null, null))
            {
                builder.WithSetting("DropDownFieldSettings.Filter", model.Filter);               
                yield return DefinitionTemplate(model);
            }

        }
    }

当管理员在filter Field中输入一些值时,将调用以下PartFieldEditorUpdate函数。在此函数中,此行保存您的设置

代码语言:javascript
复制
builder.WithSetting("DropDownFieldSettings.Filter",model.Filter);

要全面了解这一切,我们可以查看OrcharCMS根目录下的模块文件夹下的Orchard.Fields模块。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34973679

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档