2016-01-24 80 views
1

我已经使用以下方法来创建自定义下拉字段,以将其插入到我的自定义窗体中。现在我需要的是从管理面板配置此字段以实现过滤目的。与管理面板类似,我将能够在文本框中编写过滤器,并且该过滤器将以ajax调用作为参数从数据库检索过滤的数据。自定义字段OrchardCMS

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

MyModule的/场/ MyCustomField.cs:

public class MyCustomField : ContentField { 
    public string SelectedValue { 
     get { return Storage.Get<string>(); } 
     set { Storage.Set(value); } 
    } 
} 

MyModule的/驱动器/ MyCustomFieldDriver.cs:

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中/查看/场/ MyCustom.cshtml:

@{ 
    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:

<Placement> 
    <Place Fields_MyCustom_Edit="Content:3" /> 
    <Place Fields_MyCustom="Content:3" /> 
</Placement> 

回答

1

在OrchardCMS中,我们可以创建自定义字段。每个领域都应该由管理员处理,以使其具有通用性,为此目的,果园通过提供设置选项让事情变得如此简单。 所以我们可以说在orchardCMS中,当我们创建任何自定义字段时,我们也有一个设置部分。我们所要做的就是创建一个设置文件夹,并且在该文件夹中我们需要创建一个设置类,比如DropDownFieldSetting.cs,在这里我们需要编写需要被管理员视为设置的公共属性。 设置/ DropDownFieldSettings.cs

public class DropDownFieldSettings 
    { 
      public string Filter { get; set; } 
    } 

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

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); 
      } 

     } 
    } 

当管理员进入过滤器领域的一些值以下PartFieldEditorUpdate函数被调用。这里在这个函数这一行保存设置

builder.WithSetting("DropDownFieldSettings.Filter",model.Filter); 

对于这一切我们可以采取在OrcharCMS根模块文件夹下看看Orchard.Fields模块完整的想法。