使用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']"。如果需要复写其他列,修改成其他名字就行。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | < xsl:template name="FieldRef_header.ID" ddwrt:dvt_mode="header" <font style="background-color: rgba(255, 255, 0, 1)">match="FieldRef[@Name='ID']"</ font > 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" <font style="background-color: rgba(255, 255, 0, 1)">match="FieldRef[@Name='ID']"</ font > 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返回。
对于特定需求,例如需要增加一列来根据其他列的值来显示不同的内容,但是不想直接显示引用的列,就需要隐藏了。
其他情况,可以直接删除该列。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南