uni-indexed-list.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. emits:['click'],
  89. props: {
  90. options: {
  91. type: Array,
  92. default () {
  93. return []
  94. }
  95. },
  96. showSelect: {
  97. type: Boolean,
  98. default: false
  99. }
  100. },
  101. data() {
  102. return {
  103. lists: [],
  104. winHeight: 0,
  105. itemHeight: 0,
  106. winOffsetY: 0,
  107. touchmove: false,
  108. touchmoveIndex: -1,
  109. scrollViewId: '',
  110. touchmovable: true,
  111. loaded: false,
  112. isPC: false
  113. }
  114. },
  115. watch: {
  116. options: {
  117. handler: function() {
  118. this.setList()
  119. },
  120. deep: true
  121. }
  122. },
  123. mounted() {
  124. // #ifdef H5
  125. this.isPC = this.IsPC()
  126. // #endif
  127. setTimeout(() => {
  128. this.setList()
  129. }, 50)
  130. setTimeout(() => {
  131. this.loaded = true
  132. }, 300);
  133. },
  134. methods: {
  135. setList() {
  136. let index = 0;
  137. this.lists = []
  138. this.options.forEach((value, index) => {
  139. if (value.data.length === 0) {
  140. return
  141. }
  142. let indexBefore = index
  143. let items = value.data.map(item => {
  144. let obj = {}
  145. obj['key'] = value.letter
  146. obj['name'] = item
  147. obj['itemIndex'] = index
  148. index++
  149. obj.checked = item.checked ? item.checked : false
  150. return obj
  151. })
  152. this.lists.push({
  153. title: value.letter,
  154. key: value.letter,
  155. items: items,
  156. itemIndex: indexBefore
  157. })
  158. })
  159. // #ifndef APP-NVUE
  160. uni.createSelectorQuery()
  161. .in(this)
  162. .select('#list')
  163. .boundingClientRect()
  164. .exec(ret => {
  165. this.winOffsetY = ret[0].top
  166. this.winHeight = ret[0].height
  167. this.itemHeight = this.winHeight / this.lists.length
  168. })
  169. // #endif
  170. // #ifdef APP-NVUE
  171. dom.getComponentRect(this.$refs['list'], (res) => {
  172. this.winOffsetY = res.size.top
  173. this.winHeight = res.size.height
  174. this.itemHeight = this.winHeight / this.lists.length
  175. })
  176. // #endif
  177. },
  178. touchStart(e) {
  179. this.touchmove = true
  180. let pageY = this.isPC ? e.pageY : e.touches[0].pageY
  181. let index = Math.floor((pageY - this.winOffsetY) / this.itemHeight)
  182. let item = this.lists[index]
  183. if (item) {
  184. this.scrollViewId = 'uni-indexed-list-' + index
  185. this.touchmoveIndex = index
  186. // #ifdef APP-NVUE
  187. dom.scrollToElement(this.$refs['uni-indexed-list-' + index][0], {
  188. animated: false
  189. })
  190. // #endif
  191. }
  192. },
  193. touchMove(e) {
  194. // #ifndef APP-PLUS
  195. let pageY = this.isPC ? e.pageY : e.touches[0].pageY
  196. let index = Math.floor((pageY - this.winOffsetY) / this.itemHeight)
  197. if (this.touchmoveIndex === index) {
  198. return false
  199. }
  200. let item = this.lists[index]
  201. if (item) {
  202. this.scrollViewId = 'uni-indexed-list-' + index
  203. this.touchmoveIndex = index
  204. }
  205. // #endif
  206. // #ifdef APP-PLUS
  207. throttleTouchMove.call(this, e)
  208. // #endif
  209. },
  210. touchEnd() {
  211. this.touchmove = false
  212. this.touchmoveIndex = -1
  213. },
  214. /**
  215. * 兼容 PC @tian
  216. */
  217. mousedown(e) {
  218. if (!this.isPC) return
  219. this.touchStart(e)
  220. },
  221. mousemove(e) {
  222. if (!this.isPC) return
  223. this.touchMove(e)
  224. },
  225. mouseleave(e) {
  226. if (!this.isPC) return
  227. this.touchEnd(e)
  228. },
  229. // #ifdef H5
  230. IsPC() {
  231. var userAgentInfo = navigator.userAgent;
  232. var Agents = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"];
  233. var flag = true;
  234. for (let v = 0; v < Agents.length - 1; v++) {
  235. if (userAgentInfo.indexOf(Agents[v]) > 0) {
  236. flag = false;
  237. break;
  238. }
  239. }
  240. return flag;
  241. },
  242. // #endif
  243. onClick(e) {
  244. let {
  245. idx,
  246. index
  247. } = e
  248. let obj = {}
  249. for (let key in this.lists[idx].items[index]) {
  250. obj[key] = this.lists[idx].items[index][key]
  251. }
  252. let select = []
  253. if (this.showSelect) {
  254. this.lists[idx].items[index].checked = !this.lists[idx].items[index].checked
  255. this.lists.forEach((value, idx) => {
  256. value.items.forEach((item, index) => {
  257. if (item.checked) {
  258. let obj = {}
  259. for (let key in this.lists[idx].items[index]) {
  260. obj[key] = this.lists[idx].items[index][key]
  261. }
  262. select.push(obj)
  263. }
  264. })
  265. })
  266. }
  267. this.$emit('click', {
  268. item: obj,
  269. select: select
  270. })
  271. }
  272. }
  273. }
  274. </script>
  275. <style lang="scss" scoped>
  276. .uni-indexed-list {
  277. position: absolute;
  278. left: 0;
  279. top: 0;
  280. right: 0;
  281. bottom: 0;
  282. /* #ifndef APP-NVUE */
  283. display: flex;
  284. /* #endif */
  285. flex-direction: row;
  286. }
  287. .uni-indexed-list__scroll {
  288. flex: 1;
  289. }
  290. .uni-indexed-list__menu {
  291. width: 24px;
  292. background-color: lightgrey;
  293. /* #ifndef APP-NVUE */
  294. display: flex;
  295. /* #endif */
  296. flex-direction: column;
  297. }
  298. .uni-indexed-list__menu-item {
  299. /* #ifndef APP-NVUE */
  300. display: flex;
  301. /* #endif */
  302. flex: 1;
  303. align-items: center;
  304. justify-content: center;
  305. /* #ifdef H5 */
  306. cursor: pointer;
  307. /* #endif */
  308. }
  309. .uni-indexed-list__menu-text {
  310. line-height: 20px;
  311. font-size: 12px;
  312. text-align: center;
  313. color: #aaa;
  314. }
  315. .uni-indexed-list__menu--active {
  316. background-color: rgb(200, 200, 200);
  317. }
  318. .uni-indexed-list__menu-text--active {
  319. color: #007aff;
  320. }
  321. .uni-indexed-list__alert-wrapper {
  322. position: absolute;
  323. left: 0;
  324. top: 0;
  325. right: 0;
  326. bottom: 0;
  327. /* #ifndef APP-NVUE */
  328. display: flex;
  329. /* #endif */
  330. flex-direction: row;
  331. align-items: center;
  332. justify-content: center;
  333. }
  334. .uni-indexed-list__alert {
  335. width: 80px;
  336. height: 80px;
  337. border-radius: 80px;
  338. text-align: center;
  339. line-height: 80px;
  340. font-size: 35px;
  341. color: #fff;
  342. background-color: rgba(0, 0, 0, 0.5);
  343. }
  344. </style>