Copied to clipboard

Flag this post as spam?

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


  • Frank Caico 7 posts 37 karma points
    Aug 06, 2009 @ 16:47
    Frank Caico
    0

    Node ordering

    I'm very new to Umbraco so please bear with me if this is a dumb question.

    After playing with Runway a bit, I've been using Creative Website Starter on a new Umbraco installation.

    Anyway,  I've got a "News and Events" page which is a node under my Home content node. Under this node are a bunch of nodes -- either News Items or Event Items.   The News and Events page displays all the news and event items (sort of like a blog).   Anyhow, the Settings for the News and Events node has to sorting fields:   Sort By and Sort Order.   These sound great, but unfortunately no matter what I do, the news and event items always show up in the order in which the nodes are "physically" ordered under the News and Events node.

    I have a theory as to why this is happening.  The News and Events page template uses an XSLT macro to display the nodes under it in a nice pretty and abbreviated way.   Of course this XSLT macro uses a <xsl:for-each select="$currentPage/node"> to iterate through the nodes displaying each one using the appropriate markup.    My theory is that this for-each is the culprite as it is ignoring any sort preferences of the News and Events node.   Is this right?!?!    And if it is, how do I correct this behavior so that the Ascending/Descending sort order will be obeyed?

    Thanks for your help!

    Frank

  • Thomas Höhler 1237 posts 1709 karma points MVP
    Aug 06, 2009 @ 16:50
    Thomas Höhler
    0

    You can use xsl:sort direct after the xsl:for-each. eg:

    <xsl:sort select ="@creatDate" order="descending" />

    How it is done in the CWS2 I have to look into it first, but perhaps this will take you to the right direction.

    Thomas

  • Timsn 121 posts 231 karma points
    Aug 06, 2009 @ 16:53
    Timsn
    0

    Hi Frank,

    you can sort your listing by adding a sort tag inside your for-each loop.

       <xsl:for-each select="$currentPage/node">
    <xsl:sort select="@name"/>
    <xsl:value-of select="@name"/><br/>
    </xsl:for-each>

    and if you want to sort by two values you can simply add another sort tag

       <xsl:for-each select="$currentPage/node">
    <xsl:sort select="@name"/>
    <xsl:sort select="@createdate"/>
    <xsl:value-of select="@name"/><br/>
    </xsl:for-each>

    to sort in descending order you can write

    <xsl:sort select="@name" order="descending"/>

    hope this helps.

    Tim

  • dandrayne 1138 posts 2262 karma points
    Aug 06, 2009 @ 16:54
    dandrayne
    0

    Do you have a line like the following in your xslt?

    <xsl:sort select="@*[name() = $SortBy]" order="{$SortOrder}" data-type="{$DataType}"/>

    it should probably be just after the for-each that lists your news/events nodes.  The above code won't work directly but should point you in the right direction.  more at http://www.w3schools.com/xsl/el_sort.asp.

    It may be that the macro parameters are not being sent to the xslt correctly (from the template in which the macro is embedded).

     

  • Frank Caico 7 posts 37 karma points
    Aug 06, 2009 @ 17:25
    Frank Caico
    0

    Thanks guys this is definitely getting me to the right place.

    One last question:

    My News and Events List Doc type has 2 generic properties:   Sort By (sortBy) and Sort Order (sortOrder).  sortBy

    sortBy has the following values (sortOrder, createDate and updateDate)  Sortorder has the values (ascending, descending).

     

    I can get my xslt to sort the way I want like so:

    <xsl:sort select="@updateDate" order="descending"/> 

     

    This is good, but if I want the sort to obey the Sort By and Sort order properties in the doc type, how do I specify that in xslt?

     

    I went through the XSLT tutorial, but I think the problem is I dont know what xml is being passed to my transfor, so Im not sure what to write... 

    @dandrayne - I tried your statement above and that didnt work -- what does the {$...}  do? 

     

    Thanks again for your help

     

  • dandrayne 1138 posts 2262 karma points
    Aug 06, 2009 @ 17:54
    dandrayne
    100

    If you are passing a sortorder parameter from your template/macro to the xslt, you can use it like

    <xsl:variable name="sortOrder" select="/macro/sortOrder" />

    then you can use it like {$sortOrder} in your sort statement.  You should check that you are passing something in your template, like

    <umbraco:Macro Alias="SomeMacro" sortOrder="yourSortOrder" runat="server"></umbraco:Macro>

     

     

     

  • Chris Koiak 700 posts 2626 karma points
    Aug 06, 2009 @ 19:22
    Chris Koiak
    0

    "but I think the problem is I dont know what xml is being passed to my transfor"

    You can view the raw xml document at /data/umbraco.config

    Chris

  • Thomas Höhler 1237 posts 1709 karma points MVP
    Aug 06, 2009 @ 19:46
    Thomas Höhler
    0

    Use xsl:copy-of to get the raw xml (it will probably not display in the browser, but in the html-code it is there)

    Thomas

  • Frank Caico 7 posts 37 karma points
    Aug 06, 2009 @ 21:03
    Frank Caico
    0

    Okay, I've got everything working now.  Here is what I needed to do:

    in my XSLT transform, I had to do the following:

    1.   Get the SortOrder and SortBy variables from the current page data:

        <!-- Get Properties of how to sort the blog items-->
        <xsl:variable name="SortOrder" select="$currentPage/data [@alias = 'sortOrder']" />
        <xsl:variable name="SortBy" select="$currentPage/data [@alias = 'sortBy']" />

    2.  I had to make sure that the SortBy variable had the correct data type

        <!--
        =============================================================
        Lets setup a variable called DataType. If the user has
        chosen 'sortOrder' from the sortBy dropdown list, then we
        need to set the datatype variable to = number otherwise it
        will sort the data wrongly.   
        =============================================================
        -->
        <xsl:variable name="DataType">
            <xsl:choose>
                <xsl:when test="$SortBy='sortOrder'">
                    <xsl:value-of select="'number'" />
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="'text'" />
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

    3.  In my xsl:for-each  statement, I needed to make sure the selected subnodes were only nodes that had the SortBy and SortOrder parameters (This isn't technically necessary because the News and Events document doesnt allow anything but News items and Event items and both those items have these, but the page testing will fail if I dont do this.

              <xsl:for-each select="$currentPage/node [@nodeTypeAlias = 'CWS_EventItem' or @nodeTypeAlias = 'CWS_NewsItem']">

     

    4. Now I can do the sort correctly as:

    <xsl:sort select="@*[name() = $SortBy]" order="{$SortOrder}" data-type="{$DataType}"/>

    This sorts the nodes using the attribute specified in SortBy and using the ordering specified by SortOrder. 

  • Frank Caico 7 posts 37 karma points
    Aug 06, 2009 @ 21:03
    Frank Caico
    0

    Thanks everyone!

Please Sign in or register to post replies

Write your reply to:

Draft