Copied to clipboard

Flag this post as spam?

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


  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Aug 16, 2009 @ 21:02
    Matt Brailsford
    0

    Sort by property A or property B if property A is empty

    Hi Guys,

    I'm currently sorting a list of reports by createdDate, however I need to be able to override this date so I'm setting up a new property "reportDate". What i need is for the list to use this date if it is set, otherwise resort back to the createdDate.

    Anybody know how to go about this?

    Many thanks

    Matt

  • Tom Maton 387 posts 660 karma points
    Aug 16, 2009 @ 21:19
    Tom Maton
    1

    Hi Matt,

    I am not an XSLT expert so this is just thought out loud, couldn't you set a variable which by default is set to "reportDate" and if this is not set change the variable to "createdDate".

    <xsl:choose>
    <xsl:when test="./data [@alias = "reportedDate"] != ''">
    <xsl:variable name="sortBy" select="reportedDate"/>
    </xsl:when>
    <xsl:otherwise>
    <xsl:variable name="sortBy" select="createdDate"/>
    </xsl:otherwise>
    </xsl:choose>

    then pass this variable into the XSLT sort?

    Tom

  • Tommy Poulsen 514 posts 708 karma points
    Aug 16, 2009 @ 21:29
    Tommy Poulsen
    0

    another approach would be to make an eventhandler/action triggered on updates on these types of documents, and then set the reportDate to createDate in the eventhandler in case it is not already set.

    You could also consider using multiple sort-statemenets in your xslt - depending on how you want the output presented.

    >Tommy

     

  • Chris Koiak 700 posts 2626 karma points
    Aug 16, 2009 @ 21:31
    Chris Koiak
    0

    I'd probably take the approach of making the reportDate mandatory or defaulting the field to the createDate if it's left blank.

    Although Umbraco doesn't support default values for properties, you could easily implement it with an event handler or make a custom data type.

    Not an xslt answer I know, I don't know if what you're trying to do is possible in xslt.

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Aug 16, 2009 @ 21:34
    Matt Brailsford
    0

    Cheers guys,

    I ended up solving it another way. 

    I created a function inside the XSLT as follows:

    <msxsl:script language="C#" implements-prefix="scripts">
        <![CDATA[
    public string isNull(string s1, string s2)
        {
            return !String.IsNullOrEmpty(s1)? s1 : s2;
        }
    ]]>
    </msxsl:script> 

    Then is used it within the xslt sort attribute as follows:

    <xsl:sort select="scripts:isNull(data [@alias = 'ReportDate'], @createDate)" order="descending"/>

    Many thanks

    Matt

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Aug 16, 2009 @ 21:52
    Dirk De Grave
    0

    Matt,

    Don't think that'll solve all of your issues, as you'll be sorting the list on either reportDate or createDate, and you don't know upfront whether each of the items in the list will have a value for that property you'll be sorting on.

    I agree with tommy and chris, create some event handler that on save of document check whether reportDate is filled in, and if not, copy value of createDate.

    Sorting will become a breeze as you don't need to check anymore whether values are existing.

     

    Cheers,

    /Dirk

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Aug 16, 2009 @ 22:00
    Matt Brailsford
    0

    Orindarily I would go the event handler route. The reason I haven't, is that the site is already full of reports, and this feature is being added on.

    You are right that I won't know up front whether an item has a ReportDate set, but that is the purpose of the mehtod, if one exists use that to sort with, if not, resort to the createdDate. Or am I missing something?

    Many thanks

    Matt

  • Chris Koiak 700 posts 2626 karma points
    Aug 16, 2009 @ 22:03
    Chris Koiak
    0

    I didn't know you could implement a sort like that. Very Interesting.

    if you go down the event handler route you could republish all the pages and the handler would update the field on all the historic pages.

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Aug 16, 2009 @ 22:12
    Matt Brailsford
    0

    ahhh, cool. I might have a go at that then, as it would obvously be better if i didn't have to do extra processing.

    Cheers guys.

    Matt

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Aug 17, 2009 @ 07:51
    Dirk De Grave
    0

    Hm, didn't know that either, so you're saying that the sort will take either value of createDate or ReportDate, depending on whether value exists (and it does that for each item from the list)? Interesting, need to refresh my xslt knowledge on that specific method. 

     

    Cheers,

    /Dirk

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Aug 17, 2009 @ 14:08
    Matt Brailsford
    0

    Yup, that's correct. Well, thats how it worked for me at least =)

    Matt

Please Sign in or register to post replies

Write your reply to:

Draft