2016-01-24 79 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模塊完整的想法。