Copied to clipboard

Flag this post as spam?

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


  • Matthew Kirschner 323 posts 611 karma points
    Feb 28, 2017 @ 17:03
    Matthew Kirschner
    0

    Html Helper to Get Recursive Grid Content

    From what I can tell, it is currently impossible in Umbraco to get the grid content of an ancestor if the child node already has a grid property of the same name. This is because the recursive GetProperty method still sees the empty grid as having a value.

    For example, I have a "Page" document type with a grid editor named "sidebarGrid". In the Content section, I create one root "Page" node with several "Page" children. Only the root node has content in the "sidebarGrid" property, while all children leave this property empty.

    One would think that the following call would fetch the root node's "sidebarGrid" content for all child pages:

    @Html.GetGridHtml(Model.Content.GetProperty("sidebarGrid", recurse: true), "bootstrap3")
    

    However, this just returns an empty MvcHtmlString, since the recursive GetProperty method still sees the empty JSON in the grid object as having a value.

    My question is: is there an existing workaround for this that I'm somehow missing?

  • Matthew Kirschner 323 posts 611 karma points
    Feb 28, 2017 @ 17:08
    Matthew Kirschner
    0

    My quick and dirty workaround for this is to create an Html extension method that scrubs the grid property's JSON, checking to see if there is any section or row content.

    If there isn't a built-in method to accomplish the same thing, can anyone help make this better? My code is making a lot of assumptions about the grid's JSON object that I don't feel comfortable with.

    public static MvcHtmlString GetRecursiveGrid(this HtmlHelper html, IPublishedContent content, string propertyAlias, string framework)
    {
        while (content != null)
        {
            var serializer = new JavaScriptSerializer();
            dynamic grid = serializer.Deserialize<object>(content.GetPropertyValue<string>("sidebar"));
    
            if (grid == null || grid["sections"] == null)
            {
                content = content.Parent;
    
                continue;
            }
            var sections = grid["sections"][0];
            var rows = sections["rows"];
    
            if (rows == null || 
                rows.Length == 0 || 
                rows[0]["areas"][0]["controls"] == null || 
                rows[0]["areas"][0]["controls"].Length == 0)
            {
                content = content.Parent;
    
                continue;
            }
    
            return html.GetGridHtml(content.GetProperty("sidebar"), "basicstrata");
        }
    
        return null;
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft