N2 checkboxlist attribute

N2 checkboxlist attribute

I have used N2 CMS a lot without delving into creating a custom editor, until I needed an editable checkbox list property.

My experience of N2 CMS at this point, only ever involved storing an int or string ... it never involved storing a string array. So my initial idea was as follows


[Editable("Divisions", typeof(CheckBoxList), "SelectedValue", 85, DataBind = true, ContainerName = Tabs.Content)]
[EditorModifier("DataSource", new string[] { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6" })]
public virtual string[] Divisions
{
    get { return (string[])(GetDetail("Divisions")); }
    set { SetDetail("Divisions", value); }
}

The issue here was that only the first selected option was being persisted. So began my journey into creating a custom editor, and after many trials and tribulations, I devised the following solution.


public class EditableCheckBoxListAttribute : AbstractEditableAttribute
{
    public override void UpdateEditor(N2.ContentItem item, Control editor)
    {
        CheckBoxList lst = editor as CheckBoxList;
        if (lst != null)
        {
            foreach(ListItem li in lst.Items)
            {
                if (item[this.Name].ToString().Contains(li.Value))
                {
                    li.Selected = true;
                }
            }
        }
    }

    public override bool UpdateItem(N2.ContentItem item, Control editor)
    {
        CheckBoxList lst = editor as CheckBoxList;
        ArrayList items = new ArrayList();
        foreach (ListItem li in lst.Items)
        {
            if (li.Selected)
            {
               items.Add(li.Value);
            }
        }
        string[] itemID = (string[])items.ToArray(typeof(string));
        item[this.Name] = String.Join(",",itemID);
        return true;
    }

    protected override Control AddEditor(Control container)
    {
        CheckBoxList lst = new CheckBoxList();
        var items = N2.Find.Items
            .Where.Type.Eq(typeof(TestItem))
            .Filters(new NavigationFilter())
            .Select();
        foreach (TestItem i in items)
        {
            lst.Items.Add(new ListItem(i.Title, i.ID.ToString()));
        }
        container.Controls.Add(lst);
        return lst;
    }
}

UpdateItem method uses an ArrayList to temporarily store the selected list item values, then converts that ArrayList to a string array, and finally uses the string join function to concatenate the string array with a comma for persisting the data into N2.

UpdateEditor method iterates through all the checkbox list items, if the list item value is contained in the editable attribute, then that list item selected property is set to true.

AddEditor methods creates the list items and adds the checkbox list to the interface.

And this is how you use the editable checkbox list attribute


[EditableCheckBoxList(Title = "Items")]
public virtual string Items
{
    get { return (string)(GetDetail("Items", "")); }
    set { SetDetail("Items", value, ""); }
}

No comments (Add your own)

Add a New Comment


code
 

Comment Guidelines: No HTML is allowed. Off-topic or inappropriate comments will be edited or deleted. Thanks.