永城市12345_前端 - 这个不用

mui.indexedlist.js 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /**
  2. * IndexedList
  3. * 类似联系人应用中的联系人列表,可以按首字母分组
  4. * 右侧的字母定位工具条,可以快速定位列表位置
  5. * varstion 1.0.0
  6. * by Houfeng
  7. * Houfeng@DCloud.io
  8. **/
  9. mui.ajax(huayi.config.callcenter_url + 'AddressBook/GetAppList?', {
  10. data: {
  11. token: localStorage.getItem('token'),
  12. },
  13. dataType: 'json', //服务器返回json格式数据
  14. type: 'get', //HTTP请求类型
  15. timeout: 10000, //超时时间设置为10秒;
  16. headers: {
  17. 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'
  18. },
  19. success: function(data) {
  20. if(data.state = "success") {
  21. var html
  22. $(data.rows).each(function(i, n) {
  23. html = '<li data-group="'+n.NameOneCode+'" class="mui-table-view-divider mui-indexed-list-group">'+n.NameOneCode+'</li>';
  24. $(n.AddressBooklist).each(function(j, m) {
  25. html += '<li data-value="'+m.NameOneCode+'" data-tags="'+m.NameOneCode+'" class="mui-table-view-cell mui-indexed-list-item">'+
  26. '<div class="mui-slider-cell">'+
  27. '<div class="oa-contact-cell mui-table">'+
  28. '<div class="oa-contact-avatar mui-table-cell">'+
  29. '<img src="img/2.png"/>'+
  30. '</div>'+
  31. '<div class="oa-contact-content mui-table-cell">'+
  32. '<h4 class="oa-contact-name size-14">'+m.F_Name+'</h4>'+
  33. '<span class="oa-contact-position mui-h6">'+m.F_Position+'</span>'+
  34. '<h4 class="oa-contact-email mui-h6">'+m.F_Mobile+'</h4>'+
  35. '</div>'+
  36. '</div>'+
  37. '</div>'+
  38. '</li>';
  39. })
  40. html += '</li>';
  41. $(html).appendTo($(".TX"));
  42. })
  43. var header = document.querySelector('header.mui-bar');
  44. var list = document.getElementById('list');
  45. //calc hieght
  46. list.style.height = (document.body.offsetHeight - header.offsetHeight) + 'px';
  47. //create
  48. window.indexedList = new mui.IndexedList(list);
  49. }
  50. },
  51. error: function(xhr, type, errorThrown) {
  52. //异常处理;
  53. mui.alert("");
  54. }
  55. });
  56. (function($, window, document) {
  57. var classSelector = function(name) {
  58. return '.' + $.className(name);
  59. }
  60. var IndexedList = $.IndexedList = $.Class.extend({
  61. /**
  62. * 通过 element 和 options 构造 IndexedList 实例
  63. **/
  64. init: function(holder, options) {
  65. var self = this;
  66. self.options = options || {};
  67. self.box = holder;
  68. if (!self.box) {
  69. throw "实例 IndexedList 时需要指定 element";
  70. }
  71. self.createDom();
  72. self.findElements();
  73. self.caleLayout();
  74. self.bindEvent();
  75. },
  76. createDom: function() {
  77. var self = this;
  78. self.el = self.el || {};
  79. //styleForSearch 用于搜索,此方式能在数据较多时获取很好的性能
  80. self.el.styleForSearch = document.createElement('style');
  81. (document.head || document.body).appendChild(self.el.styleForSearch);
  82. },
  83. findElements: function() {
  84. var self = this;
  85. self.el = self.el || {};
  86. self.el.search = self.box.querySelector(classSelector('indexed-list-search'));
  87. self.el.searchInput = self.box.querySelector(classSelector('indexed-list-search-input'));
  88. self.el.searchClear = self.box.querySelector(classSelector('indexed-list-search') + ' ' + classSelector('icon-clear'));
  89. self.el.bar = self.box.querySelector(classSelector('indexed-list-bar'));
  90. self.el.barItems = [].slice.call(self.box.querySelectorAll(classSelector('indexed-list-bar') + ' a'));
  91. self.el.inner = self.box.querySelector(classSelector('indexed-list-inner'));
  92. self.el.items = [].slice.call(self.box.querySelectorAll(classSelector('indexed-list-item')));
  93. self.el.liArray = [].slice.call(self.box.querySelectorAll(classSelector('indexed-list-inner') + ' li'));
  94. self.el.alert = self.box.querySelector(classSelector('indexed-list-alert'));
  95. },
  96. caleLayout: function() {
  97. var self = this;
  98. var withoutSearchHeight = (self.box.offsetHeight - self.el.search.offsetHeight) + 'px';
  99. self.el.bar.style.height = withoutSearchHeight;
  100. self.el.inner.style.height = withoutSearchHeight;
  101. var barItemHeight = ((self.el.bar.offsetHeight - 40) / self.el.barItems.length) + 'px';
  102. self.el.barItems.forEach(function(item) {
  103. item.style.height = barItemHeight;
  104. item.style.lineHeight = barItemHeight;
  105. });
  106. },
  107. scrollTo: function(group) {
  108. var self = this;
  109. var groupElement = self.el.inner.querySelector('[data-group="' + group + '"]');
  110. if (!groupElement || (self.hiddenGroups && self.hiddenGroups.indexOf(groupElement) > -1)) {
  111. return;
  112. }
  113. self.el.inner.scrollTop = groupElement.offsetTop;
  114. },
  115. bindBarEvent: function() {
  116. var self = this;
  117. var pointElement = null;
  118. var findStart = function(event) {
  119. if (pointElement) {
  120. pointElement.classList.remove('active');
  121. pointElement = null;
  122. }
  123. self.el.bar.classList.add('active');
  124. var point = event.changedTouches ? event.changedTouches[0] : event;
  125. pointElement = document.elementFromPoint(point.pageX, point.pageY);
  126. if (pointElement) {
  127. var group = pointElement.innerText;
  128. if (group && group.length == 1) {
  129. pointElement.classList.add('active');
  130. self.el.alert.innerText = group;
  131. self.el.alert.classList.add('active');
  132. self.scrollTo(group);
  133. }
  134. }
  135. event.preventDefault();
  136. };
  137. var findEnd = function(event) {
  138. self.el.alert.classList.remove('active');
  139. self.el.bar.classList.remove('active');
  140. if (pointElement) {
  141. pointElement.classList.remove('active');
  142. pointElement = null;
  143. }
  144. };
  145. self.el.bar.addEventListener($.EVENT_MOVE, function(event) {
  146. findStart(event);
  147. }, false);
  148. self.el.bar.addEventListener($.EVENT_START, function(event) {
  149. findStart(event);
  150. }, false);
  151. document.body.addEventListener($.EVENT_END, function(event) {
  152. findEnd(event);
  153. }, false);
  154. document.body.addEventListener($.EVENT_CANCEL, function(event) {
  155. findEnd(event);
  156. }, false);
  157. },
  158. search: function(keyword) {
  159. var self = this;
  160. keyword = (keyword || '').toLowerCase();
  161. var selectorBuffer = [];
  162. var groupIndex = -1;
  163. var itemCount = 0;
  164. var liArray = self.el.liArray;
  165. var itemTotal = liArray.length;
  166. self.hiddenGroups = [];
  167. var checkGroup = function(currentIndex, last) {
  168. if (itemCount >= currentIndex - groupIndex - (last ? 0 : 1)) {
  169. selectorBuffer.push(classSelector('indexed-list-inner li') + ':nth-child(' + (groupIndex + 1) + ')');
  170. self.hiddenGroups.push(liArray[groupIndex]);
  171. };
  172. groupIndex = currentIndex;
  173. itemCount = 0;
  174. }
  175. liArray.forEach(function(item) {
  176. var currentIndex = liArray.indexOf(item);
  177. if (item.classList.contains($.className('indexed-list-group'))) {
  178. checkGroup(currentIndex, false);
  179. } else {
  180. var text = (item.innerText || '').toLowerCase();
  181. var value = (item.getAttribute('data-value') || '').toLowerCase();
  182. var tags = (item.getAttribute('data-tags') || '').toLowerCase();
  183. if (keyword && text.indexOf(keyword) < 0 &&
  184. value.indexOf(keyword) < 0 &&
  185. tags.indexOf(keyword) < 0) {
  186. selectorBuffer.push(classSelector('indexed-list-inner li') + ':nth-child(' + (currentIndex + 1) + ')');
  187. itemCount++;
  188. }
  189. if (currentIndex >= itemTotal - 1) {
  190. checkGroup(currentIndex, true);
  191. }
  192. }
  193. });
  194. if (selectorBuffer.length >= itemTotal) {
  195. self.el.inner.classList.add('empty');
  196. } else if (selectorBuffer.length > 0) {
  197. self.el.inner.classList.remove('empty');
  198. self.el.styleForSearch.innerText = selectorBuffer.join(', ') + "{display:none;}";
  199. } else {
  200. self.el.inner.classList.remove('empty');
  201. self.el.styleForSearch.innerText = "";
  202. }
  203. },
  204. bindSearchEvent: function() {
  205. var self = this;
  206. self.el.searchInput.addEventListener('input', function() {
  207. var keyword = this.value;
  208. self.search(keyword);
  209. }, false);
  210. $(self.el.search).on('tap', classSelector('icon-clear'), function() {
  211. self.search('');
  212. }, false);
  213. },
  214. bindEvent: function() {
  215. var self = this;
  216. self.bindBarEvent();
  217. self.bindSearchEvent();
  218. }
  219. });
  220. //mui(selector).indexedList 方式
  221. $.fn.indexedList = function(options) {
  222. //遍历选择的元素
  223. this.each(function(i, element) {
  224. if (element.indexedList) return;
  225. element.indexedList = new IndexedList(element, options);
  226. });
  227. return this[0] ? this[0].indexedList : null;
  228. };
  229. })(mui, window, document);