Copied to clipboard

Flag this post as spam?

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


  • rfiegle 5 posts 25 karma points
    Nov 08, 2012 @ 16:38
    rfiegle
    0

    OrderBy on numeric property not working

    Hi - I am new to Umbraco.  I am trying to sort pages in a nav macro based on a custom numeric property in their doc type.  I don't get an error, but the pages aren't sorted.  Here is the entire macro:

    <nav> 
    <ul id="topNavigation">
    @{ var homeNode = Model.AncestorOrSelf("HomePage"); }
    
            <li><a href="@homeNode.Url" class="@Library.If(homeNode.Id == Model.Id, "current", "")">@homeNode.pageTitle</a></li>
    
            @foreach (var page in homeNode.Children.Where("Visible").OrderBy("sortOrder"))
            {
                if (page.HasProperty("pageTitle"))
                     {
                <li>
                   <a href="@page.Url" class="@Library.If(page.Id == Model.Id, "current", "")">@page.pageTitle</a>
    
                            <!-- If the page has child nodes (2nd level) that are visible -->
                            @if (page.Children.Where("Visible").Count() > 0)
                            {
                           <ul class="childNavList">
                                   @foreach (var childPage in page.Children.Where("Visible").OrderBy("sortOrder"))
                                            {
                                           <li><a href="@childPage.Url" class="@Library.If(childPage.Id == Model.Id, "current", "")">@childPage.pageTitle</a></li>
                                            }
                                    </ul>
                            }
                            else if (page.Name == "Map Page") {
                                <ul class="childNavList">
                                    <li><a href="/map-page.aspx?q=driver">Driver</a></li>
                                    <li><a href="/map-page.aspx?q=staff">Staff</a></li>
                                    <li><a href="/map-page.aspx?q=warehouse">Warehouse</a></li>
                                </ul>
    
                            }
                    </li>
            }
            }
    </ul>
    </nav>

    It looks like examples I have found for using the OrderBy method, so I'm not sure what the problem is.  Any help you can offer is appreciated.

  • rfiegle 5 posts 25 karma points
    Nov 08, 2012 @ 17:38
    rfiegle
    0

     

    I found that if I sort by

    OrderBy("sortOrder desc") 

    it will sort by the id / created date descending rather than my sortOrder property. Is this a bug?

     

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Nov 08, 2012 @ 17:45
    Jan Skovgaard
    1

    Hi rfiegle - welcome to our :)

    Cool that you managed to figure out the issue by yourself.

    In short, no it's not a bug - unless you specifically add "desc" for descending the default sortorder will be "asc" for ascending.

    You can read a bit more about OrderBy here http://umbraco.com/follow-us/blog-archive/2011/3/1/umbraco-razor-feature-walkthrough-%E2%80%93-part-4.aspx and then I'll recommend that you download the Razor cheat sheet.

    I hope you have a fun time learning Umbraco - please don't hesitate to ask any kind of questions if there is something you're in doubt about.

    /Jan

  • rfiegle 5 posts 25 karma points
    Nov 08, 2012 @ 18:03
    rfiegle
    0

    Thanks for the response Jan, but all I figured out is that it is actually sorting by the when the page was created (or Id) rather than my custom sortOrder property.  I thought maybe "sortOrder" was already a property in Umbraco, so I tried changing it to navViewOrder, but I am still experiencing the same problem.

     

    Edit: Actually, renaming the property did fix it - there was just a database problem the first time I tried it.

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Nov 08, 2012 @ 19:00
    Jan Skovgaard
    0

    Coming to think of it...why do you sort the documents using af custom property?

    When you fetch the nodes it displays in the sort order that it has in the backoffice. So if you change the sort order by right clicking a parent node and then drag and drop nodes around so it has a new sort order this will be displayed on the website. Having a custom sort order property seems double to me :)

    /Jan

  • rfiegle 5 posts 25 karma points
    Nov 08, 2012 @ 19:29
    rfiegle
    0

    When we tried dragging the nodes around, it did not update their order in our nav.  It looks like Umbraco sorts by Id by default, rather than sortOrder.  I did not know of the existence of sortOrder until I ran into this problem by also naming my custom property sortOrder (after my last post I found that there is, in fact, a default property saved in the nodes called sortOrder in the umbraco.config file).  So, yes, you are correct - having another property for sort recreates existing functionality, but we also found it very difficult to drag and drop the nodes, so being able to just fill in the sort order manually will help us in that regard.

     

    Edit: To clarify, we were dragging and dropping the actual nodes - I was not aware of the sort menu.  Thank you for informing me of that.

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Nov 08, 2012 @ 19:34
    Jan Skovgaard
    0

    After you did the sorting did you then right click the "Content" node (the default node) and choose "Republish entire site"? This should make the changes appear on the website.

    Hope this helps.

    /Jan

  • rfiegle 5 posts 25 karma points
    Nov 08, 2012 @ 19:46
    rfiegle
    0

    It looks like dragging and dropping the actual nodes does not change the sort - only using the sort menu will save the sort of the nodes.  The sort menu is much easier to use than moving the nodes, so it works out great.  It looks like I don't need my custom property. Thanks, you have been very helpful.

Please Sign in or register to post replies

Write your reply to:

Draft