paging.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /**
  2. * Paging 组件
  3. * @description 基于laytpl 、laypage、layer 封装的组件
  4. *
  5. * @license MIT
  6. * @version 1.0.1
  7. */
  8. layui.define(['layer', 'laypage', 'laytpl'], function(exports) {
  9. "use strict";
  10. var $ = layui.jquery,
  11. layer = parent.layui.layer === undefined ? layui.layer : parent.layui.layer,
  12. laytpl = layui.laytpl;
  13. var Paging = function() {
  14. this.config = {
  15. url: undefined, //数据远程地址
  16. type: 'POST', //数据的获取方式 get or post
  17. elem: undefined, //内容容器
  18. params: {}, //获取数据时传递的额外参数
  19. openWait: false, //加载数据时是否显示等待框
  20. tempElem: undefined, //模板容器
  21. tempType: 0,
  22. paged: true,
  23. pageConfig: { //参数应该为object类型
  24. elem: undefined,
  25. pageSize: 15 //分页大小
  26. },
  27. success: undefined, //type:function
  28. fail: undefined, //type:function
  29. complate: undefined, //type:function
  30. serverError: function(xhr, status, error) { //ajax的服务错误
  31. throwError("错误提示: " + xhr.status + " " + xhr.statusText);
  32. }
  33. };
  34. };
  35. /**
  36. * 版本号
  37. */
  38. Paging.prototype.v = '1.0.2';
  39. /**
  40. * 设置
  41. * @param {Object} options
  42. */
  43. Paging.prototype.set = function(options) {
  44. var that = this;
  45. $.extend(true, that.config, options);
  46. return that;
  47. };
  48. /**
  49. * 初始化
  50. * @param {Object} options
  51. */
  52. Paging.prototype.init = function(options) {
  53. var that = this;
  54. $.extend(true, that.config, options);
  55. var _config = that.config;
  56. if(_config.url === undefined) {
  57. throwError('Paging Error:请配置远程URL!');
  58. }
  59. if(_config.elem === undefined) {
  60. throwError('Paging Error:请配置参数elem!');
  61. }
  62. if($(_config.elem).length === 0) {
  63. throwError('Paging Error:找不到配置的容器elem!');
  64. }
  65. if(_config.tempType === 0) {
  66. if(_config.tempElem === undefined) {
  67. throwError('Paging Error:请配置参数tempElem!');
  68. }
  69. if($(_config.tempElem).length === 0) {
  70. throwError('Paging Error:找不到配置的容器tempElem!');
  71. }
  72. }
  73. if(_config.paged) {
  74. var _pageConfig = _config.pageConfig;
  75. if(_pageConfig.elem === undefined) {
  76. throwError('Paging Error:请配置参数pageConfig.elem!');
  77. }
  78. }
  79. if(_config.type.toUpperCase() !== 'GET' && _config.type.toUpperCase() !== 'POST') {
  80. throwError('Paging Error:type参数配置出错,只支持GET或都POST');
  81. }
  82. that.get({
  83. pageIndex: 1,
  84. pageSize: _config.pageConfig.pageSize
  85. });
  86. return that;
  87. };
  88. /**
  89. * 获取数据
  90. * @param {Object} options
  91. */
  92. Paging.prototype.get = function(options) {
  93. var that = this;
  94. var _config = that.config;
  95. var loadIndex = undefined;
  96. if(_config.openWait) {
  97. loadIndex = layer.load(2);
  98. }
  99. //默认参数
  100. var df = {
  101. pageIndex: 1,
  102. pageSize: _config.pageConfig.pageSize
  103. };
  104. $.extend(true, _config.params, df, options);
  105. $.ajax({
  106. type: _config.type,
  107. url: _config.url,
  108. data: _config.params,
  109. dataType: 'json',
  110. success: function(result, status, xhr) {
  111. if(result.code === 0) {
  112. //获取模板
  113. var tpl = _config.tempType === 0 ? $(_config.tempElem).html(): _config.tempElem;
  114. //渲染数据
  115. laytpl(tpl).render(result, function(html) {
  116. $(_config.elem).html(html);
  117. });
  118. if(_config.paged) {
  119. if(result.count === null || result.count === 0) {
  120. throwError('Paging Error:请返回数据总数!');
  121. return;
  122. }
  123. var _pageConfig = _config.pageConfig;
  124. var pageSize = _pageConfig.pageSize;
  125. var pages = result.count % pageSize == 0 ?
  126. (result.count / pageSize) : (result.count / pageSize + 1);
  127. var defaults = {
  128. cont: $(_pageConfig.elem),
  129. curr: _config.params.pageIndex,
  130. pages: pages,
  131. jump: function(obj, first) {
  132. //得到了当前页,用于向服务端请求对应数据
  133. var curr = obj.curr;
  134. if(!first) {
  135. that.get({
  136. pageIndex: curr,
  137. pageSize: pageSize
  138. });
  139. }
  140. }
  141. };
  142. $.extend(defaults, _pageConfig); //参数合并
  143. layui.laypage(defaults);
  144. }
  145. if(_config.success) {
  146. _config.success(); //渲染成功
  147. }
  148. } else {
  149. if(_config.fail) {
  150. _config.fail(result.msg); //获取数据失败
  151. }
  152. }
  153. if(_config.complate) {
  154. _config.complate(); //渲染完成
  155. }
  156. if(loadIndex !== undefined)
  157. layer.close(loadIndex); //关闭等待层
  158. },
  159. error: function(xhr, status, error) {
  160. if(loadIndex !== undefined)
  161. layer.close(loadIndex); //关闭等待层
  162. _config.serverError(xhr, status, error); //服务器错误
  163. }
  164. });
  165. };
  166. /**
  167. * 抛出一个异常错误信息
  168. * @param {String} msg
  169. */
  170. function throwError(msg) {
  171. throw new Error(msg);
  172. };
  173. var paging = new Paging();
  174. exports('paging', function(options) {
  175. return paging.set(options);
  176. });
  177. });