对数据岛数据实现动态排序二法


现在数据岛被越来越广泛的应用;其中必然会涉及到的就是动态排序了;下面列出两种实现方法:

1、为<xsl:param>标签设定参数实现动态排序(推荐)
<html>
<body>
<table DATASRC="#catalogs" border=1>
<thead>
<tr>
<td οnclick="sort('TITLE','descending');">TITLE</td>
<td>ARTIST</td>
<td οnclick="sort('COUNTRY','ascending');">COUNTRY</td>
<td>COMPANY</td>
<td>PRICE</td>
<td>YEAR</td>
</tr>
</thead>
<tbody>
<tr>
<td ><div DATAFLD="TITLE"></div></td>
<td ><div DATAFLD="ARTIST"></div></td>
<td ><div DATAFLD="COUNTRY"></div></td>
<td ><div DATAFLD="COMPANY"></div></td>
<td ><div DATAFLD="PRICE"></div></td>
<td ><div DATAFLD="YEAR"></div></td>
</tr>
</tbody>
</table>

<xml id="catalogs">
<CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
Bob Dylan
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
<CD>
<TITLE>Burlesque</TITLE>
Dylan
<COUNTRY>UA</COUNTRY>
<COMPANY>umbia</COMPANY>
<PRICE>1.90</PRICE>
<YEAR>1987</YEAR>
</CD>
<CD>
<TITLE>Empire</TITLE>
Bob
<COUNTRY>US</COUNTRY>
<COMPANY>bia</COMPANY>
<PRICE>12.90</PRICE>
<YEAR>1995</YEAR>
</CD>
</CATALOG>
</xml>

<xml id="xstyle">
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml"/>
<xsl:param name="sortorder" select="'descending'"/>
<xsl:param name="sortfield" select="'xxx'"/>
<xsl:template match="/">
<CATALOG>
<xsl:for-each select="CATALOG/CD">
<xsl:sort select="*[name()=$sortfield]" order="{$sortorder}" />
<CD>
<TITLE><xsl:value-of select="TITLE"/></TITLE>
<xsl:value-of select="ARTIST"/>
<COUNTRY><xsl:value-of select="COUNTRY"/></COUNTRY>
<COMPANY><xsl:value-of select="COMPANY"/></COMPANY>
<PRICE><xsl:value-of select="PRICE"/></PRICE>
<YEAR><xsl:value-of select="YEAR"/></YEAR>
</CD>
</xsl:for-each>
</CATALOG>
</xsl:template>
</xsl:stylesheet>
</xml>

<script language="vbscript">
function sort(strSortField, strSortOrder)

  dim objXSL, objXML, objTemplate, objProcessor, strHTML, strDrinkType
 
  Set objXML = CreateObject("Msxml2.FreeThreadedDOMDocument")
  Set objXSL = CreateObject("Msxml2.FreeThreadedDOMDocument")
  
  'Load the XML document
  objXML.async = False
  objXML.Loadxml catalogs.xml  
  
  'Load the XSL document
  objXSL.async = False
  objXSL.Loadxml xstyle.xml
     
  'Create an instance of our XSL Template object
  Set objTemplate = CreateObject("MSXML2.XSLTemplate")
     
  'Create an instance of our stylesheet object using our recently loaded XSLT document
  Set objTemplate.stylesheet = objXSL
      
  'Create an instance of our Processor object
  Set objProcessor = objTemplate.createProcessor

  'Define the input object for our object equal to our recently loaded XML document
  objProcessor.input = objXML

  'Now, finally we can add any parameters that we require to our Template processor
  objProcessor.AddParameter "sortfield", strSortField
  objProcessor.AddParameter "sortorder", strSortOrder
     
  'Last but not least we do our transformation
  objProcessor.Transform
     
  'Store the results of the output into a string.
  strXML = objProcessor.output

  'Load up an XML DOM object from the recent XML output
  objXML.loadxml strXML
  
  'Select only the "employees" elements from our document object
  objXML.selectNodes("//CATALOG")
  
  'Load our Data Island using our new XML object
  catalogs.loadxml  objXML.xml  
 end function  
</script>

<script language="javascript">
function sort2(xmlObj, xslObj, sortByColName)
{
var xmlData=eval("document.all."+xmlObj).XMLDocument;
var xslData=eval("document.all."+xslObj).XMLDocument;
var nodes=xslData.documentElement.selectSingleNode("xsl:for-each");
nodes.selectSingleNode("@order-by").value=sortByColName;

xmlData.documentElement.transformNodeToObject(xslData.documentElement,xmlData);
}
</script>

</body>
</html>

2、直接通过DOM实现,灵活性欠缺:
<html>
<body>
<table DATASRC="#catalogs" border=1>
<thead>
<tr>
<td οnclick="sort('catalogs','xstyle','TITLE');">TITLE</td>
<td>ARTIST</td>
<td>COUNTRY</td>
<td>COMPANY</td>
<td>PRICE</td>
<td>YEAR</td>
</tr>
</thead>
<tbody>
<tr>
<td ><div DATAFLD="TITLE"></div></td>
<td ><div DATAFLD="ARTIST"></div></td>
<td ><div DATAFLD="COUNTRY"></div></td>
<td ><div DATAFLD="COMPANY"></div></td>
<td ><div DATAFLD="PRICE"></div></td>
<td ><div DATAFLD="YEAR"></div></td>
</tr>
</tbody>
</table>

<xml id='catalogs'>
<CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
Bob Dylan
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
<CD>
<TITLE>Burlesque</TITLE>
Dylan
<COUNTRY>UA</COUNTRY>
<COMPANY>umbia</COMPANY>
<PRICE>1.90</PRICE>
<YEAR>1987</YEAR>
</CD>
<CD>
<TITLE>Empire</TITLE>
Bob
<COUNTRY>US</COUNTRY>
<COMPANY>bia</COMPANY>
<PRICE>12.90</PRICE>
<YEAR>1995</YEAR>
</CD>
</CATALOG>
</xml>

<xml id="xstyle">
<CATALOG>
<xsl:for-each select="CD" order-by="+TITLE" xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<CD>
<TITLE><xsl:value-of select="TITLE"/></TITLE>
<xsl:value-of select="ARTIST"/>
<COUNTRY><xsl:value-of select="COUNTRY"/></COUNTRY>
<COMPANY><xsl:value-of select="COMPANY"/></COMPANY>
<PRICE><xsl:value-of select="PRICE"/></PRICE>
<YEAR><xsl:value-of select="YEAR"/></YEAR>
</CD>
</xsl:for-each>
</CATALOG>
</xml>

<script language="javascript">
function sort(xmlObj, xslObj, sortByColName)
{
var xmlData=eval("document.all."+xmlObj).XMLDocument;
var xslData=eval("document.all."+xslObj).XMLDocument;
var nodes=xslData.documentElement.selectSingleNode("xsl:for-each");
nodes.selectSingleNode("@order-by").value=sortByColName;

xmlData.documentElement.transformNodeToObject(xslData.documentElement,xmlData);
}
</script>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页
评论

打赏作者

colee

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值