Introduction to implementing front-end Paging for table and ul li in js
This article mainly introduces js to implement front-end Paging for table and ul li. If you need it, refer
The Code is as follows:
(Function ($ ){
$. Fn. tablepage = function (oObj, dCountOfPage, fresh_id ){
Var dPageIndex = 1;
Var dNowIndex = 1;
Var sPageStr = "";
Var dCount = 0;
Var oSource = $ (this );
Var sNoSelColor = "# CCCCCC ";
Var sSelColor = "black ";
Var sFontColor = "white ";
Var nowIndex = 1;
Change_page_content ();
Function change_page_content (){
// Obtain the data volume
DCount = oSource. children (). children (). length;
// The alarm rule is displayed.
SPageStr = "<div class = 'ref '> <a href = 'javascript: void (0)' id = 'fresh _"
+ Fresh_id
+ "'Onclick = 'fresh (this. id); 'class = 'sx 'style = 'color: #247AA9; '> <span> refresh </span> </a> </div> ";
SPageStr + = "<div class = 'msdn '> <a href = 'javascript: void (0)'> homepage </a> <a href = 'javascript: void (0) '> previous page </a> <a href = 'javascript: void (0)'> next page </a> <a href = 'javascript: void (0) '> last page </a> </div> ";
SPageStr + = "<div class = 'txt '>" + "Total" + Math. ceil (dCount/dCountOfPage) + "Page, current" + "<strong>" + dNowIndex + "</strong>" + "page" + "</div> ";
OObj.html (sPageStr );
DPageIndex = 1;
// Content in the internal table
Var rr = oSource. children (). children ("tr ");
OSource. children (). children ("tr"). each (function (){
// = 2
If (dPageIndex> = (dNowIndex-1) * dCountOfPage) + 1)
& DPageIndex <= (dNowIndex * dCountOfPage ))){
$ (This). show ();
} Else {
$ (This). hide ();
}
DPageIndex ++;
});
// OSource. children (). children ("tr"). first (). show (); // The head must show
If (dCount <= dCountOfPage ){
Var tt = $ ("# table_page _" + fresh_id). children ('. msdn'). children (). each (function (I ){
If (I = 2 | I = 3 | I = 0 | I = 1 ){
$ (This). addClass ("disabled ");
}
});
}
Else if (dNowIndex = Math. ceil (dCount/dCountOfPage )){
Var tt = $ ("# table_page _" + fresh_id). children ('. msdn'). children (). each (function (I ){
If (I = 2 | I = 3 ){
$ (This). addClass ("disabled ");
}
});
} Else if (dNowIndex = 1 ){
Var tt = $ ("# table_page _" + fresh_id). children ('. msdn'). children (). each (function (I ){
If (I = 0 | I = 1 ){
$ (This). addClass ("disabled ");
}
});
}
// Add a merge tasks event
OObj. children (). children (). each (function (){
$ (This). click (function (){
DNowIndex = $ (this) [0]. innerHTML;
If (dNowIndex = 'homepage '){
DNowIndex = 1;
Change_page_content ();
NowIndex = dNowIndex;
}
If (dNowIndex = 'last page '){
DNowIndex = Math. ceil (dCount/dCountOfPage );
Change_page_content ();
NowIndex = dNowIndex;
}
If (dNowIndex = 'Next page '){
If (nowIndex <Math. ceil (dCount/dCountOfPage )){
DNowIndex = parseInt (nowIndex) + 1;
Change_page_content ();
NowIndex = nowIndex + 1;
}
}
If (dNowIndex = 'preview '){
If (nowIndex> 1 ){
DNowIndex = parseInt (nowIndex)-1;
Change_page_content ();
NowIndex = nowIndex-1;
}
}
});
});
}
};
}) (JQuery );
The Code is as follows:
<Script type = "text/javascript">
$ (Document). ready (function (){
Var totalPage = $ ("ul li"). length;
Var total = Math. ceil ($ ("ul li"). length/5 );
Var current = 1;
// Var index = 4;
If (totalPage> 5 ){
$ ("Ul li: gt (4)"). hide ();
$ ("# BtnPrev "). attr ("class", "tabs-scroller-left-disabled "). attr ("disabled", "disabled "). click (function (){
Debugger;
$ ("# BtnNext"). attr ("class", "tabs-scroller-right ");
$ ("# BtnNext"). removeAttr ("disabled ");
Current-= 1;
Var tt = current;
$ ("Ul li"). show ();
Var indexStart = (current-1 );
Var indexEnd = indexStart + 4;
$ ("Li: lt (" + indexStart + "), li: gt (" + indexEnd + ")", $ ("ul"). hide ();
If (current = 1 ){
$ (This). attr ("class", "tabs-scroller-left-disabled ");
$ (This). attr ("disabled", "disabled ");
}
});
$ ("# BtnNext"). click (function (){
Debugger;
$ ("# BtnPrev"). attr ("class", "tabs-scroller-left ");
$ ("# BtnPrev"). removeAttr ("disabled ");
Current + = 1;
$ ("Ul li"). show ();
Var indexStart = (current-1 );
Var indexEnd = current + 4 >$ ("ul li"). length-1? $ ("Ul li"). length-1: current + 3;
$ ("Li: lt (" + indexStart + "), li: gt (" + indexEnd + ")", $ ("ul"). hide ();
If (current + 4> = totalPage ){
$ (This). attr ("class", "tabs-scroller-right-disabled ");
$ (This). attr ("disabled", "disabled ");
}
});
} Else {
Alert (1 );
$ ("# BtnPrev"). attr ("class", "tabs-scroller-left-disabled ");
$ ("# BtnNext"). attr ("class", "tabs-scroller-right-disabled ");
}
});
</Script>