1

我正在处理根据选择的过滤器动态构建的表。我目前可以按我单击的任何行进行排序。问题是当我单击另一行时,我需要保留排序依据的第一行,以便我单击的第二行在第一行的范围内进行排序。一个例子我有两列,名字和姓氏。

  **FirstName**        **LastName**
    Bob                  Zimmer
    Bob                  Anderson 
    Kathy                Walege
    Kathy                Ball

所以说我点击第一个名字行。它将保持不变,因为名字已经按顺序排列。但是,当我单击姓氏行时,我希望发生以下情况。

 **FirstName**         **LastName**
   Bob                   Anderson
   Bob                   Zimmer
   Kathy                 Ball
   Kathy                 Walege

我目前正在使用它,因此它一次只能按一列排序。我同时使用 javascript 和 vb.net 来让它与我的代码一起使用。

这是javascript:

    function SortColumn(col)
        {
            __doPostBack('SortBy',col);
        }

这是调用 javascript 函数的 vb 函数:

     Private Function AddSortLinkToColumn(ByVal sSortColumn As String) As String
        Dim sVal As String = ""

        sVal = "<a href=""javascript:SortColumn('" & sSortColumn & "');""><font face=""Webdings"">"
        If m_sSort <> "" AndAlso Split(m_sSort, "-")(0) = sSortColumn AndAlso m_sSortDrxn = "ASC" Then
            sVal &= "&#53;"
        Else
            sVal &= "&#54;"
        End If
        sVal &= "</font></a>"

        Return sVal
    End Function

这当前正在获取正在单击的列并将其与我的全局排序变量匹配。如果它们匹配,则会显示一个向上箭头。如果失败,它会显示一个向下箭头。

我知道我需要以某种方式跟踪被点击的第一列,但我不知道该怎么做。我已经通过在排序中添加另一列在 vb.net 端和 javascript 端进行了尝试,但如果我不能让第一列值保持不变,这对我没有任何好处!

如何保持第一列的排序,然后按第二列排序?任何帮助将不胜感激!

更新

在下面列出的答案的帮助下,我终于得到了这个工作!这是代码:

    function SortColumn(col)
        {
            <% if m_sSort = nothing then %>
                __doPostBack('SortBy', col);
            <% else %>
                __doPostBack('SortBy', document.getElementById("SortField").value + "," + col); 
            <% end if %>
        }

m_sSort 是 vb.net 端的公共变量。“SortField”是存储 m_sSort 值的隐藏字段的名称。

4

1 回答 1

0

Add a hidden field to the page called prevCol or something like that. Since it is a hidden field it will be posted back with the form.

In your postBack event handler, make sure to consume that hidden field and replace its value with the currently clicked column.

In this way you will have the column that is clicked as well as the column that was previously clicked. If you check the value and it is empty you know you are starting off.

Your remove sort button would then clear the prevCol field to ensure that you are starting fresh.

So your flow would look like this:

  • First column click: prevCol is empty so you sort by the currently clicked column and write its identifier to the prevCol field on the returned page.

  • Second column Click: prevCol now has a value. So first sort by that value, then sort by the supplied column. Make sure you write the supplied column to prevCol in the return.

  • Xth column Click: Same as second click. Inherintly this is going to make the prevCol the first column that is sorted each time.

When you click your remove sort button, ensure you are not writing a value to prevCol and thus starting the cycle over.

In this way you are kind of caching your previous column on the client with each response so that it is always present during post back.

EDIT:

I thought it would be simple to alternatively do this entirely on the client side with jQuery as long as the server did not need to know the order of the rows.

So I spent some time thinking about how this would be done using jQuery and I found it was a bit more challenging than I originally anticipated.

with some help from James Padolsey I was able to easily sort the table rows, but the logic of sorting based on the previous column was a bit strange.

This is what I ended up with.

于 2013-05-03T15:59:58.897 回答