loadData.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. mbui.define(['tool'], function (exports) {
  2. let tool = mbui.tool;
  3. //html转义,防止XSS
  4. function escapeHtml(text) {
  5. var map = {
  6. '&': '&',
  7. '<': '&lt;',
  8. '>': '&gt;',
  9. '"': '&quot;',
  10. "'": '&#039;'
  11. };
  12. return text.replace(/[&<>"']/g, function(m) { return map[m]; });
  13. }
  14. var LoadData = function () {
  15. this.config = {
  16. elem: "#listBox",
  17. seachBar: "#listSearch",
  18. url: "",
  19. where: {},
  20. limit: 10,
  21. scroll: 1,
  22. template: function (data) {
  23. return JSON.parse(data);
  24. }
  25. };
  26. this.loaded = 0;
  27. this.page = 1;
  28. this.count = 0;
  29. this.total = 0;
  30. };
  31. // 初始化
  32. LoadData.prototype.init = function (options) {
  33. var that = this;
  34. $.extend(true, that.config, options);
  35. var elem = $(that.config.elem);
  36. elem.html('<div class="load-data-container"></div><div class="load-data-none"><i class="iconfont icon-none"></i><br>暂无数据</div><div class="load-data-loading"><span>努力加载中</span></div><div class="load-data-end"><span>—————— 底 ● 线 ——————</span></div>');
  37. if(that.config.scroll==2){
  38. //容器监听滚动事件
  39. $('#root').scroll(function(){
  40. if ($(this).scrollTop() + $('#root').height() >= $('#app').height()-10) {
  41. // 滚动到页面底部时加载更多数据
  42. if (that.total < that.count && that.loaded == 0){
  43. that.ajax();
  44. }
  45. }
  46. });
  47. }
  48. else{
  49. // 页面监听滚动事件
  50. $(window).scroll(function () {
  51. if ($(window).scrollTop() + $(window).height() >= $(document).height()-10) {
  52. // 滚动到页面底部时加载更多数据
  53. if (that.total < that.count && that.loaded == 0){
  54. that.ajax();
  55. }
  56. }
  57. });
  58. }
  59. //回车搜索
  60. $(that.config.seachBar).on('keyup','[type="search"]',function(event){
  61. if (event.keyCode === 13) {
  62. $(this).blur();
  63. that.page=1;
  64. that.ajax();
  65. }
  66. })
  67. //清空时
  68. $(that.config.seachBar).on('input','[type="search"]', function() {
  69. var val = $(this).val();
  70. // 如果值为空,说明可能点击了 X 或者手动删除了
  71. if (val === '') {
  72. $(this).blur();
  73. that.page=1;
  74. that.ajax();
  75. }
  76. });
  77. //点击搜索重置按钮
  78. $(that.config.seachBar).on('click','.search-reset',function(){
  79. $(that.config.seachBar).find('input').val('');
  80. that.page=1;
  81. that.ajax();
  82. })
  83. that.ajax();
  84. };
  85. LoadData.prototype.reload = function () {
  86. var that = this;
  87. that.page=1;
  88. that.ajax();
  89. };
  90. LoadData.prototype.ajax = function () {
  91. var that = this;
  92. var elem = $(that.config.elem),seachBar = $(that.config.seachBar),map = {page:that.page,limit:that.config.limit};
  93. var keys = seachBar.find('.search-key');
  94. keys.each(function(){
  95. let key=$(this).attr('name');
  96. let val=$(this).val();
  97. if(val!=''){
  98. map[key]=val;
  99. }
  100. });
  101. var maps = $.extend({}, that.config.where, map);
  102. // 发送请求获取数据
  103. $.ajax({
  104. url: that.config.url,
  105. type: 'GET',
  106. data: maps,
  107. beforeSend: function () {
  108. // 显示加载按钮
  109. that.loaded = 1;
  110. elem.find('.load-data-loading').show();
  111. elem.find('.load-data-end').hide();
  112. },
  113. success: function (res) {
  114. if(res.count === 'undefined'){
  115. that.count=res.data.length;
  116. that.total=res.data.length;
  117. }
  118. else{
  119. that.count=res.count;
  120. that.total+=res.data.length;
  121. }
  122. if(that.page==1){
  123. elem.find('.load-data-container').html('');
  124. }
  125. if(that.count>=that.total && that.count>0){
  126. elem.find('.load-data-end').show();
  127. }
  128. elem.find('.load-data-none').addClass('load-data-'+that.count);
  129. if (res.data.length > 0) {
  130. that.page++;
  131. $.each(res.data, function (index, item) {
  132. // 转义JSON对象中的字符串值,防止XSS
  133. for (var key in item) {
  134. if (typeof item[key] === 'string') {
  135. item[key] = escapeHtml(item[key]);
  136. }
  137. }
  138. // 创建列表项并添加到列表中
  139. var listItem = that.config.template(item);
  140. elem.find('.load-data-container').append(listItem);
  141. });
  142. }
  143. },
  144. complete: function () {
  145. that.loaded = 0;
  146. elem.find('.load-data-loading').hide();
  147. }
  148. });
  149. }
  150. // 导出loadData模块
  151. exports('loadData', function (options) {
  152. var loadData = new LoadData();
  153. loadData.init(options);
  154. return loadData;
  155. });
  156. });