Copied to clipboard

Flag this post as spam?

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


  • Anders Brohus 194 posts 475 karma points
    Dec 01, 2015 @ 09:17
    Anders Brohus
    0

    Check if Umbraco Grid is empty

    Hi! :-)

    Is there a way to check if the Umbraco Grid is empty?

    Because when i get the "PropertyValue", it's not empty or null.

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Dec 16, 2015 @ 10:33
    Dirk De Grave
    1

    Have you tried Model.Content.HasValue("gridAlias")?

  • Aaron Morf 15 posts 124 karma points
    Dec 16, 2015 @ 11:01
    Aaron Morf
    2

    Hi,

    First you Need this: https://github.com/skybrud/Skybrud.Umbraco.GridData

    than you can get the GridDataModel like this:

    /*var json = entity.GetValue<string>("grid");
    GridDataModel grid = GridDataModel.Deserialize(json);*/ 
    GridDataModel grid = Model.Content.GetPropertyValue<GridDataModel>("bodyGrid");// thanks to Streety
    

    With this function you get a boolean if the grid has data or not

    using Skybrud.Umbraco.GridData;
    using System.Linq;
    
    namespace Web.Helpers
    {
        public static class GridHelper
        {
            public static bool HasData(this GridDataModel grid)
            {
                bool hasData = false;
                if (grid == null)
                {
                    hasData = false;
                }
                else
                {
                    hasData = (from section in grid.Sections
                                where section.HasRows
                                from row in section.Rows
                                where row.HasAreas
                                from area in row.Areas
                                let hasGridData = area.HasControls
                                select hasGridData)
                                .Any(hasGridData => hasGridData == true);
                }
                return hasData;
            }
        }
    }
    

    Finally you can check for the grid if it is empty

    if (grid.HasData() == false)
    {
        //grid is empty
    }
    

    I hope this helps you :)

  • Guido Adam 21 posts 65 karma points
    Dec 16, 2015 @ 11:32
    Guido Adam
    0

    It has some basic JSON markup, so it's never empty. It never said the following ;) you could check for stringlength of the gridvalue, beneath 120 is empty.

  • Streety 358 posts 568 karma points
    Apr 29, 2016 @ 14:26
    Streety
    2

    OK so i got this to work but the above suggestion doesn't and its rather confused.

    So the first bit, get the

    https://github.com/skybrud/Skybrud.Umbraco.GridData

    to get access to the gridmodel

    Put a using statement to access the Model & your Gridhelper method

    @using Skybrud.Umbraco.GridData et al

    next paste in this block

    @{GridDataModel json = Model.Content.GetPropertyValue<GridDataModel>("bodyGrid");
    
    if (json.HasData())
    {
              Do stuff if the grid has content
    }
    

    }

    You actually don't want to deserialise at all.

    Works for me.

    Thanks for the Gridhelper Aaron Morf

  • Aaron Morf 15 posts 124 karma points
    Apr 29, 2016 @ 14:39
    Aaron Morf
    0

    Actually I don't know why I deserialized it first.

    But thank you Streety for your hint!

  • Anders Brohäll 295 posts 561 karma points c-trib
    Sep 16, 2016 @ 11:25
    Anders Brohäll
    0

    GridDataModel doesn't seem to have a HasData()-method in the 1.5.1 release. How should it be checked? I really don't want to count characters :)

  • Aaron Morf 15 posts 124 karma points
    Sep 16, 2016 @ 11:31
    Aaron Morf
    0

    It is because there is no method like this. You need to create an extension.

    Get the method from my previous post https://our.umbraco.org/forum/umbraco-7/using-umbraco-7/73346-check-if-umbraco-grid-is-empty#comment-236349

    and then see how Streety got the json to check if it hasData here https://our.umbraco.org/forum/umbraco-7/using-umbraco-7/73346-check-if-umbraco-grid-is-empty#comment-245934

  • Anders Brohäll 295 posts 561 karma points c-trib
    Sep 16, 2016 @ 11:34
    Anders Brohäll
    0

    Ehrm. Sorry about that :)

  • Elin 45 posts 166 karma points
    Jul 14, 2017 @ 14:30
    Elin
    1

    Hi,

    Is there any solution to that problem that doesn't involve getting the https://github.com/skybrud/Skybrud.Umbraco.GridData ?

    I have the same issue and don't want to download this package as it can mess things up for me, and specially that I'm still not clear on how to use it

  • Mike Beach 3 posts 51 karma points
    Jul 28, 2017 @ 09:12
    Mike Beach
    108

    I also didn't want to install another package. There is probably a better way but this worked for me.

    var grid = Json.Decode(Model.Content.GetPropertyValue("grid").ToString());
    
    if (Model.Content.HasValue("grid") && grid.sections[0].rows.Length >= 1){
        @Html.GetGridHtml(Model.Content, "grid")
    }
    

    The var grabs the property as JSON and an empty grid with no rows returns:

    { "name": "1 column layout", "sections": [ { "grid": 12, "rows": [] } ] }
    

    So I just simply check that no rows have been generated.

  • Hayden 32 posts 170 karma points
    Aug 20, 2017 @ 23:30
    Hayden
    0

    This worked for me.

    +1 for not having to install an extension for something simple.

  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    Oct 30, 2017 @ 08:54
    Alex Skrypnyk
    1

    Hi

    We have an extension for checking is there a grid value:

    public static bool HasGridValue(this IPublishedContent contentItem, string alias)
        {
            try
            {
                if (contentItem.HasValue(alias))
                {
                    var stringValue = contentItem.GetPropertyValue<string>(alias);
                    if (!string.IsNullOrWhiteSpace(stringValue))
                    {
                        var grid = Json.Decode(stringValue);
    
                        if (grid.sections[0].rows.Length >= 1)
                        {
                            return true;
                        }
                    }
                }
    
            }
            catch (Exception e)
            {
                CustomLogHelper.Error<UmbracoViewPage>(e.Message, e);
            }
    
    
            return false;
        }
    

    Use it like:

    if (contentItem.HasGridValue("body"))
    

    Thanks,

    Alex

  • Damian Chambers 23 posts 87 karma points
    Jan 08, 2018 @ 14:15
    Damian Chambers
    0

    This worked very well for me. Thank you.

  • Sjoerd Stottelaar 31 posts 189 karma points
    Jul 03, 2019 @ 14:44
    Sjoerd Stottelaar
    1

    Thank you for sharing. I've edited it slightly to work with V8:

    public static bool HasGridValue(this IPublishedContent contentItem, string alias)
        {
            try
            {
                if (contentItem.HasValue(alias))
                {
                    var stringValue = contentItem.Value<string>(alias);
                    if (!string.IsNullOrWhiteSpace(stringValue))
                    {
                        var grid = Json.Decode(stringValue);
    
                        if (grid.sections[0].rows.Length >= 1)
                        {
                            return true;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                throw(e);
            }
    
            return false;
        }
    
  • Mike Chambers 636 posts 1253 karma points c-trib
    Aug 01, 2019 @ 11:21
    Mike Chambers
    0

    quick and dirty with ModelsBuilder....

    var dev = Model as DevelopmentPage; var hadGridContent = dev.GridContent["sections"][0]["rows"].Count() >= 1;

  • Sven Geusens 169 posts 881 karma points c-trib
    Sep 16, 2019 @ 11:08
    Sven Geusens
    0

    My 2 cents. Adding empty sections/rows should also count as an empty grid, so i check on any editors being present. You can get the value of the grid as a JToken en just query it that way

    public static bool HasGridValue(this IPublishedContent content, string alias) 
    {
        return content.Value<JToken>(alias)["sections"]?.Any(s => s["rows"]?.Any(r => r["areas"]?.Any(a => a["controls"].Any()) == true) == true) == true;
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft