Sorting a table in asp.net MVC

前端 未结 5 1268
半阙折子戏
半阙折子戏 2021-02-07 08:36

I was wondering how people were going about sorting a table in asp.net mvc? I\'ve heard of javascript solutions that work pretty well with non-paged tables, such as jquery\'s ta

5条回答
  •  花落未央
    2021-02-07 08:54

    Definately liking Jan's solution - thanks a lot Jan... You just saved me some 60 lines of code with a case statement parsing each of the column headers. The solution "makes a great toggle click, only sort by 1 column solution on tables", when used with two session variables, one to retain the ASC/DESC as a boolean and one to hold the Type/Column Name.

    I used this C# Example Extension and implemented it with VB this afternoon. I managed to cut a 30 line case statement into one line of code.

    AT THE VIEW:

    Date ">
    

    THE EXTENSION (Public Module OrderByExtender):

    Imports System.Linq.Expressions
    
    Public Function OrderBy(Of T)(collection As IEnumerable(Of T), key As String, isDescending As Boolean) As IOrderedEnumerable(Of T)
        Dim sortLambda As LambdaExpression = BuildLambda(Of T)(key)
        If isDescending Then
            Return collection.OrderByDescending(DirectCast(sortLambda.Compile(), Func(Of T, Object)))
        Else
            Return collection.OrderBy(DirectCast(sortLambda.Compile(), Func(Of T, Object)))
        End If
    End Function
    
    Public Function ThenBy(Of T)(collection As IOrderedEnumerable(Of T), key As String, isDescending As Boolean) As IOrderedEnumerable(Of T)
        Dim sortLambda As LambdaExpression = BuildLambda(Of T)(key)
    
        If (isDescending) Then
            Return collection.ThenByDescending(DirectCast(sortLambda.Compile(), Func(Of T, Object)))
        Else
            Return collection.ThenBy(DirectCast(sortLambda.Compile(), Func(Of T, Object)))
        End If
    End Function
    
    Private Function BuildLambda(Of T)(key As String) As LambdaExpression
        Dim TParameterExpression As ParameterExpression = Expression.Parameter(GetType(T), "p")
        Dim sortLambda As LambdaExpression = Expression.Lambda(Expression.Convert(Expression.[Property](TParameterExpression, key), GetType(Object)), TParameterExpression)
        Return sortLambda
    End Function
    

    AT THE CONTROLLER ACTION:

    Public Function SortStationVisits(Optional page As Integer = 1, Optional SortField As String = "") As ActionResult
        Dim sps = LoadSession()
    
        If SortField = sps.StationVisitSorter Then
            sps.StationVisitDescOrder = Not (sps.StationVisitDescOrder)
        Else
            sps.StationVisitDescOrder = False
        End If
    
        sps.StationVisitSorter = SortField
    
        SaveSession(sps)
        Return RedirectToAction("Show")
    End Function
    

    AT THE CONTROLLER SHOW METHOD (1 line of code W00T!) :

    spv.SelectableStationVisits = spv.SelectableStationVisits.OrderBy(sps.StationVisitSorter, sps.StationVisitDescOrder).ToList
    

提交回复
热议问题