In Web display, we often encounter paging display. There are many paging methods on the Internet, but they consume too much bandwidth. So I thought of using Ajax to pagination, the returned Json is used to process the returned data, which greatly simplifies the bandwidth pressure. The idea is to asynchronously execute ajax to return the data required by the new list in json format and output the table. You can output the ui li (high output efficiency) on the page.
:
Html code:
Copy codeThe Code is as follows:
Set their Class = "page" so that you can add a Click event operation page to them.
<Div id = "showPage" style = "width: margin PX; margin: 0 auto; display: none" class = "pages">
<Div style = "float: left">
<A id = "first" class = "pages"> homepage </a>
<A id = "prev" class = "pages"> previous page </a>
<A id = "next" class = "pages"> next page </a>
<A id = "last" class = "pages"> last page </a>
Jump to the <input type = "text" id = "txtGoPage" style = "width: 45px; height: 15px; border: 1px solid"/>
Page
</Div>
<Div style = "margin: 0; float: left">
<Input type = "button" class = "pages btn-info" id = "go" value = ""/>
<Span id = "SumCount"> </span> entries in total, <span id = "ItemCount"> </span> entries per page,
Current <span id = "Index"> </span>/<span id = "PageCount"> </span> page
</Div>
</Div>
Use the following div to output the returned results
<Div id = "divBadProductInfo"> </div>
Css code:
Copy codeThe Code is as follows:
/* Paging */
. Pages {
Cursor: pointer;
Text-align: center;
Margin: 0 auto;
Padding-right: 0px;
Padding-bottom: 2px;
Padding-top: 2px;
Font-family: verdana, helvetica, arial, sans-serif;
}
. Pages {
Border-right: 1px solid;
Padding-right: 6px;
Border-top: 1px solid;
Padding-left: 6px;
Padding-bottom: 0px;
Overflow: hidden;
Border-left: 1px solid;
Line-height: 20px;
Margin-right: 2px;
Padding-top: 0px;
Border-bottom: 1px solid;
Height: 30px;
}
. Pages {
Border-left-color: # e6e7e1;
Border-bottom-color: # e6e7e1;
Color: # 09c;
Border-top-color: # e6e7e1;
Background-color: # fff;
Border-right-color: # e6e7e1;
}
. Pages a: hover {
Text-decoration: none;
Border-left-color: # 09c;
Border-bottom-color: # 09c;
Border-top-color: # 09c;
Border-right-color: # 09c;
}
. Pages a. next {
Border-left-color: # 09c;
Border-bottom-color: # 09c;
Border-top-color: # 09c;
Border-right-color: # 09c;
}
JS Code:
Introduction: <script src = "assets/js/jquery-1.8.2.min.js"> </script> // can be another version
Copy codeThe Code is as follows:
$ (Document). ready (function ()
{
// Search criteria
Var search = $ ("# txtFactroy"). val () + "_" + $ ("# txtTimeSelect"). val ()
+ "_" + $ ("# TxtPinfan"). val () + "_" +
$ ('Input [type = "checkbox"] [name = "option1"]: checked'). val ();
$. Ajax ({
Type: "post", <span style = "color: # ff0000;"> // return format
Url: "ResponseHandler. ashx" // return to the general handler for processing. // The return parameter indicates the page on which the request is sent. encodeURIComponent formats Chinese characters to prevent garbled characters.
Data: "BadProductWhere =" + encodeURIComponent (search) + "& currPage = 1 ",
Datatype: "json", // json of the returned data
Async: false, // disable browser caching
Success: function (returnData, textstatus, xmlhttprequest)
{
$ ("# ShowPage" ).css ('display', 'block'); // display the page
$ ("# DivBadProductInfo" ).html (returnData. split ('_') [0]); // the return value is displayed separately.
Var page = returnData. split ('_') [1]. split (',');
$ ("# SumCount"). text (page [0]); // total number of data records
$ ("# ItemCount"). text (page [1]); // how many data entries per page
$ ("# Index"). text (page [2]); // current page
$ ("# PageCount"). text (page [3]); // total number of pages}
});
// Clear the redirection page
$ ("# TxtGoPage"). val ("");
// Paging operation
$ (". Pages"). click (function (){
// When the total number of pages is greater than 1, the upper and lower first and last pages are available.
If (parseFloat ($ ("# PageCount" pai.html ()> 1 ){
// Obtain whether the control type is ID or class
Var type = $ (this). attr ("id ");
// Obtain the current page number
Var thisindex = $ ("# Index"). text ();
Var search = $ ("# txtFactroy"). val () + "_" + $ ("# txtTimeSelect"). val ()
+ "_" + $ ("# TxtPinfan"). val () + "_" +
$ ('Input [type = "checkbox"] [name = "option1"]: checked'). val ();
Switch (type ){
Case 'first ':
{
$ ("# TxtGoPage"). val ("");
Badpageindex = 1;
BadPageIndex (1, search); // Ajax return Function
Return;
}
Case 'prev ':
{
$ ("# TxtGoPage"). val ("");
Badpageindex = parseInt (thisindex)-1;
If (badpageindex <1) return;
BadPageIndex (badpageindex, search );
Return;
}
Case 'Next ':
{
$ ("# TxtGoPage"). val ("");
Badpageindex = parseInt (thisindex) + 1;
If (badpageindex> parseInt ($ ("# PageCount" ).html () return;
Else
BadPageIndex (badpageindex, search );
Return;
}
Case 'last ':
{
Var max = parseInt ($ ("# PageCount" ).html ());
$ ("# TxtGoPage"). val ("");
Badpageindex = max;
BadPageIndex (max, search );
Return;
}
Case 'Go ':
{
Var _ go = $ ("# txtGoPage"). val ();
Badpageindex = _ go;
BadPageIndex (_ go, search );
Return;
}
}
}
})
});
Copy codeThe Code is as follows:
Var badpageindex;
// Index, for example, page index 1, 2, 3
// BadProductWhere query Condition
Function BadPageIndex (index, searchwhere ){
$. Ajax ({
Type: "post ",
Url: "ResponseHandler. ashx ",
Data: "BadProductWhere =" + encodeURIComponent (searchwhere) + "& currPage =" + index,
Datatype: "json ",
Async: false,
Success: function (returnData, textstatus, xmlhttprequest ){
$ ("# DivDisplay" ).css ('display', 'None ');
$ ("# ShowPage" ).css ('display', 'block ');
$ ("# DivBadProductInfo" ).html (returnData. split ('_') [0]);
Var page = returnData. split ('_') [1]. split (',');
$ ("# SumCount"). text (page [0]);
$ ("# ItemCount"). text (page [1]);
$ ("# Index"). text (page [2]);
$ ("# PageCount"). text (page [3]);
},
Error: function (){
Alert ("service error ");
}
});
}
C # code: (ResponseHandler. ashx)
Copy codeThe Code is as follows:
/// <Summary>
/// Number of entries per page
/// </Summary>
Private int pageSize = 20;
StringBuilder sbBadProductInfo = new StringBuilder ();
If (! String. IsNullOrEmpty (context. Request ["BadProductWhere"]) &
! String. IsNullOrEmpty (context. Request ["currPage"])
{
# Region // product B title information
SbBadProductInfo. Append (@ "<div class = 'row-fluid'>
<Div class = 'span12 components'> <div class = 'widget-header'>
<Span class = 'title'>
<I class = 'icol-blog'> </I> information about the product list
</Span>
</Div> ");
SbBadProductInfo. Append (@ "<div class = 'widget-content summary-list'>
<Div class = 'row-fluid' style = 'padding-top: 2px; '>
<Div class = 'span12 section '>
<Table class = 'table table-bordered table-striped'>
<Thead>
<Tr>
<Th> Number of the box ticket </th>
<Th> boxing generation date </th>
<Th> factory name </th>
<Th> packing times </th>
<Th> packing box number </th>
<Th> Number of packages </th>
<Th> warehouse confirmation </th>
<Th> warehouse picking validator </th>
<Th> date of delivery </th>
<Th> View Details </th>
</Tr>
</Thead> <tbody> ");
# Endregion
List <bstate_view> lstGetBadProductData = (from p in lstGetBadProductData
Where! String. IsNullOrEmpty (p. Warehouse picking validator) & p. Warehouse picking date! = Null
Select p). ToList <bstate_view> ();
String pageInfo = lstGetBadProductData. Count () + "," + pageSize + "," + context. Request ["currPage"] +
"," + (LstGetBadProductData. Count () % 20 = 0? (LstGetBadProductData. Count ()/20 ):
(LstGetBadProductData. Count ()/20 + 1 ));
List <bstate_view> lstGetBadItemData = (lstGetBadProductData. Count> pageSize?
LstGetBadProductData. Skip (int. Parse (context. Request ["currPage"]) = 1? 0:
PageSize * (int. Parse (context. Request ["currPage"])-1). Take (pageSize)
: LstGetBadProductData). ToList <bstate_view> ();
# Region => B box ticket information
Foreach (var item in lstGetBadItemData)
{
Var cssName = rowCount % 2 = 0? "Warning": "error ";
SbBadProductInfo. Append (@ "<tr class = '" + cssName + "'> ");
SbBadProductInfo. Append (@ "<td>" + item. Box No. + "</td> ");
SbBadProductInfo. Append (@ "<td>" + item. Boxed generation date + "</td> ");
SbBadProductInfo. Append (@ "<td>" + item. Factory name + "</td> ");
SbBadProductInfo. Append (@ "<td>" + item. Packing times + "</td> ");
SbBadProductInfo. Append (@ "<td>" + item. Boxed box number + "</td> ");
SbBadProductInfo. Append (@ "<td>" + item. Number of packages + "</td> ");
SbBadProductInfo. Append (@ "<td>" + item. Warehouse confirmation + "</td> ");
SbBadProductInfo. Append (@ "<td>" + item. Warehouse picking confirmer + "</td> ");
SbBadProductInfo. Append (@ "<td>" + item. Outbound date + "</td> ");
SbBadProductInfo. Append (@ "<td> <input type = 'button 'class = 'btn btn-primary'
Style = 'width: 80px; height: 30px; 'value = 'view details '");
SbBadProductInfo. Append (@ "onclick = 'openbadinfo (" + item. Box Number + "); '/> </td> </tr> ");
RowCount ++;
}
SbBadProductInfo. Append (@ "</tbody> </table>
</Div>
</Div>
</Div>
</Div>
</Div>
</Div>
</Div> ");
# Endregion
Context. Response. Write (sbBadProductInfo. ToString () + "_" + pageInfo );
Context. Response. End ();
Paging effect: