使用SPD2010定制XsltListViewWebPart根据条件隐藏列
XsltListViewWebPart对于SharePoint 2010来说是个新玩意,这个web part继承于我们经常在2007时代可以看到的DataFormWebPart,并且在2010里面承担起了解析List显示效果的重任。
这个Web Part带来的一个明显的好处就是你可以查看,修改显示视图的设置,CAML查询,ViewFields等等。
另外一个好处就是如果需要高级定制,只需要按需XSLT化相关列,而不是整个视图。
你可以选中一个列,使用Customize Item来做到。
试想一下,如果你有几十个列,就为了编辑其中一列,XSLT化整个视图,看着你原本数百行的文件,变成了数千,数万行,然后SPD一次又一次的崩溃,只能欲哭不能。
既然可以按需编辑列,SharePoint怎么实现这个呢?
默认情况下,SharePoint生成ID列作为例子,如下:
不需要懂太多。
对于一列,会生成一个表头和一个内容列,根据match的结果进行解析。例如ID列就是match="FieldRef[@Name='ID']"。如果需要复写其他列,修改成其他名字就行。
<xsl:template name="FieldRef_header.ID" ddwrt:dvt_mode="header" match="FieldRef[@Name='ID']" mode="header" ddwrt:ghost="" xmlns:ddwrt2="urn:frontpage:internal">
<th nowrap="nowrap" scope="col" onmouseover="OnChildColumn(this)">
<xsl:attribute name="class">
<xsl:choose>
<xsl:when test="(@Type='User' or @Type='UserMulti') and ($PresenceEnabled='1')">ms-vh</xsl:when>
<xsl:otherwise>ms-vh2</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:call-template name="dvt_headerfield">
<xsl:with-param name="fieldname">
<xsl:value-of select="@Name" />
</xsl:with-param>
<xsl:with-param name="fieldtitle">
<xsl:value-of select="@DisplayName" />
</xsl:with-param>
<xsl:with-param name="displayname">
<xsl:value-of select="@DisplayName" />
</xsl:with-param>
<xsl:with-param name="fieldtype">
<xsl:choose>
<xsl:when test="@Type='Number' or @Type='Currency'">number</xsl:when>
<xsl:otherwise>x:string</xsl:otherwise>
</xsl:choose>
</xsl:with-param>
</xsl:call-template>
</th>
</xsl:template>
<xsl:template name="FieldRef_printTableCell_EcbAllowed.ID" match="FieldRef[@Name='ID']" mode="printTableCellEcbAllowed" ddwrt:dvt_mode="body" ddwrt:ghost="" xmlns:ddwrt2="urn:frontpage:internal">
<xsl:param name="thisNode" select="."/>
<xsl:param name="class" />
<td>
<xsl:if test="@ClassInfo='Menu' or @ListItemMenu='TRUE'">
<xsl:attribute name="height">100%</xsl:attribute>
<xsl:attribute name="onmouseover">OnChildItem(this)</xsl:attribute>
</xsl:if>
<xsl:attribute name="class">
<xsl:call-template name="getTDClassValue">
<xsl:with-param name="class" select="$class" />
<xsl:with-param name="Type" select="@Type" />
<xsl:with-param name="ClassInfo" select="@ClassInfo" />
</xsl:call-template>
</xsl:attribute>
<xsl:apply-templates select="." mode="PrintFieldWithECB">
<xsl:with-param name="thisNode" select="$thisNode" />
</xsl:apply-templates>
</td>
</xsl:template>
现在很简单了,如果要隐藏ID列,只需要去掉中间所有的XSLT样式就行。只保留如下两行。
<xsl:template name="FieldRef_header.ID" ddwrt:dvt_mode="header" match="FieldRef[@Name='ID']" mode="header" ddwrt:ghost="" xmlns:ddwrt2="urn:frontpage:internal"></xsl:template>
<xsl:template name="FieldRef_printTableCell_EcbAllowed.ID" match="FieldRef[@Name='ID']" mode="printTableCellEcbAllowed" ddwrt:dvt_mode="body" ddwrt:ghost="" xmlns:ddwrt2="urn:frontpage:internal"></xsl:template>
如果隐藏所有多行文本呢,
<xsl:template ddwrt:dvt_mode="header" match="FieldRef[@Type='Note']" mode="header" ddwrt:ghost="" xmlns:ddwrt2="urn:frontpage:internal"></xsl:template>
<xsl:template match="FieldRef[@Type='Note']" mode="printTableCellEcbAllowed" ddwrt:dvt_mode="body" ddwrt:ghost="" xmlns:ddwrt2="urn:frontpage:internal"></xsl:template>
以此类推。
问题:为什么我们要隐藏列而不是直接从Add / Remove Columns 去掉?
这是因为Add / Remove Columns 修改的是CAML查询中的ViewFields,如果去掉了,这个列就不会从Web Service返回。
对于特定需求,例如需要增加一列来根据其他列的值来显示不同的内容,但是不想直接显示引用的列,就需要隐藏了。
其他情况,可以直接删除该列。



浙公网安备 33010602011771号