uni-indexed-list.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. <template>
  2. <view class="uni-indexed-list" ref="list" id="list">
  3. <!-- #ifdef APP-NVUE -->
  4. <list class="uni-indexed-list__scroll" scrollable="true" show-scrollbar="false">
  5. <cell v-for="(list, idx) in lists" :key="idx" :ref="'uni-indexed-list-' + idx">
  6. <!-- #endif -->
  7. <!-- #ifndef APP-NVUE -->
  8. <scroll-view :scroll-into-view="scrollViewId" class="uni-indexed-list__scroll" scroll-y>
  9. <view v-for="(list, idx) in lists" :key="idx" :id="'uni-indexed-list-' + idx">
  10. <!-- #endif -->
  11. <indexed-list-item :list="list" :loaded="loaded" :idx="idx" :showSelect="showSelect" @itemClick="onClick"></indexed-list-item>
  12. <!-- #ifndef APP-NVUE -->
  13. </view>
  14. </scroll-view>
  15. <!-- #endif -->
  16. <!-- #ifdef APP-NVUE -->
  17. </cell>
  18. </list>
  19. <!-- #endif -->
  20. <view class="uni-indexed-list__menu" :class="touchmove ? 'uni-indexed-list__menu--active' : ''" @touchstart="touchStart"
  21. @touchmove.stop.prevent="touchMove" @touchend="touchEnd" @mousedown.stop="mousedown" @mousemove.stop.prevent="mousemove"
  22. @mouseleave.stop="mouseleave">
  23. <view v-for="(list, key) in lists" :key="key" class="uni-indexed-list__menu-item">
  24. <text class="uni-indexed-list__menu-text" :class="touchmoveIndex == key ? 'uni-indexed-list__menu-text--active' : ''">{{ list.key }}</text>
  25. </view>
  26. </view>
  27. <view v-if="touchmove" class="uni-indexed-list__alert-wrapper">
  28. <text class="uni-indexed-list__alert">{{ lists[touchmoveIndex].key }}</text>
  29. </view>
  30. </view>
  31. </template>
  32. <script>
  33. import indexedListItem from './uni-indexed-list-item.vue'
  34. // #ifdef APP-NVUE
  35. const dom = weex.requireModule('dom');
  36. // #endif
  37. // #ifdef APP-PLUS
  38. function throttle(func, delay) {
  39. var prev = Date.now();
  40. return function() {
  41. var context = this;
  42. var args = arguments;
  43. var now = Date.now();
  44. if (now - prev >= delay) {
  45. func.apply(context, args);
  46. prev = Date.now();
  47. }
  48. }
  49. }
  50. function touchMove(e) {
  51. let pageY = e.touches[0].pageY
  52. let index = Math.floor((pageY - this.winOffsetY) / this.itemHeight)
  53. if (this.touchmoveIndex === index) {
  54. return false
  55. }
  56. let item = this.lists[index]
  57. if (item) {
  58. // #ifndef APP-NVUE
  59. this.scrollViewId = 'uni-indexed-list-' + index
  60. this.touchmoveIndex = index
  61. // #endif
  62. // #ifdef APP-NVUE
  63. dom.scrollToElement(this.$refs['uni-indexed-list-' + index][0], {
  64. animated: false
  65. })
  66. this.touchmoveIndex = index
  67. // #endif
  68. }
  69. }
  70. const throttleTouchMove = throttle(touchMove, 40)
  71. // #endif
  72. /**
  73. * IndexedList 索引列表
  74. * @description 用于展示索引列表
  75. * @tutorial https://ext.dcloud.net.cn/plugin?id=375
  76. * @property {Boolean} showSelect = [true|false] 展示模式
  77. * @value true 展示模式
  78. * @value false 选择模式
  79. * @property {Object} options 索引列表需要的数据对象
  80. * @event {Function} click 点击列表事件 ,返回当前选择项的事件对象
  81. * @example <uni-indexed-list options="" showSelect="false" @click=""></uni-indexed-list>
  82. */
  83. export default {
  84. name: 'UniIndexedList',
  85. components: {
  86. indexedListItem
  87. },
  88. props: {
  89. options: {
  90. type: Array,
  91. default () {
  92. return []
  93. }
  94. },
  95. showSelect: {
  96. type: Boolean,
  97. default: false
  98. }
  99. },
  100. data() {
  101. return {
  102. lists: [],
  103. winHeight: 0,
  104. itemHeight: 0,
  105. winOffsetY: 0,
  106. touchmove: false,
  107. touchmoveIndex: -1,
  108. scrollViewId: '',
  109. touchmovable: true,
  110. loaded: false,
  111. isPC: false
  112. }
  113. },
  114. watch: {
  115. options: {
  116. handler: function() {
  117. this.setList()
  118. },
  119. deep: true
  120. }
  121. },
  122. mounted() {
  123. // #ifdef H5
  124. this.isPC = this.IsPC()
  125. // #endif
  126. setTimeout(() => {
  127. this.setList()
  128. }, 50)
  129. setTimeout(() => {
  130. this.loaded = true
  131. }, 300);
  132. },
  133. methods: {
  134. setList() {
  135. let index = 0;
  136. this.lists = []
  137. this.options.forEach((value, index) => {
  138. if (value.data.length === 0) {
  139. return
  140. }
  141. let indexBefore = index
  142. let items = value.data.map(item => {
  143. let obj = {}
  144. obj['key'] = value.letter
  145. obj['name'] = item
  146. obj['itemIndex'] = index
  147. index++
  148. obj.checked = item.checked ? item.checked : false
  149. return obj
  150. })
  151. this.lists.push({
  152. title: value.letter,
  153. key: value.letter,
  154. items: items,
  155. itemIndex: indexBefore
  156. })
  157. })
  158. // #ifndef APP-NVUE
  159. uni.createSelectorQuery()
  160. .in(this)
  161. .select('#list')
  162. .boundingClientRect()
  163. .exec(ret => {
  164. this.winOffsetY = ret[0].top
  165. this.winHeight = ret[0].height
  166. this.itemHeight = this.winHeight / this.lists.length
  167. })
  168. // #endif
  169. // #ifdef APP-NVUE
  170. dom.getComponentRect(this.$refs['list'], (res) => {
  171. this.winOffsetY = res.size.top
  172. this.winHeight = res.size.height
  173. this.itemHeight = this.winHeight / this.lists.length
  174. })
  175. // #endif
  176. },
  177. touchStart(e) {
  178. this.touchmove = true
  179. let pageY = this.isPC ? e.pageY : e.touches[0].pageY
  180. let index = Math.floor((pageY - this.winOffsetY) / this.itemHeight)
  181. let item = this.lists[index]
  182. if (item) {
  183. this.scrollViewId = 'uni-indexed-list-' + index
  184. this.touchmoveIndex = index
  185. // #ifdef APP-NVUE
  186. dom.scrollToElement(this.$refs['uni-indexed-list-' + index][0], {
  187. animated: false
  188. })
  189. // #endif
  190. }
  191. },
  192. touchMove(e) {
  193. // #ifndef APP-PLUS
  194. let pageY = this.isPC ? e.pageY : e.touches[0].pageY
  195. let index = Math.floor((pageY - this.winOffsetY) / this.itemHeight)
  196. if (this.touchmoveIndex === index) {
  197. return false
  198. }
  199. let item = this.lists[index]
  200. if (item) {
  201. this.scrollViewId = 'uni-indexed-list-' + index
  202. this.touchmoveIndex = index
  203. }
  204. // #endif
  205. // #ifdef APP-PLUS
  206. throttleTouchMove.call(this, e)
  207. // #endif
  208. },
  209. touchEnd() {
  210. this.touchmove = false
  211. this.touchmoveIndex = -1
  212. },
  213. /**
  214. * 兼容 PC @tian
  215. */
  216. mousedown(e) {
  217. if (!this.isPC) return
  218. this.touchStart(e)
  219. },
  220. mousemove(e) {
  221. if (!this.isPC) return
  222. this.touchMove(e)
  223. },
  224. mouseleave(e) {
  225. if (!this.isPC) return
  226. this.touchEnd(e)
  227. },
  228. // #ifdef H5
  229. IsPC() {
  230. var userAgentInfo = navigator.userAgent;
  231. var Agents = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"];
  232. var flag = true;
  233. for (let v = 0; v < Agents.length - 1; v++) {
  234. if (userAgentInfo.indexOf(Agents[v]) > 0) {
  235. flag = false;
  236. break;
  237. }
  238. }
  239. return flag;
  240. },
  241. // #endif
  242. onClick(e) {
  243. let {
  244. idx,
  245. index
  246. } = e
  247. let obj = {}
  248. for (let key in this.lists[idx].items[index]) {
  249. obj[key] = this.lists[idx].items[index][key]
  250. }
  251. let select = []
  252. if (this.showSelect) {
  253. this.lists[idx].items[index].checked = !this.lists[idx].items[index].checked
  254. this.lists.forEach((value, idx) => {
  255. value.items.forEach((item, index) => {
  256. if (item.checked) {
  257. let obj = {}
  258. for (let key in this.lists[idx].items[index]) {
  259. obj[key] = this.lists[idx].items[index][key]
  260. }
  261. select.push(obj)
  262. }
  263. })
  264. })
  265. }
  266. this.$emit('click', {
  267. item: obj,
  268. select: select
  269. })
  270. }
  271. }
  272. }
  273. </script>
  274. <style lang="scss" scoped>
  275. .uni-indexed-list {
  276. position: absolute;
  277. left: 0;
  278. top: 0;
  279. right: 0;
  280. bottom: 0;
  281. /* #ifndef APP-NVUE */
  282. display: flex;
  283. /* #endif */
  284. flex-direction: row;
  285. }
  286. .uni-indexed-list__scroll {
  287. flex: 1;
  288. }
  289. .uni-indexed-list__menu {
  290. width: 24px;
  291. background-color: lightgrey;
  292. /* #ifndef APP-NVUE */
  293. display: flex;
  294. /* #endif */
  295. flex-direction: column;
  296. }
  297. .uni-indexed-list__menu-item {
  298. /* #ifndef APP-NVUE */
  299. display: flex;
  300. /* #endif */
  301. flex: 1;
  302. align-items: center;
  303. justify-content: center;
  304. /* #ifdef H5 */
  305. cursor: pointer;
  306. /* #endif */
  307. }
  308. .uni-indexed-list__menu-text {
  309. line-height: 20px;
  310. font-size: 12px;
  311. text-align: center;
  312. color: #aaa;
  313. }
  314. .uni-indexed-list__menu--active {
  315. background-color: rgb(200, 200, 200);
  316. }
  317. .uni-indexed-list__menu-text--active {
  318. color: #007aff;
  319. }
  320. .uni-indexed-list__alert-wrapper {
  321. position: absolute;
  322. left: 0;
  323. top: 0;
  324. right: 0;
  325. bottom: 0;
  326. /* #ifndef APP-NVUE */
  327. display: flex;
  328. /* #endif */
  329. flex-direction: row;
  330. align-items: center;
  331. justify-content: center;
  332. }
  333. .uni-indexed-list__alert {
  334. width: 80px;
  335. height: 80px;
  336. border-radius: 80px;
  337. text-align: center;
  338. line-height: 80px;
  339. font-size: 35px;
  340. color: #fff;
  341. background-color: rgba(0, 0, 0, 0.5);
  342. }
  343. </style>