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;
}
}
}
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.
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
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.
Have you tried Model.Content.HasValue("gridAlias")?
Hi,
First you Need this: https://github.com/skybrud/Skybrud.Umbraco.GridData
than you can get the GridDataModel like this:
With this function you get a boolean if the grid has data or not
Finally you can check for the grid if it is empty
I hope this helps you :)
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.
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
}
You actually don't want to deserialise at all.
Works for me.
Thanks for the Gridhelper Aaron Morf
Actually I don't know why I deserialized it first.
But thank you Streety for your hint!
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 :)
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
Ehrm. Sorry about that :)
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
I also didn't want to install another package. There is probably a better way but this worked for me.
The var grabs the property as JSON and an empty grid with no rows returns:
So I just simply check that no rows have been generated.
This worked for me.
+1 for not having to install an extension for something simple.
Hi
We have an extension for checking is there a grid value:
Use it like:
Thanks,
Alex
This worked very well for me. Thank you.
Thank you for sharing. I've edited it slightly to work with V8:
quick and dirty with ModelsBuilder....
var dev = Model as DevelopmentPage; var hadGridContent = dev.GridContent["sections"][0]["rows"].Count() >= 1;
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
is working on a reply...