Copied to clipboard

Flag this post as spam?

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


  • Garrett Fisher 341 posts 496 karma points
    May 20, 2010 @ 23:38
    Garrett Fisher
    0

    Populating a Dropdown List With All Prevalues of a Data Type

    Hi,

    The subject says it all-- how do I populate a dropdown list (on the Client, in an XSLT script referenced by a Macro) with the prevalues of a data type?  Strangely, I've never had to do this before, but I do now.  I've invented a data type called "Industry," which is a property of a Customer object I also created.  This data type has several prevalues such as Technology, Consumer, Financial, etc.  I need a select box on a page which is to be used as a Filter for the Customer list, and it therefore needs to show all of these options dynamically.  How to do?

    Thanks in advance,

    Garrett

  • Stefan Kip 1614 posts 4131 karma points c-trib
    May 21, 2010 @ 00:00
    Stefan Kip
    0

    One simple Google search gives you the answer:

    http://our.umbraco.org/forum/developers/api-questions/5442-Custom-DDL-DataType-prevalues---text-and-value#comment19835

    I don't think it's possible with XSLT though, but .NET is great(er)! ;-)

  • Nik Wahlberg 639 posts 1237 karma points MVP
    May 21, 2010 @ 03:16
    Nik Wahlberg
    0

    Here's a quick (and dirty) example of how to get prevalues loaded in a control. 

                    string xmlStr = "";
                    XPathNodeIterator xpathIterator = umbraco.library.GetPreValues([id of DT]);
                    foreach (XPathNavigator n in xpathIterator)
                    {
                        xmlStr = n.OuterXml;
                    }
                    var x = new XmlDocument();
                    x.LoadXml(xmlStr);
                    XmlNodeList nl = x.SelectNodes("//preValues/preValue");
                    try {
                        foreach (XmlNode xn in nl)
                        {
                            var li = new ListItem(xn.InnerText, xn.Attributes["id"].Value);
                            YourControlId.Items.Add(li);
                        }
                    } catch (Exception ex){}

    So, just replace the [id of DT] with the ID of the data type that you created (or are targeting), and the 'YourControlId' with the control in your Usercontrol. 

    NOTE: it's not ideal to hard code ID's like this, but you could easily put that into a config file so that it is not compiled. 

    That should do it!

    -- Nik

  • Jonas Eriksson 930 posts 1825 karma points
    May 21, 2010 @ 08:26
    Jonas Eriksson
    0

    In Xslt it would be something like this:

    <xsl:for-each select="umbraco.library:GetPreValues($idOfDataType)//preValue">
        <xsl:value-of select="." /><br/>
    </xsl:for-each>
    
    Regards
    Jonas
  • Jonas Eriksson 930 posts 1825 karma points
    May 21, 2010 @ 08:32
  • Garrett Fisher 341 posts 496 karma points
    May 25, 2010 @ 15:31
    Garrett Fisher
    0

    I continue to be amazed at how great this forum is.

    THANKS TO ALL of you for ALL being right.  Jonas I had your code working in less than a minute.

    One more XSLT question though.  When you're doing a search form using contains(), how can you print out a message ("Sorry, your search turned up no results.") since there is no such thing as an <xsl:else>?  In other words, in order to DO the search, I need to be in a <xsl:for-each> loop, so if I use when (contains())/otherwise then the message prints out for every iteration of the loop.  And I know that <xsl:variable> can't be reset from an initial value.  What's the best way to know at the end of this loop that contains() was never true? That nothing was found?

    Cheers,

    Garrett

  • dandrayne 1138 posts 2262 karma points
    May 25, 2010 @ 16:21
    dandrayne
    0

    Hi Garrett

    I've used something like the following in the past - basically its

    • Build up a variable with xml nodes of matched results
    • use msxml:node-set to make it traversable in xslt
    • Count results

    Like the following stripped-down example

    <xsl:variable name="searchResults">
    <xsl:for-each select="$currentPage/ancestor-or-self::node[@level=2]/data[@alias='SOMEALIAS']/descendant::node">

    <xsl:if test="contains(current()/data[@alias='testThis'], 'SEARCH TERM'">
    <xsl:copy-of select="current()" />
    </xsl:if>

    </xsl:for-each>
    </xsl:variable>



    <xsl:choose>
    <xsl:when test="count(msxml:node-set($searchResults)/node) = 0">
    <xsl:text>No results</xsl:text>
    </xsl:when>
    <xsl:otherwise>
    <xsl:text>We got results</xsl:text>
    </xsl:otherwise>
    </xsl:choose>

    Hope this helps,
    Dan

  • Garrett Fisher 341 posts 496 karma points
    Jun 01, 2010 @ 17:48
    Garrett Fisher
    0

    Thanks as always, @dandrayne-- this works great.  One further question though.  In order to display the results, inside the <xsl:otherwise>, I assume I have to do the loop again? Or is there a way to use the $searchResults variable?

    Regards,

    Garrett

  • Garrett Fisher 341 posts 496 karma points
    Jun 01, 2010 @ 17:50
    Garrett Fisher
    0

    Never mind, Dan-- I figured that out:

    <xsl:for-each select="msxml:node-set($searchResults)/node">

    //Garrett

Please Sign in or register to post replies

Write your reply to:

Draft