Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Ben 19 posts 109 karma points
    Feb 20, 2019 @ 16:03
    Ben
    0

    Multiple classes in Grid Settings

    Hi All,

    Having some trouble trying to get multiple classes working on the grid settings.

    Use case: For the user to set a number of classes on grid rows/areas. For example select text colour, select background colour, select button colour.

    Id like to do this as a class rather than a style as it gives greater control.

    Ive set up the relevant json in the settings/class sections but it only adds one class.

    I searched around and found the package: umbraco-grid-settings, documentation

    But i cant get this to work, this is the grid view ive got at the moment:

       @inherits UmbracoViewPage<dynamic>
    @using Umbraco.Web.Templates
    @using Newtonsoft.Json.Linq
    @using Our.Umbraco.GridSettings.Web
    @using Our.Umbraco.GridSettings.Resolvers
    
    @{
        var attributesResolver = new GroupByPrefixTokenGridSettingsAttributesResolver("_");
    }
    
    @if (Model != null && Model.sections != null)
    {
        var oneColumn = ((System.Collections.ICollection)Model.sections).Count == 1;
    
        <div class="umb-grid">
            @if (oneColumn)
            {
                foreach (var section in Model.sections)
                {
                    <div class="grid-section">
                        @foreach (var row in section.rows)
                        {
                            @renderRow(row, true);
                        }
                    </div>
                }
            }
            else
            {
                <div class="container">
                    <div class="row clearfix">
                        @foreach (var s in Model.sections)
                        {
                            <div class="@("span" + s.grid) column">
                                @foreach (var row in s.rows)
                                {
                                    @renderRow(row, false);
                                }
                            </div>
                        }
                    </div>
                </div>
            }
        </div>
    }
    
    @helper renderRow(dynamic row, bool singleColumn)
    {
    
        <div @Html.RenderGridSettingAttributes(row as JObject)>
            <div class="d-md-flex flex-md-equal w-100">
                @foreach (var area in row.areas)
                {
                    <div @Html.RenderGridSettingAttributes(area as JObject)>
                        <div class="my-5 py-5 px-10">
                            <div class="@("span" + area.grid) column">
                                @foreach (var control in area.controls)
                                {
                                    if (control != null && control.editor != null && control.editor.view != null)
                                    {
                                        <text>@Html.Partial("grid/editors/base", (object)control)</text>
                                    }
                                }
                            </div>
                        </div>
                    </div>
                }
            </div>
        </div>
    }
    
    @functions {
        public static MvcHtmlString RenderElementAttributes(dynamic contentItem)
        {
            var attrs = new List<string>();
            JObject cfg = contentItem.config;
    
            if (cfg != null)
                foreach (JProperty property in cfg.Properties())
                {
                    var propertyValue = HttpUtility.HtmlAttributeEncode(property.Value.ToString());
                    attrs.Add(property.Name + "=\"" + propertyValue + "\"");
                }
    
            JObject style = contentItem.styles;
    
            if (style != null)
            {
                var cssVals = new List<string>();
                foreach (JProperty property in style.Properties())
                {
                    var propertyValue = property.Value.ToString();
                    if (string.IsNullOrWhiteSpace(propertyValue) == false)
                    {
                        cssVals.Add(property.Name + ":" + propertyValue + ";");
                    }
                }
    
                if (cssVals.Any())
                    attrs.Add("style=\"" + HttpUtility.HtmlAttributeEncode(string.Join(" ", cssVals)) + "\"");
            }
    
            return new MvcHtmlString(string.Join(" ", attrs));
        }
    }
    

    If anyone can offer any advice id really appreciate it.

  • Marius 6 posts 77 karma points
    Feb 28, 2019 @ 17:00
    Marius
    0

    Hi Ben, you can simply check if property not null or empty then add desired class depending on what you need.

  • Ben 19 posts 109 karma points
    Feb 28, 2019 @ 17:07
    Ben
    0

    Hi Marius,

    Thanks for the reply.

    Apologies im not sure i understand.

    I need the user to be able to select a number of settings on a grid row or area.

    The settings represent a class, so the user can select red as the background colour and this will add a class to the grid row/area.

  • Marius 6 posts 77 karma points
    Feb 28, 2019 @ 19:01
    Marius
    0

    Hi Ben, I did some testing (Umbraco 7.4.3) and I see what you mean. By the looks of it umbraco grid data type only takes one class from settings and ignores others. As a workaround you can create your custom key, lets say for each setting which you will use for different options the key will be class1, class2 etc. enter image description here

    then in you grid partial

    var attrs = new List<string>();
        string cssClassString = "class='";
        JObject cfg = contentItem.config;
        var countClasses = 0;
        var count = 0;
        if (cfg != null)
        {
            foreach (JProperty property in cfg.Properties())
            {
                if (property.Name.ToLower().Contains("class"))
                {
                    countClasses++;
                }
            }
            foreach (JProperty property in cfg.Properties())
            {
                if (property.Name.ToLower().Contains("class"))
                {
                    count++;
                    cssClassString = cssClassString + " " + property.Value.ToString();
                    if (count == countClasses)
                    {
                        attrs.Add(cssClassString + "'");
                    }
                }
                else
                {
                    attrs.Add(property.Name + "=\"" + property.Value.ToString() + "\"");
                }
    
            }
    
        }
    

    As an alternative you can always use stacked content plugin which is more flexible and easier to use for content editors. https://our.umbraco.com/packages/backoffice-extensions/stacked-content/

    I hope this will help.

  • Ben 19 posts 109 karma points
    Feb 28, 2019 @ 20:43
    Ben
    0

    Thanks Marius,

    Will give this a go. Will let you know how i get on.

    Will look into that package as well, i did try and use a package (umbraco-grid-settings) but couldnt get it working correctly, think i was missing some steps.

Please Sign in or register to post replies

Write your reply to:

Draft