JQuery to build a customer/service-separated link model sorting analysis in table _jquery

Source: Internet
Author: User
But in this model, because we can't take advantage of these controls, we need to use pure JavaScript and HTML static code to achieve, it seems to be very troublesome? In fact ...
Paging code since the WebService interface service side of the middle layer processing good return, then our sorting code can also be like this, just before the return to sort on OK, the following analysis.
Read the previous article, you can know that in the WebService interface server-side middle-tier processing data is a generic list object manipulation
Code
Copy Code code 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 and red lines.
How to sort it? List This object has a sort () method

Obviously we can't sort by using the default comparer, because if this is not the goal (we need to click on a column on the page to sort by that column, and the default comparer does not achieve this precise control), note: This is not used in the database sorting, why? Because things that can be handled through a list generic object do not need to be resolved through the database.
List.sort (Generic IComparer)
IComparer are interfaces that are used by the System.Collections.Generic.List.Sort and System.Collections.Generic.List.BinarySearch methods. It provides a way to customize the sort order of a collection.
The interface has a method that requires overloading int Compare (A,B)
To achieve ascending and descending arrangement by adjusting the order of the parameter a,b
Copy Code code as follows:

public int Compare (obj x, obj y)
{
If the data type of the table field that corresponds to the column you are comparing is datetime, the different data types correspond to different
Return Datetime.compare (x,y); --Ascending
Return Datetime.compare (Y,X); --Descending
}

Start building a middle-tier comparer object
Code
Copy Code code as follows:

<summary>
Object "Investor" 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 term" 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 have constructed two comparators, namely "investor", "balance payment period"
We construct a factory method to facilitate the call
Code
Copy Code code as follows:

<summary>
Object Sort Comparer 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

Here we use, this method is a WebService interface end of the new write method, we see the Red Code snippet is a sort block, green annotation is a filter code block (code has been omitted)
Code
Copy Code code as follows:

<summary>
Paging function table Fill service end (with sort)
</summary>
<param name= "Flowid" ></param>
<param name= "PageCount" > per page number </param>
<param name= "currentpage" > Current page </param>
<param name= "SortType" > Sort 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);
Part of the code omitted, omit the code can see the article
return new JavaScriptSerializer (). Serialize (list);
}

By adding a comparer, we reached the arbitrary sort on the generic list object without needing to sort through the SQL statements. In the client's page as long as the necessary parameters can be implemented, the middle-tier server has implemented all the core, the client code only needs to determine which column to be sorted, and note that "first page" "Next" and so on in order to page the sequence, where the client code omitted
Effect Chart:
In ascending order BY "balance Payment term" column

Sorted by "investor" in descending order

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.