Sorting Displays in Data View Web Parts )

來源:互聯網
上載者:User

 

I figured out a great little trick for sorting a DVWP-based display based on a column name passed in the Query String.  You can see this in action in the default All Items or All Documents views in lists or libraries.  When you click on the column headers that allow sorting, the browser is sent to the same page with the Query String looking something like:

http://[servername]/Lists/[listname]Forms/AllItems.aspx?SortField=ContentType&SortDir=Asc

By passing the column name that you want to sort on in the Query String, the List View Web Part “knows” what to sort the display on and in what order.

If you want to build analogous functionality in a DVWP, you can easily rig the column headers to act the same way, but the trick is to get the items to then sort correctly.  In your body template, you need some XSL like the following:

 

<xsl:variable name="AscDesc"> <xsl:choose>  <xsl:when test="string-length($SortDir) = 0 or $SortDir = 'Asc'">ascending</xsl:when>  <xsl:otherwise>descending</xsl:otherwise> </xsl:choose</xsl:variable><xsl:for-each select="$Rows"> <xsl:sort select="@*[name()=$SortField]" order="{$AscDesc}" />  <xsl:call-template name="Community_ContentDetailed.rowview">  <xsl:with-param name="SiteType" select="$SiteType" />   <xsl:with-param name="CommunityContentFilterValue" select="$CommunityContentFilterValue" /> </xsl:call-template></xsl:for-each>

 

 

The cool line is #8.  It allows you to take the Query String value for SortField and use it as the column name by which to sort.  The select parameter in the xsl:sort evaluates to @ContentType (from the URL above).

I wanted to be able to easily add the sorting option to header columns in my DVWPs, so I created the following utility template to create the links.  It decides based on the Query String values whether this is the first or second click of the column header (ascending or descending) and shows the appropriate image indicator for the result.

 

 

<xsl:template name="SortHeader">

  < xsl:param name = "LinkText" />   < xsl:param name = "ColumnName" />   < xsl:param name = "SortField" />   < xsl:param name = "SortDir" />   < xsl:variable name = "ASortIMG" select = "'/_layouts/images/sortaz.gif'" />   < xsl:variable name = "DSortIMG" select = "'/_layouts/images/sortza.gif'" />   < xsl:variable name = "SortableIMG" select = "'/SiteCollectionImages/sortable.gif'" />   < xsl:variable name = "AscDesc" >    < xsl:choose >     < xsl:when test = "$ColumnName = $SortField and (string-length($SortDir) = 0 or $SortDir = 'Asc')" >Desc</ xsl:when >     < xsl:otherwise >Asc</ xsl:otherwise >    </ xsl:choose >   </ xsl:variable >   < img alt = "Sortable" border = "0" src = "{$SortableIMG}" />   < a href = "#" >    < xsl:attribute name = "onclick" >     document.location = '< xsl:value-of select = "$URL" />' +      '?SortField=' + '< xsl:value-of select = "$ColumnName" />' +      '&amp;SortDir=' + '< xsl:value-of select = "$AscDesc" />';    </ xsl:attribute >    < xsl:value-of select = "$LinkText" />    < span style = "padding-left:2px;" >    < xsl:choose >     < xsl:when test = "$ColumnName = $SortField and ($SortDir = '' or $SortDir = 'Asc')" >      < img alt = "Ascending - Click to Reverse" border = "0" src = "{$ASortIMG}" />     </ xsl:when >     < xsl:when test = "$ColumnName = $SortField and $SortDir = 'Desc'" >      < img alt = "Descending - Click to Reverse" border = "0" src = "{$DSortIMG}" />     </ xsl:when >     < xsl:otherwise >     </ xsl:otherwise >    </ xsl:choose >    </ span >   </ a > </ xsl:template >

 

Come from:

http://sympmarc.com/2008/12/16/sorting-displays-in-data-view-web-parts/

 

Other Link:

 

http://social.msdn.microsoft.com/Forums/en/sharepointcustomization/thread/01a994b1-579a-4a82-9d8b-70715730b4e6


 


 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.