3

I am trying to figure out if i can have multiple columns and multiple rows for one particular field under an item in Content Editor.

Field Name:
[column 1][column 2][column 3][column 4][column 5]

This serves as a drawing. So in Content Editor, where you can fill out Details of the item, I would like to have multiple columns and multiple rows. So the data will be served under one particular "Type". Hopefully this makes sense

  • Can the rows be predefined (for example each item will have 5 rows) or do they need to be different for each item? – Koen Heye Jan 26 '17 at 20:29
2

I think you are talking about custom field type.

Looks like you are talking about something that cannot be done with standard field types thus you will have to create your own custom field type for that

How to create custom field type

  1. Implement your custom field
namespace Sitecore.Playground.Fields.FieldTypes
{
    public class CustomField : Edit
    {         
        protected override void DoRender(HtmlTextWriter output)
        {
            Attributes["placeholder"] = Translate.Text(Placeholder);
            string str = " type=\"hidden\"";
            SetWidthAndHeightStyle();
            output.Write("<input" + ControlAttributes + str + ">");
            RenderChildren(output);
            // render your custom control here
        }
    }
}

Notice output.Write usage. Now you have to build your own HTML with rows, columns and then store a raw value of the field in <input>.

Perhaps you will have to override Value property from the base class to properly serialise/deserialize stored value.

As you probably know each field value can be expressed as Raw Value. Idea is that your field raw value could be for example:

1|2|3|4;5|6|7;8|9|0

then in C# code, you need to properly render those values as rows/columns. This is an example, I don't know what you want to store there.

  1. Register your custom field with config
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
    <sitecore>
        <controlSources>
            <source mode="on" namespace="Sitecore.Playground.Fields.FieldTypes" assembly="Sitecore.Playground" prefix="myCustomPrefix" />
        </controlSources>
    </sitecore>
</configuration>
  1. Create field type item in core database

    /sitecore/system/Field types/Developer Types/My Custom field

Fill Control field with this value: myCustomPrefix:CustomField

This blog post might be useful for you:

https://www.sitecore.net/company/blog/474/creating-a-custom-sitecore-field-4246

2

I can't give you a complete solution but I can guide you a bit to along the right path.

You'll need a custom field type for this. Credit here goes to jammykam

Who wrote a nice blog article about creating a key/value data field with lookup: https://jammykam.wordpress.com/2015/03/08/custom-sitecore-field-for-storing-keyvalue-data-with-lookups/

I've already implemented it in a solution and it works perfectly.
You'll need to define your column headers somewhere. Cfr. Chemical Types in the example.

And then, depending on how flexible you want your rows to be, you either:

  1. Create a template that has a fixed number of your custom field type.
  2. Or create a template that has only 1 field of your custom field type. You then use this template to create children under the item. So each child = 1 row with data.

Here's a link to the gist of the code: https://gist.github.com/jammykam/d935b2c5b6054ca10282 You'll probably need to make some tweaks to it. Like if you don't want to show the 'Chemical unit'.

Hope this helps! If you have questions, just ask!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.