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?
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.
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?
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?
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
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)! ;-)
Here's a quick (and dirty) example of how to get prevalues loaded in a control.
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
In Xslt it would be something like this:
Here's a good post by Lee
http://www.blogfodder.co.uk/post/Getting-Dropdown-List-DataType-Values-In-Your-HTML.aspx
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
Hi Garrett
I've used something like the following in the past - basically its
Like the following stripped-down example
Hope this helps,
Dan
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
Never mind, Dan-- I figured that out:
//Garrett
is working on a reply...