Try our conversational search powered by Generative AI!

CollectionEditorDescriptor - How to display a list of complex types?

Vote:
 

Hi Guys,

First off apologies if this has already been posted.

That aside, Now for my issue....

I have the following class structure:

public class MenuItem

{

public virtual LinkItemCollection Links { get; set; }

}

public class MenuSubCategory

{

public virtual string SubCategoryTitle { get; set; }

[EditorDescriptor(EditorDescriptorType = typeof(CollectionEditorDescriptor))]
public virtual IList MenuItems { get; set; }

}

public class MenuCategory

{

public virtual string CategoryTitle { get; set; }

[EditorDescriptor(EditorDescriptorType = typeof(CollectionEditorDescriptor))]
public virtual IList MenuSubCategories { get; set; }

}

In addition to this I also have a number of PropertyDefinitionTypePlugIns defined which look similar to this:

[PropertyDefinitionTypePlugIn]
public class MenuCategoriesProperty : PropertyList

{

protected override MenuCategory ParseItem(string value)

{

return JsonConvert.DeserializeObject(value);

}

public override PropertyData ParseToObject(string value)

{

ParseToSelf(value);
return this;

}

}

And finally I have added the flowwing property to my Block:

[EditorDescriptor(EditorDescriptorType = typeof(CollectionEditorDescriptor))]
public virtual IList MenuCategories { get; set; }

Now, when I attempt to edit the above itall works fine.  The issue I have is that episerver doesn't seem to know how to render my complex type so in edit mode, when I add a new Menu Categor, I see the following:

Cards       [object Object], object Object]

What I would like it to display is:

Cards        Birthday, Thank You, Well Done....

All that explained my question is, how would I go about making the above dislpay correctly?

Thanks in advance!

#185960
Dec 07, 2017 14:02
Vote:
 

Hi Darren,

If you need to change how something is being formatted in the editor then you can specify a custom formatter for that property.

First, create an extension to the default episerver collection editor. This will look something like this...

define([
"dojo/_base/array",
"dojo/_base/declare",
"dojo/_base/lang",
"epi-cms/contentediting/editors/CollectionEditor"
],
function (
array,
declare,
lang,
CollectionEditor
) {
return declare([CollectionEditor], {
_getGridDefinition: function () {
var result = this.inherited(arguments);

result.menuSubCategories.formatter = function (values) {
return values.map(msc => msc.subCategoryTitle).join();
};

return result;
},
});
});

...lets save that as MenuSubCategory.js.

This is hooking into the grid definition code, and telling episerver to use a custom formatter for our MenuSubCategories property.

Now, to wire this up, you can add an attribute to the MenuSubCategories property in your MenuCategory object:

public class MenuCategory
{
public virtual string CategoryTitle { get; set; }

[EditorDescriptor(EditorDescriptorType = typeof(CollectionEditorDescriptor<MenuSubCategory>))]
[ClientEditor(ClientEditingClass = "path/to/MenuSubCategories")]
public virtual IList<MenuSubCategory> MenuSubCategories { get; set; }
}

..and that should do the job. Now, when the editor loads, it should go into the custom formatter we specified, and whatever you return from this will be what is rendered.

Follow exactly the same pattern for MenuItems property on your MenuSubCategory object.

#186251
Edited, Dec 14, 2017 16:04
Vote:
 

The above worked perfectly for me, with just a few tripping points:

  • Probably obvious from Darren's example: The properties come through with the first character of the property name in lowercase, regardless of the capitalisation on your model in code (Your model property MenuSubCategories must be interacted with as menuSubCategories)
  • I'd recommend an if statement around the map - If the property doesn't exist (e.g. if the model's field is renamed), not checking for a null / undefined on the property will result in the entire table not being rendered
  • I had to add the full editor path to the definition for it to properly be recognised, as follows:
define("path/to/MenuSubCategories", [
  "dojo/_base/array",
  "dojo/_base/declare",
  "dojo/_base/lang",
  "epi-cms/contentediting/editors/CollectionEditor"
  ],
  function (
    array,
    declare,
    lang,
    CollectionEditor
  )

Thanks!

#205493
Edited, Jul 15, 2019 8:12
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.