Using XSLT search with multiple dropdowns

I've been modifying Douglas Robar's Umbraco XSLT search for a project where I need to filter a bunch of pages using some multiple dropdown lists. This is just a short description of how I've done it.

The scenario I have a bunch of nodes that are categorized using some multiple ddl datatypes. For example onde datatype "Season" with the values "Winter,Spring,Summer,Fall" and another "Theme" - "Theme1,Theme2,Theme3".

Now I want the user to filter all of my nodes by selecting some of the values. The tricky part is that if a node contains "Winter,Spring" and the user searches for "Spring,Summer" there should be a match.

I added a javascript function to the XSLT:

 

<msxsl:script language="JavaScript" implements-prefix="ps">
<![CDATA[
// Here is a lot of functions from the original XSLT
// My new function function CompareParams(params, values){ var par = params.split(","); var vals = values.split(","); var score = 0; var i = 0; for(i = 0; i < par.length; i++){ var j = 0; for(j = 0; j < vals.length; j++){ if(par[i] == vals[j]){ score = score + 1; } } } if(values == ''){ return true; } else { if(score > 0){ return true; } else { return false; } } } ]]> </msxsl:script>

 

Then I added this to my Xpath:

 

<xsl:variable name="itemsSeason" select="$itemsRefined[ps:CompareParams(string(./data[@alias = 'season']),$season) = 'true'
and ps:CompareParams(string(./data[@alias = 'theme']),$theme) = 'true'
and @nodeTypeAlias = 'Aktivitet']" />

 

Creating the search form The next step was to create the search form using the datatypes. Luckily Umbraco has a neat little funtion for that. GetPreValues:

 

<xsl:template match="/">
<!-- start writing XSLT -->
<form action="?" method="GET"> <select multiple="multiple" name="season"> <xsl:for-each select="umbraco.library:GetPreValues('1042')/node()"> <option> <xsl:attribute name="value"> <xsl:value-of select="."/> </xsl:attribute> <xsl:if test="contains(umbraco.library:RequestQueryString('season'),./node())"> <xsl:attribute name="selected">selected</xsl:attribute> </xsl:if> <xsl:value-of select="."/> </option> </xsl:for-each> </select> <select multiple="multiple" name="theme"> <xsl:for-each select="umbraco.library:GetPreValues('1043')/node()"> <option> <xsl:attribute name="value"> <xsl:value-of select="."/> </xsl:attribute> <xsl:if test="contains(umbraco.library:RequestQueryString('theme'),./node())"> <xsl:attribute name="selected">selected</xsl:attribute> </xsl:if> <xsl:value-of select="."/> </option> </xsl:for-each> </select>
<xsl:variable name="searchstring" select="umbraco.library:RequestQueryString('search')" /> <input type="text" name="search" value="{$searchstring}" /> </form> </xsl:template>

 

I hope you all can use this :-)

Download the complete files here

Related posts

Comments

Comments disabled

Comments are disabled for this post.