Don't miss out Virtual Happy Hour today (April 26).

Try our conversational search powered by Generative AI!

How to hide some properties from table of PropertyList<T>

Vote:
 

Hi guys,

Since EPiServer 9, they support PropertyList in model, I wonder how I can hide some property from table?

In example, there are QuestionListPage and question class, I implement code like below. Now, I want to hide Id property in Question class from Editmode (from both display table and create/edit dialog). How can I hide it?

 I also try to add [ScaffoldColumn(false)] or [Ignore] on top of Id property but Id can only hide in create/edit dialog, not hide in displaytable. 

Another question is how can I create new GUID string value and add to Id when a question has been created?

public class QuestionListPage : PageData
    {
        [EditorDescriptor(EditorDescriptorType = typeof(CollectionEditorDescriptor))]
        public virtual IList QuestionList { get; set; }
    }
public class Question
    {
        public string Id { get; set; }
        public string QuestionText{ get; set; }
    }

    [PropertyDefinitionTypePlugIn]
    public class PropertyQuestionList : PropertyList
    {
        protected override Question ParseItem(string value)
        {
            return JsonConvert.DeserializeObject(value);
        }

        public override PropertyData ParseToObject(string value)
        {
            ParseToSelf(value);
            return this;
        }
    }

Thanks,

Linh

#175509
Feb 22, 2017 11:08
Vote:
 

Hi,

In order to hide the ID property from edit mode, there are two things you need to do.
First, to hide in from the create/edit template, simply add the  ScaffoldColumn(false) attribute as you've already did.

Second, to hide it from the table, you'll need to add a custom editor descriptor and tell the editor to exclude the ID column. Here's an example:

    public class MyCustomCollectionEditorDescriptor : CollectionEditorDescriptor<Question>
    {
        public override void ModifyMetadata(ExtendedMetadata metadata, IEnumerable<Attribute> attributes)
        {
            this.GridDefinition.ExcludedColumns.Add(nameof(Question.Id));
            base.ModifyMetadata(metadata, attributes);
        }
    }

Hope that helps!

#175514
Feb 22, 2017 11:58
Vote:
 

As for creating a new ID when creating a new object, you could simply add a backing field and assign a new value in the getter:

        private string _id;
        [ScaffoldColumn(false)]
        public string Id
        {
            get
            {
                if (string.IsNullOrEmpty(_id))
                    _id = Guid.NewGuid().ToString();

                return _id;
            }
            set { _id = value; }
        }
#175515
Edited, Feb 22, 2017 11:59
Vote:
 

Thank you Per Magne,

This is exactly what I'm looking for and this codes work great :)

#175603
Feb 24, 2017 4:53
Vote:
 

I can´t get this to work unfortunately. Not sure if I´m missing something or if it´s no longer working for later version of CMS (we´re running 10.9.0).

I have a page type called StartLandingPage. There I have a property of type PropertyList, it looks like this:

[EditorDescriptor(EditorDescriptorType = typeof(CollectionEditorDescriptor<IntegrationToolModel>))]
public virtual IList<IntegrationToolModel> ToolIntegrationTabs { get; set; }

I then have an EditorDescriptor class that looks like this:

public class IntegrationToolEditorDescriptor : CollectionEditorDescriptor<IntegrationToolModel>
    {
        public override void ModifyMetadata(ExtendedMetadata metadata, IEnumerable<Attribute> attributes)
        {
            GridDefinition.ExcludedColumns.Add(nameof(IntegrationToolModel.ApiUrl));
            GridDefinition.ExcludedColumns.Add(nameof(IntegrationToolModel.HideOnDesktop));
            GridDefinition.ExcludedColumns.Add(nameof(IntegrationToolModel.HideOnMobile));
            GridDefinition.ExcludedColumns.Add(nameof(IntegrationToolModel.FormResult));
            GridDefinition.ExcludedColumns.Add(nameof(IntegrationToolModel.CountryCode));
            GridDefinition.ExcludedColumns.Add(nameof(IntegrationToolModel.DestinationUrl));
            GridDefinition.ExcludedColumns.Add(nameof(IntegrationToolModel.ResultPage));
            base.ModifyMetadata(metadata, attributes);
        }
    }

Where I specify a bunch of properties of IntegrationToolModel object that I don´t want to show in the table view. But it´s not working, they are not hidden from the view.

What am I missing or doing wrong?

#179508
Jun 14, 2017 13:20
Vote:
 

I know this is an older post, but just in case someone is having the same issue like Petra, you need to:

  • remove the EditorDescriptor attribute from your property, because it tells Episerver to use the default editor
  • register the new CollectionEditorDescriptor using the EditorDescriptorRegistration attribute, and setting the correct TargetType property

The updated code would look like this:

public virtual IList<IntegrationToolModel> ToolIntegrationTabs { get; set; }

[EditorDescriptorRegistration(TargetType = typeof(IList<IntegrationToolModel>))]
public class IntegrationToolEditorDescriptor : CollectionEditorDescriptor<IntegrationToolModel>
    {
        public override void ModifyMetadata(ExtendedMetadata metadata, IEnumerable<Attribute> attributes)
        {
            GridDefinition.ExcludedColumns.Add(nameof(IntegrationToolModel.ApiUrl));
            GridDefinition.ExcludedColumns.Add(nameof(IntegrationToolModel.HideOnDesktop));
            GridDefinition.ExcludedColumns.Add(nameof(IntegrationToolModel.HideOnMobile));
            GridDefinition.ExcludedColumns.Add(nameof(IntegrationToolModel.FormResult));
            GridDefinition.ExcludedColumns.Add(nameof(IntegrationToolModel.CountryCode));
            GridDefinition.ExcludedColumns.Add(nameof(IntegrationToolModel.DestinationUrl));
            GridDefinition.ExcludedColumns.Add(nameof(IntegrationToolModel.ResultPage));
            base.ModifyMetadata(metadata, attributes);
        }
    }
#180647
Edited, Jul 19, 2017 13:58
Vote:
 

Big thanks Cristi! This works just like I wanted! Nice!

#181033
Aug 07, 2017 11:38
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* 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.