Copied to clipboard

Flag this post as spam?

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


  • Simone Chiaretta 134 posts 541 karma points c-trib
    Feb 17, 2016 @ 11:07
    Simone Chiaretta
    0

    How to see if a property of type Grid has content?

    If I do call the HasValue method on a property whose type is a grid I always get true even if there is no actual content, but just the JSON with the definition of the empty grid.

    Is there a way to detect whether a grid has content?

    Simone

  • Elin 45 posts 166 karma points
    Jul 14, 2017 @ 14:37
    Elin
    0

    Hi Simone,

    Have you found any solution to that issue.

    I'm currently having the same issue, and it's driving me nuts.

  • Nicholas Westby 2054 posts 7103 karma points c-trib
    Jul 14, 2017 @ 15:26
    Nicholas Westby
    0

    You could always fetch the value and compare it against the known structure of an empty grid.

  • Anders Bjerner 487 posts 2995 karma points MVP 8x admin c-trib
    Jul 15, 2017 @ 11:42
    Anders Bjerner
    2

    Hi Simone,

    Assuming the alias of your grid property is content, your code could look like this:

    @using Newtonsoft.Json.Linq
    @inherits UmbracoTemplatePage
    @{
    
        Layout = "Master.cshtml";
    
        if (CurrentPage.content is JObject) {
    
            JObject grid = CurrentPage.content;
    
            if (grid.SelectTokens("sections[*].rows[*].areas[*].controls").Any()) {
                @CurrentPage.GetGridHtml("content", "fanoe")
            }
    
        }
    
    }
    

    Umbraco will expose the grid property value as a instance of JObject, which again has a SelectTokens method for matching one or more JSON tokens/objects - in this case to match the controls.

Please Sign in or register to post replies

Write your reply to:

Draft