uni-indexed-list.vue 8.5 KB

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