Sorting Displays in Data View Web Parts (转)

 

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


 


 



转载于:https://www.cnblogs.com/LeimOO/archive/2012/03/12/2391906.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值