But in this model, because we cannot use these controls, we need to use pure Javascript and html static code to implement them. It seems quite troublesome? Actually ......
Since the paging code is returned after being processed by the intermediate layer of the Webservice interface server, our sorting code can also be like this, but it is okay to sort the code before returning it. Let's analyze it below.
After reading the previous article, we can know that processing data on the intermediate layer of the Webservice interface server is to manipulate a generic List object.
Code
Copy codeThe Code is as follows:
List <TB_WEB_NZ_INVESTMENT> list = new List <TB_WEB_NZ_INVESTMENT> ();
List = objBusinessFacade. GetTB_WEB_NZ_INVESTMENT_CollectionByFlowID (FlowID );
Return new JavaScriptSerializer (). Serialize (list );
Then our sorting code is embedded between the blue row and the red row.
How to sort it? The List object contains an Sort () method.
Obviously, we cannot use the default comparator for sorting, because this will not achieve the goal (we need to click a column on the page to sort by this column, by default, the comparator cannot achieve this precise control.) Note: The database is not used for sorting. Why? Because there is no need to solve the issues that can be handled by List generic objects through databases.
List. Sort (generic IComparer)
IComparer is an interface used by the System. Collections. Generic. List. Sort and System. Collections. Generic. List. BinarySearch methods. It provides a way to customize the sorting order of a set.
This interface has a method int Compare (a, B) that needs to be overloaded)
Sort parameters a and B in ascending or descending order.
Copy codeThe Code is as follows:
Public int Compare (obj x, obj y)
{
// If the data type of the table field corresponding to the column to be compared is DateTime, different data types correspond to different
Return DateTime. Compare (x, y); -- Ascending
// Return DateTime. Compare (y, x); -- descending order
}
Start building intermediate layer comparator object
Code
Copy codeThe Code is as follows:
/// <Summary>
/// Object [creator] Comparator
/// </Summary>
Public class ContributivePerson_INV_Comparer: IComparer <TB_WEB_NZ_INVESTMENT>
{
Private ESortType m_ESortType = ESortType. ASC;
Public ContributivePerson_INV_Comparer (ESortType eSortType)
{
M_ESortType = eSortType;
}
Public int Compare (TB_WEB_NZ_INVESTMENT x, TB_WEB_NZ_INVESTMENT y)
{
Int rtnCompare = 0 ;;
Switch (m_ESortType)
{
Case ESortType. ASC:
RtnCompare = string. Compare (x. INV, y. INV );
Break;
Case ESortType. DESC:
RtnCompare = string. Compare (y. INV, x. INV );
Break;
} // Switch
Return rtnCompare;
}
} // Class
/// <Summary>
// [Balance payment period] Comparator
/// </Summary>
Public class ContributivePerson_BALDEPER_Comparer: IComparer <TB_WEB_NZ_INVESTMENT>
{
Private ESortType m_ESortType = ESortType. ASC;
Public ContributivePerson_BALDEPER_Comparer (ESortType eSortType)
{
M_ESortType = eSortType;
}
Public int Compare (TB_WEB_NZ_INVESTMENT x, TB_WEB_NZ_INVESTMENT y)
{
Int rtnCompare = 0 ;;
DateTime xDateTime = DateTime. Parse (x. BALDEPER. ToString ());
DateTime yDateTime = DateTime. Parse (y. BALDEPER. ToString ());
Switch (m_ESortType)
{
Case ESortType. ASC:
RtnCompare = DateTime. Compare (xDateTime, yDateTime );
Break;
Case ESortType. DESC:
RtnCompare = DateTime. Compare (yDateTime, xDateTime );
Break;
} // Switch
Return rtnCompare;
}
} // Class
From the above Code, we construct two comparator: [investor], [Balance payment period]
We construct a factory method to facilitate the call.
Code
Copy codeThe Code is as follows:
/// <Summary>
/// Object sorting comparator Factory
/// </Summary>
Public class ContributivePerson_SortComparerFactory
{
/// <Summary>
///
/// </Summary>
/// <Param name = "FieldName"> </param>
/// <Returns> </returns>
Public IComparer <TB_WEB_NZ_INVESTMENT> GetSortComparer (string FieldName, ESortType eSortType)
{
IComparer <TB_WEB_NZ_INVESTMENT> IComparer = null;
Switch (FieldName)
{
Case "BALDEPER": // Balance payment period
IComparer = new ContributivePerson_BALDEPER_Comparer (eSortType );
Break;
Case "INV": // investor
IComparer = new ContributivePerson_INV_Comparer (eSortType );
Break;
} // Switch
Return IComparer;
}
} // Class
Next we will use this method, which is a new method written on the Webservice Interface side. We can see that the red code segment is the sorting block, and the green annotation is the filtering code block (the code has been omitted)
Code
Copy codeThe Code is as follows:
/// <Summary>
/// Fill the table with the paging function on the server (with sorting)
/// </Summary>
/// <Param name = "FlowID"> </param>
/// <Param name = "PageCount"> entries per page </param>
/// <Param name = "CurrentPage"> current page </param>
/// <Param name = "SortType"> sorting type: "ASC", "DESC" </param>
/// <Returns> </returns>
[WebMethod]
[ScriptMethod (ResponseFormat = ResponseFormat. Json)]
Public string Load_ContributivePerson_Table_Sort (string FlowID, int PageCount,
Int CurrentPage, string SortType, string SortFieldName)
{
List <TB_WEB_NZ_INVESTMENT> list = new List <TB_WEB_NZ_INVESTMENT> ();
List = objBusinessFacade. GetTB_WEB_NZ_INVESTMENT_CollectionByFlowID (FlowID );
ContributivePerson_SortComparerFactory objFactory = new ContributivePerson_SortComparerFactory ();
IComparer <TB_WEB_NZ_INVESTMENT> objSort = null;
If (SortType. ToUpper (). Trim () = "ASC ")
{
ObjSort = objFactory. GetSortComparer (SortFieldName, ESortType. ASC );
}
Else if (SortType. ToUpper (). Trim () = "DESC ")
{
ObjSort = objFactory. GetSortComparer (SortFieldName, ESortType. DESC );
}
List. Sort (objSort );
// Skip part of the code and skip the Code. For more information, see the previous article.
Return new JavaScriptSerializer (). Serialize (list );
}
By adding a comparator, we have achieved the desired sorting on the generic list object without the need to sort through SQL statements. On the client page, you only need to add necessary parameters. The middle-layer server has implemented all the cores. The client code only needs to identify which column to sort, at the same time, pay attention to the [homepage] [Next Page] and other pages in order. Here, the client code is skipped.
:
Sort by [Balance payment term] column in ascending order
Sort by [investors] in descending order