uni-indexed-list.vue 7.5 KB

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