我希望允许用户选择要排序的列。我认为唯一的方法是进入数据视图属性,并启用带有排序的工具栏。这可以工作,并允许用户使用列的下拉列表选择要排序的列。默认值为“none”,当页面第一次加载时,不应用排序(行是按其ID显示的,这是非常随机的)。
我希望默认排序是一个特定的列-即,当第一次打开页面时,我希望数据按“姓氏”列排序。我不知道如何使用工具栏排序来实现这一点。
我曾尝试在获取数据的SQL语句中指定按姓氏排序的顺序-但是这最终会覆盖工具栏排序,因此排序总是按姓氏排序,而不管工具栏排序选择如何。
在数据视图属性中还有另一个“排序”选项,它允许您指定排序,但是这同样会覆盖所有内容,并使工具栏排序选项变得无用(选择其他内容没有任何效果)。
下面是为排序工具栏生成的代码。我尝试将“姓氏”更改为-使“姓氏”列成为下拉框中的选中项,这将使“姓氏”列在默认情况下在下拉框中处于选中状态,但实际上没有应用排序。
<table cellSpacing="0" cellPadding="2" border="0" class="ms-toolbar" style="margin-left: 3px; margin-right: 5px;">
<tr>
<td id="dvt_tb_sort" nowrap=""><table><tr><td nowrap="" class="ms-toolbar"><nobr>Sort by: <select>
<xsl:variable name="clvar1_dvt_sortfield">' + this.options[this.selectedIndex].value + '</xsl:variable>
<xsl:variable name="clvar2_dvt_sortfield">' + this.options[this.selectedIndex].fieldtype + '</xsl:variable>
<xsl:variable name="clvar3_dvt_sortfield">' + this.options[this.selectedIndex].title + '</xsl:variable>
<xsl:variable name="clvar4_dvt_sortfield">' + this.options[this.selectedIndex].sorttype + '</xsl:variable>
<xsl:attribute name="OnChange">javascript:<xsl:value-of select="ddwrt:GenFireServerEvent(concat('NotUTF8;dvt_sortfield={', $clvar1_dvt_sortfield, '};dvt_sortdir={', $dvt_sortdir, '};dvt_sorttype={', $clvar4_dvt_sortfield, '}'))" /></xsl:attribute>
<option value="">None</option>
<option value="Location">
<xsl:if test="$dvt_sortfield='Location'">
<xsl:attribute name="selected">yes</xsl:attribute>
</xsl:if>
Location</option>
<option value="Email">
<xsl:if test="$dvt_sortfield='Email'">
<xsl:attribute name="selected">yes</xsl:attribute>
</xsl:if>
Email</option>
<option value="Mobile">
<xsl:if test="$dvt_sortfield='Mobile'">
<xsl:attribute name="selected">yes</xsl:attribute>
</xsl:if>
Mobile</option>
<option value="Position">
<xsl:if test="$dvt_sortfield='Position'">
<xsl:attribute name="selected">yes</xsl:attribute>
</xsl:if>
Position</option>
<option value="Telephone">
<xsl:if test="$dvt_sortfield='Telephone' and not($dvt_sorttype='number')">
<xsl:attribute name="selected">yes</xsl:attribute>
</xsl:if>
Telephone</option>
<option value="Telephone" sorttype="number">
<xsl:if test="$dvt_sortfield='Telephone' and $dvt_sorttype='number'">
<xsl:attribute name="selected">yes</xsl:attribute>
</xsl:if>
Telephone(Number)</option>
<option value="Forename">
<xsl:if test="$dvt_sortfield='Forename' or $dvt_sortfield=''">
<xsl:attribute name="selected">yes</xsl:attribute>
</xsl:if>
Forename</option>
<option value="Surname">
<xsl:if test="$dvt_sortfield='Surname'">
<xsl:attribute name="selected">yes</xsl:attribute>
</xsl:if>
Surname</option>
</select><a>
<xsl:attribute name="href">
<xsl:choose>
<xsl:when test="$dvt_sortdir='descending'">javascript:<xsl:value-of select="ddwrt:GenFireServerEvent(concat('dvt_sortfield={', $dvt_sortfield, '};dvt_sortdir={ascending}'))" /></xsl:when>
<xsl:otherwise>javascript:<xsl:value-of select="ddwrt:GenFireServerEvent(concat('dvt_sortfield={', $dvt_sortfield, '};dvt_sortdir={descending}'))" /></xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:if test="$dvt_sortfield" ddwrt:cf_ignore="1"><img border="0">
<xsl:attribute name="src">
<xsl:choose>
<xsl:when test="$dvt_sortdir='descending'"><xsl:value-of select="ddwrt:FieldSortImageUrl('Asc')" /></xsl:when>
<xsl:otherwise><xsl:value-of select="ddwrt:FieldSortImageUrl('Desc')" /></xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:attribute name="alt">
<xsl:choose>
<xsl:when test="$dvt_sortdir='descending'">Descending</xsl:when>
<xsl:otherwise>Ascending</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</img></xsl:if>
</a></nobr></td></tr></table></td><td width="99%"></td>
</tr>
</table>