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?
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).
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']">
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
You can use xsl:sort direct after the xsl:for-each. eg:
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
Hi Frank,
you can sort your listing by adding a sort tag inside your for-each loop.
and if you want to sort by two values you can simply add another sort tag
to sort in descending order you can write
hope this helps.
Tim
Do you have a line like the following in your xslt?
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).
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
If you are passing a sortorder parameter from your template/macro to the xslt, you can use it like
then you can use it like {$sortOrder} in your sort statement. You should check that you are passing something in your template, like
"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
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
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.
Thanks everyone!
is working on a reply...