效果图

css样式

  1. <style>
  2. html,body{margin: 0;padding:0}
  3. a:focus {outline: none;}
  4. /* 通用表格显示 */
  5. table, th, td {font: 12px Arial,Helvetica,sans-serif,'宋体';margin: 0;padding: 0}
  6. table{border-spacing: 0;border-collapse: collapse;}
  7. .datatable {width: 100%;border-style: none;background-color: #fff;margin-bottom: 20px;text-align: left;}
  8. .datatable th, .datatable td { padding: 5px;line-height: 30px}
  9. .datatable thead th {background-color: #eee;margin: 0;text-align: left;border-top: 1px solid #cfcfcf;border-bottom: 1px solid #cfcfcf;font-weight: 500}
  10. .datatable tbody td {background-color: #fff;border-bottom: 1px solid #ddd;table-layout:fixed;word-break:break-all;font-weight: 400}
  11. .datatable tbody tr.evenrow td {background-color: #f4f4f4;}
  12. .datatable tfoot td {background-color: #fafafa;text-align: right;border-bottom: 1px solid #cfcfcf;}
  13. /*表格分页列表*/
  14. .datatable td.paging a {border: 1px solid #eee; color: #444; margin: 4px; padding: 2px 7px; text-decoration: none; text-align:center;}
  15. /*表格分页当前页*/
  16. .datatable td.paging a.current {background: #eee; border: 1px solid #CFCFCF; color: #444; font-weight: bold;}
  17. .datatable td.paging a.current{border: 0;cursor: auto;background:none}
  18. </style>

html结构

  1. <table id="cs_table" class="datatable"></table>

javascript封装代码

  1. <script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
  2. <script>
  3. /**
  4. * 抽象化表格
  5. */
  6. function abstractTable(){
  7. // ---------内容属性
  8. this.id = null; // 每个表格都有唯一的一个id
  9. this.tableobj = null; //表格对象
  10. this.rowNum = 0; //行数
  11. this.colNum = 0; //列数
  12. this.header = []; //表头数据
  13. this.content = []; //body数据
  14. // ----------提供外部使用获得表格内部数据
  15. this.currentClickRowID = 0; //当前点击的行数据
  16. // --- 通过表头来获得这张表的列数
  17. this.getColNum = function(){
  18. this.colNum = this.header.length;
  19. return this.colNum;
  20. }
  21. // ----------- 表格自我构建行为
  22. this.clearTable = function(){};
  23. this.showHeader = function(){};
  24. this.showContent = function(begin,end){};
  25. this.showFoot = function(){};
  26.  
  27. // --------- 分页功能属性
  28. this.allDataNum = 0; // 总数据条数
  29. this.displayNum = 10; // 每页显示条数
  30. this.maxPageNum = 0; // 最大页码值
  31. this.currentPageNum =1;// 当前页码值
  32. //tfoot分页组
  33. this.groupDataNum = 10; //每组显示10页
  34. this.groupNum = 1; //当前组
  35.  
  36. // -------- 分页功能行为
  37. this.paginationFromBeginToEnd = function(begin,end){}
  38. this.first = function(){}//首页
  39. this.last = function(){}//最后一页
  40. this.prev = function(){}//上一页
  41. this.next = function(){}//下一页
  42. this.goto = function(){} //跳到某页
  43.  
  44. // ----------- 表格初始化
  45. this.init = function(begin,end){}
  46.  
  47. }
  48.  
  49. /*
  50. 表格对象模板
  51. */
  52. function tableTemplet(table_id){
  53. abstractTable.call(this);
  54. this.id = table_id;
  55.  
  56. }
  57. /**
  58. * 表格对象
  59. * @param options
  60. */
  61. function table(options){
  62. if(!options){return;}
  63. if(!$.isPlainObject(options)){return;}
  64.  
  65. tableTemplet.call(this,options.tableId);
  66. //得到表格对象
  67. this.tableobj = $("#"+this.id);
  68. //清空表格内容
  69. this.clearTable = function(){
  70. this.tableobj.html(" ");
  71. }
  72. // 实现分页行为
  73. this.paginationFromBeginToEnd= function(x,y){
  74. this.maxPageNum = Math.ceil(this.allDataNum/this.displayNum);
  75. var arrPage = [];
  76. for(var i= x;i<y;i++){
  77. arrPage.push(this.content[i]);
  78. }
  79. return arrPage;
  80. }
  81.  
  82. this.showHeader = function(){
  83. if(this.header != null){
  84. var $thead = $("<thead>"),
  85. $tr = $("<tr>"),
  86. $th;
  87. for(var i=0;i<this.colNum;i++){
  88. $th = $("<th>").html(this.header[i]);
  89. $th.appendTo($tr);
  90. }
  91. $tr.appendTo($thead);
  92. $thead.appendTo(this.tableobj)
  93. }
  94. }
  95. //初始化tbody
  96. this.showContent = function(begin,end){
  97. if(this.content != null){
  98. var $tbody = $("<tbody>"),
  99. $tr,
  100. $td;
  101. var tempDaTa = this.paginationFromBeginToEnd(begin,end),
  102. len = tempDaTa.length;
  103. // 循环创建行
  104. for(var i=0;i<len;i++){
  105. $tr = $("<tr>").appendTo($tbody);
  106. if(i%2==1){
  107. $tr.addClass("evenrow");
  108. }
  109. // 循环创建列 取得对象中的键
  110. for(var key in tempDaTa[i]){
  111. $td = $("<td>").html(tempDaTa[i][key]).appendTo($tr);
  112. }
  113. }
  114. this.tableobj.append($tbody);
  115. }
  116.  
  117. }
  118. //初始化tfoot
  119. this.showFoot = function(){
  120. var $tfoot = $("<tfoot>"),
  121. $tr = $("<tr>"),
  122. $td = $("<td>").attr("colspan",this.colNum).addClass("paging");
  123. $tr.append($td);
  124. $tfoot.append($tr);
  125. this.tableobj.append($tfoot);
  126. this.pagination($td);
  127. }
  128. //表格分页
  129. this.pagination = function(tdCell){
  130. var $td= typeof(tdCell) == "object" ? tdCell : $("#" + tdCell);
  131. //首页
  132. var oA = $("<a/>");
  133. oA.attr("href","#1");
  134. oA.html("首页");
  135. $td.append(oA);
  136. //上一页
  137. if(this.currentPageNum>=2){
  138. var oA = $("<a/>");
  139. oA.attr("href","#"+(this.currentPageNum - 1));
  140. oA.html("上一页");
  141. $td.append(oA);
  142. }
  143. //普通显示格式
  144. if(this.maxPageNum <= this.groupDataNum){ // 10页以内 为一组
  145. for(var i = 1;i <= this.maxPageNum ;i++){
  146. var oA = $("<a/>");
  147. oA.attr("href","#"+i);
  148. if(this.currentPageNum == i){
  149. oA.attr("class","current");
  150. }
  151. oA.html(i);
  152. $td.append(oA);
  153. }
  154. }else{//超过10页以后(也就是第一组后)
  155. if(this.groupNum<=1){//第一组显示
  156. for(var j = 1;j <= this.groupDataNum ;j++){
  157. var oA = $("<a/>");
  158. oA.attr("href","#"+j);
  159. if(this.currentPageNum == j){
  160. oA.attr("class","current");
  161. }
  162. oA.html(j);
  163. $td.append(oA);
  164.  
  165. }
  166. }else{//第二组后面的显示
  167. var begin = (this.groupDataNum*(this.groupNum-1))+ 1,
  168. end ,
  169. maxGroupNum = Math.ceil(this.maxPageNum/this.groupDataNum);
  170. if(this.maxPageNum%this.groupDataNum!=0&&this.groupNum==maxGroupNum){
  171. end = this.groupDataNum*(this.groupNum-1)+this.maxPageNum%this.groupDataNum
  172. }else{
  173. end = this.groupDataNum*(this.groupNum);
  174. }
  175.  
  176. for(var j = begin;j <= end ;j++){
  177. var oA = $("<a/>");
  178. oA.attr("href","#"+j);
  179. if(this.currentPageNum == j){
  180. oA.attr("class","current");
  181. }
  182. oA.html(j);
  183. $td.append(oA);
  184. }
  185. }
  186. }
  187. //下一页
  188. if( (this.maxPageNum - this.currentPageNum) >= 1 ){
  189. var oA = $("<a/>");
  190. oA.attr("href","#" + (this.currentPageNum + 1));
  191. oA.html("下一页");
  192. $td.append(oA);
  193. }
  194. //尾页
  195. var oA = $("<a/>");
  196. oA.attr("href","#" + this.maxPageNum);
  197. oA.html("尾页");
  198. $td.append(oA);
  199.  
  200. var page_a = $td.find('a');
  201. var tempThis = this;
  202.  
  203. page_a.unbind("click").bind("click",function(){
  204. var nowNum = parseInt($(this).attr('href').substring(1));
  205.  
  206. if(nowNum>tempThis.currentPageNum){//下一组
  207. if(tempThis.currentPageNum%tempThis.groupDataNum==0){
  208. tempThis.groupNum += 1;
  209.  
  210. var maxGroupNum = Math.ceil(tempThis.maxPageNum/tempThis.groupDataNum);
  211. if(tempThis.groupNum>=maxGroupNum){
  212. tempThis.groupNum = maxGroupNum;
  213. }
  214. }
  215. }
  216. if(nowNum<tempThis.currentPageNum){//上一组
  217. if((tempThis.currentPageNum-1)%tempThis.groupDataNum==0){
  218. tempThis.groupNum -= 1;
  219. if(tempThis.groupNum<=1){
  220. tempThis.groupNum = 1;
  221. }
  222. }
  223. }
  224. if(nowNum==tempThis.maxPageNum){//直接点击尾页
  225. var maxGroupNum = Math.ceil(tempThis.maxPageNum/tempThis.groupDataNum);
  226. tempThis.groupNum = maxGroupNum;
  227. }
  228. if(nowNum==1){
  229. var maxGroupNum = Math.ceil(tempThis.maxPageNum/tempThis.groupDataNum);
  230. tempThis.groupNum = 1;
  231. }
  232. tempThis.currentPageNum = nowNum;
  233.  
  234. tempThis.init((tempThis.currentPageNum-1)*tempThis.displayNum,
  235. tempThis.currentPageNum*tempThis.displayNum);
  236. return false;
  237. });
  238. }
  239. //初始化
  240. this.init = function(begin,end){
  241. this.header = options.headers;
  242. this.colNum = this.header.length;
  243. this.content = options.data;
  244. this.allDataNum = this.content.length;
  245. if(options.displayNum){
  246. this.displayNum = options.displayNum;
  247. }
  248. if(options.groupDataNum){
  249. this.groupDataNum = options.groupDataNum;
  250. }
  251. this.clearTable();
  252. this.showHeader();
  253. this.showContent(begin,end);
  254. this.showFoot();
  255. }
  256.  
  257. this.init(0,options.displayNum);
  258. }
  259. </script>

调用方式

  1. <script type="text/javascript">
  2. var data = [];
  3. for(var i=0;i<334;i++){
  4. data[i] = {id:i+1,name:"jason"+(i+1),gender:"男",age:26,address:"成都"};
  5. }
  6. var cs = new table({
  7. "tableId":"cs_table", //必须
  8. "headers":["序号","姓名","性别","年龄","地址"], //必须
  9. "data":data, //必须
  10. "displayNum": 6, //必须 默认 10
  11. "groupDataNum":9 //可选 默认 10
  12. });
  13. </script>

【转】基于jquery的无刷新表格分页的更多相关文章

  1. jQuery.pager无刷新分页

    刚刚学习前端的时候,需要一个无刷新的分页功能,找了一个不错的,大家也有很大分享,在这里写一个自己的部分代码,前后端都有,需要的小伙伴可以参考一下,代码不是完整的. 直接上伪代码<样式代码省略,部 ...

  2. 基于Jquery+Ajax+Json+存储过程 高效分页

    在做后台开发中,都会有大量的列表展示,下面给大家给大家分享一套基于Jquery+Ajax+Json+存储过程高效分页列表,只需要传递几个参数即可.当然代码也有改进的地方,如果大家有更好的方法,愿留下宝 ...

  3. angular -- 无刷新做分页

    无刷新做分页参考地址: http://www.jq22.com/demo/angular201707111100/ 示例代码: <!DOCTYPE html> <html lang= ...

  4. 基于JQuery可拖动列表格插件DataTables的踩坑记

    前言 最近项目中在使用能够拖动列调整列位置顺序的表格插件---DataTables,这也是目前我找到的唯一一种存在有这种功能的插件. 在查找使用方法的过程中发现可用案例并不多,且大多言语不详.本文将全 ...

  5. JQUERY AJAX无刷新异步上传文件

    AJAX无刷新上传文件并显示 http://blog.csdn.net/gao3705512/article/details/9330637?utm_source=tuicool jQuery For ...

  6. JQuery实现无刷新下拉加载图片

          最近做的一个项目需要做页面无刷新下拉加载图片,调研了一番,大多都采用检测滚动条达到底部,然后利用ajax加载下一页数据对页面数据进行添加,根据这一逻辑,自己写了一个,具体代码如下: JQu ...

  7. jQuery Ajax无刷新操作

    下面是“无刷新登录”的例子,采用Ashx+jQuery Ajax实现. //后台实例代码 ashx文件(可替换为从数据库中读取) public void ProcessRequest(HttpCont ...

  8. jQuery实现无刷新切换主题皮肤功能

    主题皮肤切换功能在很多网站和系统中应用,用户可以根据此功能设置自己喜欢的主题颜色风格,增强了用户体验.本文将围绕如何使用jQuery实现点击无刷新切换主题皮肤功能. 查看演示DEMO:https:// ...

  9. jquery.axios无刷新机制删除

    思路:无刷新机制就是不用的刷新动作 ,用前端html语法删除和后端的数据库删,同时删除达到效果 除操作,来实现无刷洗的方法

随机推荐

  1. c/c++关于内存分配的知识(非常详细的比较,且VirtualAlloc分配内直接在进程的地址空间中保留一快内存)

    一个由c/C++编译的程序占用的内存分为以下几个部分 1.栈区(stack)— 由编译器自动分配释放 ,存放函数的参数值,局部变量的值等.其操作方式类似于数据结构中的栈. 2.堆区(heap) — 一 ...

  2. HDU4666 Hyperspace(曼哈顿)

    题目链接. 分析: 这是多校的一个题,当时没做出来.学长说让用multiset. 用multiset将每一个数的1<<dim个状态全部保存.假设状态 i, 最远曼哈顿距离应当是 max[i ...

  3. HBase HFile

    HFile index HFile index, which is proportional to the total number of Data Blocks. The total amount ...

  4. datagridview的数据源的操作

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  5. LeetCode——Restore IP Addresses

    Given a string containing only digits, restore it by returning all possible valid IP address combina ...

  6. C#获取字符串生成图片后的长度

    1.    使用g.MeasureString()获得 使用MeasureString测量出来的字符宽度,总是比实际宽度大一些,而且随着字符的长度增大,貌似实际宽度和测量宽度的差距也越来越大了.查了一 ...

  7. int.Parse()与int.TryParse()

      int i = -1;bool b = int.TryParse(null, out i);执行完毕后,b等于false,i等于0,而不是等于-1,切记. int i = -1;bool b = ...

  8. 【转】使用 NuGet 管理项目库-Phil Haack

    原文地址:https://msdn.microsoft.com/zh-cn/magazine/hh547106.aspx 无论多么努力,Microsoft 也没办法提供开发人员所需要的每一个库. 虽然 ...

  9. java—— 编译与运行

    内容:使用javac 指定编译多个目录下java文件 链接:http://zhidao.baidu.com/link?url=W5ZERu8_ouGD-L_JH0vqqawhJNitsGbonQAAT ...

  10. 032数值的整数次方(keep it up)

    剑指offer中题目:http://ac.jobdu.com/problem.php? pid=1514 题目描写叙述: 给定一个double类型的浮点数base和int类型的整数exponent. ...