<script type="text/javascript"> $(document).ready(function(){ ajaxGetPaging(1); }); function ajaxGetPaging(page_ing) { $.ajax({ type: "POST", url: "/User?action=paging&page="+page_ing, data:{ Sort:$("#Hd_Sort").val(), Search:$("#tcSearch").val() }, dataType: "text", success: function(data){ //alert(data); $("#paging_top").html("页码: "+data); $("#paging_bottom").html("页码: "+data); ajaxGetUserInfo(page_ing); }, error: function(){ alert("Request data error!"); } }); } function deleteInfo(uname,uid,page_ing) { zeroModal.confirm("<span style="color:#ff6666">删除</span> "+uname, function() { $.ajax({ type: "POST", url: "/User?action=delete&page="+page_ing+"&id="+uid, dataType: "text", success: function(data){ ajaxGetPaging(data); }, error: function(){ alert("Request data error!"); } }); }) } function ajaxSort(sort) { $("#Hd_Sort").val(sort); ajaxGetPaging(1); //alert($("#Hd_Sort").val()); } function ajaxSearch() { ajaxGetPaging(1); } </script>
import Option.DBUtil; import com.google.gson.Gson; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; public class Function { /*取得数据库记录数*/ public static int getRecordCount ( String tableName, String terms, String search) { int rowCount = 0; try { Connection conn = DBUtil.getConnection(); Statement statement = conn.createStatement(); ResultSet rset = statement.executeQuery("select count(*) totalCount from "+tableName+" "+search+" ORDER BY "+terms+" DESC"); if (rset.next()) { rowCount = rset.getInt("totalCount"); } } catch (Exception e) { e.printStackTrace(); } return rowCount; } //页码总数 public static int GetPageCount(int paging,String tableName, String terms, String search) { int _page = getRecordCount(tableName,terms,search) / paging; if (_page < ((double)getRecordCount(tableName,terms,search) / (double)paging)) _page += 1; if (_page<1) _page =1; return _page; } public static String[] Paging (int fPage_Ing,String tableName,int fPage_Pading,String terms,String search) { int fPage_Count; //总页数 String pageStr = ""; fPage_Count = GetPageCount(fPage_Pading,tableName,terms,search); if (fPage_Ing>fPage_Count) fPage_Ing=fPage_Count; int p_begin = 0, p_end = 10; boolean view_first, view_last; if (fPage_Count <= 10) { p_begin = 0; p_end = fPage_Count; view_first = false; view_last = false; } else { p_begin = (fPage_Ing - 1) - 5; if (p_begin <= 0) { p_begin = 0; p_end = 10; view_first = false; view_last = true; } else { p_end = p_begin + 10; if (p_end >= fPage_Count) { p_begin = fPage_Count - 10; p_end = fPage_Count; view_first = true; view_last = false; } else { p_end = p_begin + 10; view_first = true; view_last = true; } } } for (int i = p_begin; i < p_end; i++) { if ((i+1)==fPage_Ing) { pageStr += "[<button style="outline: none;color:#ffff00;font-size: 1.2rem;border:0px;background:transparent;cursor: pointer">" + (i + 1) + "</button>] "; } else { pageStr += "[<button onclick="ajaxGetPaging(" + (i+1) + ")" style="outline: none;color:#fff;font-size: 0.85rem;border:0px;background:transparent;cursor: pointer">" + (i + 1) + "</button>] "; } } if (view_first) { pageStr = "[<button onclick="ajaxGetPaging(1)" style="outline: none;color:#fff;font-size: 0.85rem;border:0px;background:transparent;cursor: pointer">"+ "1</button>] "+" ... " +pageStr; } if (view_last) { pageStr +=" ... [<button onclick="ajaxGetPaging("+fPage_Count+")" style="outline: none;color:#fff;font-size: 0.85rem;border:0px;background:transparent;cursor: pointer">"+ fPage_Count+"</button>] "; } pageStr += " "; String[] strArr = new String[2]; strArr[0] = pageStr; //返回第一个值 strArr[1] = fPage_Ing+"";//返回第一个值 return strArr; } /* 取得用户信息,返回JSON字符串*/ public static String getUserData (int begin, String terms,String search) { String user_JSON = ""; int tol = getRecordCount("teach_user",terms,search); try { Connection conn = DBUtil.getConnection(); Statement statement = conn.createStatement(); String sql = "select * from teach_user "+search+" ORDER BY "+terms+" DESC"; ResultSet rs = statement.executeQuery(sql); int t = 0; String group=""; while (rs.next()) { if (t>=((begin-1)*12) && t<begin*12) { if (rs.getString("USER_Group").equals("0")) group = "积分用户组"; else group = "管理用户组"; user_JSON += "{"uid":"" +rs.getInt("uid")+"","+ ""USER_Nic":""+rs.getString("USER_Name")+"","+ ""USER_Group":""+group+"","+ ""USER_Name":""+rs.getString("USER_Name")+ " ( uid: "+rs.getString("USER_ID")+" )","+ ""USER_RegTime":""+rs.getString("USER_RegTime")+""}"; } t++; if (t==begin*12 || t==tol) break; if (t>(begin-1)*12) user_JSON += ","; } rs.close(); } catch (Exception e) { e.printStackTrace(); } user_JSON = "["+user_JSON+"]"; return user_JSON; } }
稍加修改,java、asp.net都可用,原理相同,c/s、b/s都一样原理,不过显示方式不同而已。