Yes, that is correct. It's the "sum" part I can't figure out :) I need the sum of the properties called "antalVisnigner"...
Here is my script:
@inherits umbraco.MacroEngines.DynamicNodeContext
@* Ensure that the Current Page has children, where the property umbracoNaviHide is not True *@
@if (Model.Children.Where("Visible").Any())
{
<tbody>
@* For each child page under the root node, where the property umbracoNaviHide is not True *@
@foreach (var childPage in Model.Children.Where("Visible"))
{
<tr>
<td>@childPage.Name</td>
<td>
@* Format numeric value with punctuation *@
@if (childPage.HasValue("antalVisnigner")){
int price = childPage.antalVisnigner;
string formattedPrice = price.ToString("#,##0");
@formattedPrice
}
</td>
<td>@childPage.antalKlik</td>
<td>@childPage.cTR%</td>
<td>@childPage.avgCPM</td>
<td>@childPage.cost</td>
</tr>
}
</tbody>
<tfoot>
<tr>
<th>Total</th>
<th>Sum</th> @* <----- This is what I can't figure out *@
<th> </th>
<th> </th>
<th> </th>
<th> </th>
</tr>
</tfoot>
}
Rick's option is one possible route, however it does mean querying your fields multiple times. This shouldn't be a problem but this is an alternative:
@inherits umbraco.MacroEngines.DynamicNodeContext
@* Ensure that the Current Page has children, where the property umbracoNaviHide is not True *@
@if (Model.Children.Where("Visible").Any())
{
int sum = 0;
<tbody>
@* For each child page under the root node, where the property umbracoNaviHide is not True *@
@foreach (var childPage in Model.Children.Where("Visible"))
{
<tr>
<td>@childPage.Name</td>
<td>
@* Format numeric value with punctuation *@
@if (childPage.HasValue("antalVisnigner")){
int price = childPage.antalVisnigner;
string formattedPrice = price.ToString("#,##0");
sum =+ price;
@formattedPrice
}
</td>
<td>@childPage.antalKlik</td>
<td>@childPage.cTR%</td>
<td>@childPage.avgCPM</td>
<td>@childPage.cost</td>
</tr>
}
</tbody>
<tfoot>
<tr>
<th>Total</th>
<th>Sum: @sum </th> @* <----- This is what I can't figure out *@
<th> </th>
<th> </th>
<th> </th>
<th> </th>
</tr>
</tfoot>
}
No problem, Dan's answer below is another good option as well.
It's a good idea to mark the answer in a thread so others know there is a valid answer there if they are having the same issue.
How to get sum of property values from children?
Hi people
I need to get the sum of a property value from all children.
Can anyone tell me how this is done with Razor?
I would think it is pretty simple task, but I am relatively new to razor scripting :)
Hi Inmedia,
So you should loop though some children, and then show the data on the page above those children.
Then something like the razor blow should work for you.
What you need to do is to call custom properties is:
Inside the foreach loop
Hope this helps, and let me know if I have misunderstood your question.
/Dennis
Hi Dennis
I think I wasn't precise enough in my post.... What I need is for razor to calculate the sum of a numeric property that all children has.
I have a table that list all the children and their values. Each child gets a row... This I have already set up using the method you posted.
But in the bottom of this table, I need a sum function that calculates the total of all the child values.
Hope this makes sense? :)
I have made an image to visualise what I need :)
Hi Inmeida,
How far have you got? Have you managed to render out the table without the sum?
If so, can you share your razor code so we can get a feel for what you are doing?
Thanks,
Nik
Hi Nik
Yes, that is correct. It's the "sum" part I can't figure out :) I need the sum of the properties called "antalVisnigner"...
Here is my script:
Hee Inmedia,
I think you can try something like this:
Cheers,
Hi Rick
I get the following error, when I try that:
Hi Inmedia,
Rick's option is one possible route, however it does mean querying your fields multiple times. This shouldn't be a problem but this is an alternative:
Hi Nik
But that just sets the sum to be 0.... Right?
That's what I get when I use your script. It doesn't do any math for me, as far as I can see.
Do I need to add something else to the script, to make it work?
I think I got my sum =+ price back to front, it might need to be sum += price, or even sum = sum + price.
Eureka!!
Yes, that was it :)
Big high five to you! :)
No problem, Dan's answer below is another good option as well. It's a good idea to mark the answer in a thread so others know there is a valid answer there if they are having the same issue.
is working on a reply...