Copied to clipboard

Flag this post as spam?

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


  • Eric McDonald 16 posts 137 karma points
    Sep 24, 2017 @ 22:35
    Eric McDonald
    0

    Display Totals by Week and Year From Child Page Property

    Hi Guys,

    Been asking for some help in regards to this for the past few weeks on a different thread but getting no response. I am trying to get data from child pages and display them in a table. My table has a Week Number and a Total Colum and also the year.

    Below is an example of what I currently return but I want to display just one record per week with the totalcatch value for that week and in the correct year colum.

    enter image description here

    Here is my current code, can anyone give me an idea of how to acheive this?

    @using umbraco.MacroEngines
    

    @using System.Globalization; @inherits umbraco.MacroEngines.DynamicNodeContext @inherits Umbraco.Web.Macros.PartialViewMacroPage

    @{ var latestCatches = Umbraco.Content(1157); //var currentYear = DateTime.Now.Year; var culture = CultureInfo.GetCultureInfo("en-GB"); var dateTimeInfo = DateTimeFormatInfo.GetInstance(culture); var dateTime = DateTime.Today; int currentWeekNumber = culture.Calendar.GetWeekOfYear(dateTime, dateTimeInfo.CalendarWeekRule, dateTimeInfo.FirstDayOfWeek); int weekNumber = 0; int WeeklyCatchTotal = 0; //DateTime startOfWeek = DateTime.Today; //int delta = DayOfWeek.Monday - startOfWeek.DayOfWeek; //startOfWeek = startOfWeek.AddDays(delta); //DateTime endOfWeek = startOfWeek.AddDays(5);

    foreach (var latestCatch in latestCatches.Children.Where("Visible").OrderBy("CreateDate desc"))
    {
    
        var catchDateTime = latestCatch.catchDate;
        weekNumber = culture.Calendar.GetWeekOfYear(catchDateTime, dateTimeInfo.CalendarWeekRule, dateTimeInfo.FirstDayOfWeek);
    
        int total = latestCatch.GetPropertyValue<int>("totalFish");
        WeeklyCatchTotal = WeeklyCatchTotal + total;
        <tr><td>Week @weekNumber</td><td>@WeeklyCatchTotal</td></tr>
    }
    

    }

  • Nik 1614 posts 7260 karma points MVP 7x c-trib
    Sep 25, 2017 @ 10:57
    Nik
    0

    Hi Eric,

    I think quite a bit of restructuring might be needed to gather this information.

    How many years of information are you looking to show? Are you looking to show all 52 weeks for the year as well?

    Basically, I would look at creating an action on a surface controller. In this action I would be doing something like this:

    for(i = 1; i < 52; i++) //for week 1 => 52 of the year
    {
        //get all latest catches where the catch date matches the week
        [... code for filtering goes here...]
        for(year=startYear; year<=endYear; year++)
        {
               //for each year grab catches for the current week and sum their totals.
        }
    }
    

    I would also create a class for storing all of the results before passing it through to a partial view for rendering out the table.

    Does that help at all?

    Nik

  • Eric McDonald 16 posts 137 karma points
    Oct 08, 2017 @ 20:39
    Eric McDonald
    100

    Hi Nik,

    Thanks for your input. I eventually went down the route of using a dictionary and key value pairs to acheive what I wanted to acheive. I have included my code below in case anyone else has a similar issue they are trying to resolve.

    @using umbraco.MacroEngines @using System.Globalization; @inherits umbraco.MacroEngines.DynamicNodeContext @inherits Umbraco.Web.Macros.PartialViewMacroPage

    @{ var latestCatches = Umbraco.Content(1157); //var currentYear = DateTime.Now.Year; var culture = CultureInfo.GetCultureInfo("en-GB"); var dateTimeInfo = DateTimeFormatInfo.GetInstance(culture); var dateTime = DateTime.Today; int weekNumber = 0; var avg = 0; var avgTenTotal = 0; var avgFiveTotal = 0; var yearCountFive = 5; var yearCountTen = 10; Dictionary

    foreach (var latestCatch in latestCatches.Children.Where("Visible").OrderBy("CatchDate desc"))
    {
    
        var catchDateTime = latestCatch.catchDate;
        var catchDateYear = latestCatch.catchDate.ToString("yyyy");
        catchDateYear = Convert.ToInt32(catchDateYear);
        if (!yearArray.ContainsKey(catchDateYear))
        {
            weekArray = new Dictionary<int, int>();
            yearArray.Add(catchDateYear, weekArray);
        }
        weekNumber = culture.Calendar.GetWeekOfYear(catchDateTime, dateTimeInfo.CalendarWeekRule, dateTimeInfo.FirstDayOfWeek);
        int total = latestCatch.GetPropertyValue<int>("totalFish");
    
        if (weekArray.ContainsKey(weekNumber))
        {
            weekArray[weekNumber] = weekArray[weekNumber] + total;
        }
        else
        {
            weekArray.Add(weekNumber, total);
        }
    }
    <table class="table">
        <thead class="data-grid">
            <tr>
                <th>&nbsp;</th>
                @for (int x = yearArray.Count - 1; x >= 0; x--)
                {
                    <th>
                        @yearArray.ElementAtOrDefault(x).Key
                    </th>
                }
                <th>10 yr Avg</th>
                <th>5 yr Avg</th>
            </tr>
    
        </thead>
        <tbody>
            @for (int i = 1; i <= 40; i++, avg = 0)
            {
                <tr>
                    <td>Week @i</td>
                    @for (int x = yearArray.Count - 1; x >= 0; x--)
                    {
                        <td>
                            @if (yearArray.ElementAtOrDefault(x).Value.ContainsKey(i))
                            {
                                @yearArray.ElementAtOrDefault(x).Value[i];
                            }
    
                            &nbsp;
    
                        </td>
    
                    }
                    <td>
    
                        @for (int x = yearCountTen - 1; x >= 0; x--)
                        {
    
                            if (yearArray.ElementAtOrDefault(x).Value.ContainsKey(i))
                            {
                                avg += yearArray.ElementAtOrDefault(x).Value[i];
                            }
                        }
                        @{ avg = avg / yearCountTen;
                        }
    
                        @avg
                    </td>
                    <td>
    
                        @for (int x = yearCountFive - 1; x >= 0; x--)
                        {
    
                            if (yearArray.ElementAtOrDefault(x).Value.ContainsKey(i))
                            {
                                avg += yearArray.ElementAtOrDefault(x).Value[i];
                            }
    
                        }
                        @{ avg = avg / yearCountFive; }
    
                        @avg
                    </td>
                </tr>
            }
            <tr>
                <td>Totals</td>
                @for (int x = yearArray.Count - 1; x >= 0; x--)
                {
                    var yrTotal = @yearArray.ElementAtOrDefault(x).Value.Sum(v => v.Value);
                        <td class="totals-text">
                         @yrTotal
                        </td>
                }
    
                <td class="avg-text">
    
                    @for (int x = yearCountTen - 1; x >= 0; x--)
                    {
    
                        avgTenTotal += @yearArray.ElementAtOrDefault(x).Value.Sum(v => v.Value);
    
                    }
                    @{ avgTenTotal = avgTenTotal / yearCountTen; }
    
                    @avgTenTotal
                </td>
    
                <td class="avg-text">
    
                    @for (int x = yearCountFive - 1; x >= 0; x--)
                    {
    
                        avgFiveTotal += @yearArray.ElementAtOrDefault(x).Value.Sum(v => v.Value);
    
                    }
                    @{ avgFiveTotal = avgFiveTotal / yearCountFive; }
    
                    @avgFiveTotal
                </td>
    
            </tr>
        </tbody>
    </table>
    

    }

    Web Design Cumbernauld

Please Sign in or register to post replies

Write your reply to:

Draft