uni-indexed-list.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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="touchmove ? 'uni-indexed-list__menu--active' : ''" @touchstart="touchStart" @touchmove.stop.prevent="touchMove"
  21. @touchend="touchEnd" class="uni-indexed-list__menu">
  22. <view v-for="(list, key) in lists" :key="key" class="uni-indexed-list__menu-item">
  23. <text class="uni-indexed-list__menu-text" :class="touchmoveIndex == key ? 'uni-indexed-list__menu-text--active' : ''">{{ list.key }}</text>
  24. </view>
  25. </view>
  26. <view v-if="touchmove" class="uni-indexed-list__alert-wrapper">
  27. <text class="uni-indexed-list__alert">{{ lists[touchmoveIndex].key }}</text>
  28. </view>
  29. </view>
  30. </template>
  31. <script>
  32. import uniIcons from '../uni-icons/uni-icons.vue'
  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. uniIcons,
  87. indexedListItem
  88. },
  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. touchmoveTimeout: '',
  111. loaded: false
  112. }
  113. },
  114. watch: {
  115. options: {
  116. handler: function() {
  117. this.setList()
  118. },
  119. deep: true
  120. }
  121. },
  122. mounted() {
  123. setTimeout(() => {
  124. this.setList()
  125. }, 50)
  126. setTimeout(() => {
  127. this.loaded = true
  128. }, 300);
  129. },
  130. methods: {
  131. setList() {
  132. let index = 0;
  133. this.lists = []
  134. this.options.forEach((value, index) => {
  135. if (value.data.length === 0) {
  136. return
  137. }
  138. let indexBefore = index
  139. let items = value.data.map(item => {
  140. let obj = {}
  141. obj['key'] = value.letter
  142. obj['name'] = item
  143. obj['itemIndex'] = index
  144. index++
  145. obj.checked = item.checked ? item.checked : false
  146. return obj
  147. })
  148. this.lists.push({
  149. title: value.letter,
  150. key: value.letter,
  151. items: items,
  152. itemIndex: indexBefore
  153. })
  154. })
  155. // #ifndef APP-NVUE
  156. uni.createSelectorQuery()
  157. .in(this)
  158. .select('#list')
  159. .boundingClientRect()
  160. .exec(ret => {
  161. this.winOffsetY = ret[0].top
  162. this.winHeight = ret[0].height
  163. this.itemHeight = this.winHeight / this.lists.length
  164. })
  165. // #endif
  166. // #ifdef APP-NVUE
  167. dom.getComponentRect(this.$refs['list'], (res) => {
  168. this.winOffsetY = res.size.top
  169. this.winHeight = res.size.height
  170. this.itemHeight = this.winHeight / this.lists.length
  171. })
  172. // #endif
  173. },
  174. touchStart(e) {
  175. this.touchmove = true
  176. let pageY = e.touches[0].pageY
  177. let index = Math.floor((pageY - this.winOffsetY) / this.itemHeight)
  178. let item = this.lists[index]
  179. if (item) {
  180. this.scrollViewId = 'uni-indexed-list-' + index
  181. this.touchmoveIndex = index
  182. // #ifdef APP-NVUE
  183. dom.scrollToElement(this.$refs['uni-indexed-list-' + index][0], {
  184. animated: false
  185. })
  186. // #endif
  187. }
  188. },
  189. touchMove(e) {
  190. // #ifndef APP-PLUS
  191. let pageY = e.touches[0].pageY
  192. let index = Math.floor((pageY - this.winOffsetY) / this.itemHeight)
  193. if (this.touchmoveIndex === index) {
  194. return false
  195. }
  196. let item = this.lists[index]
  197. if (item) {
  198. this.scrollViewId = 'uni-indexed-list-' + index
  199. this.touchmoveIndex = index
  200. }
  201. // #endif
  202. // #ifdef APP-PLUS
  203. throttleTouchMove.call(this, e)
  204. // #endif
  205. },
  206. touchEnd() {
  207. this.touchmove = false
  208. this.touchmoveIndex = -1
  209. },
  210. onClick(e) {
  211. let {
  212. idx,
  213. index
  214. } = e
  215. let obj = {}
  216. for (let key in this.lists[idx].items[index]) {
  217. obj[key] = this.lists[idx].items[index][key]
  218. }
  219. let select = []
  220. if (this.showSelect) {
  221. this.lists[idx].items[index].checked = !this.lists[idx].items[index].checked
  222. this.lists.forEach((value, idx) => {
  223. value.items.forEach((item, index) => {
  224. if (item.checked) {
  225. let obj = {}
  226. for (let key in this.lists[idx].items[index]) {
  227. obj[key] = this.lists[idx].items[index][key]
  228. }
  229. select.push(obj)
  230. }
  231. })
  232. })
  233. }
  234. this.$emit('click', {
  235. item: obj,
  236. select: select
  237. })
  238. }
  239. }
  240. }
  241. </script>
  242. <style lang="scss" scoped>
  243. .uni-indexed-list {
  244. position: absolute;
  245. left: 0;
  246. top: 0;
  247. right: 0;
  248. bottom: 0;
  249. /* #ifndef APP-NVUE */
  250. display: flex;
  251. /* #endif */
  252. flex-direction: row;
  253. }
  254. .uni-indexed-list__scroll {
  255. flex: 1;
  256. }
  257. .uni-indexed-list__menu {
  258. width: 24px;
  259. background-color: lightgrey;
  260. /* #ifndef APP-NVUE */
  261. display: flex;
  262. /* #endif */
  263. flex-direction: column;
  264. }
  265. .uni-indexed-list__menu-item {
  266. /* #ifndef APP-NVUE */
  267. display: flex;
  268. /* #endif */
  269. flex: 1;
  270. align-items: center;
  271. justify-content: center;
  272. }
  273. .uni-indexed-list__menu-text {
  274. line-height: 20px;
  275. font-size: 12px;
  276. text-align: center;
  277. color: #aaa;
  278. }
  279. .uni-indexed-list__menu--active {
  280. background-color: rgb(200, 200, 200);
  281. }
  282. .uni-indexed-list__menu-text--active {
  283. color: #007aff;
  284. }
  285. .uni-indexed-list__alert-wrapper {
  286. position: absolute;
  287. left: 0;
  288. top: 0;
  289. right: 0;
  290. bottom: 0;
  291. /* #ifndef APP-NVUE */
  292. display: flex;
  293. /* #endif */
  294. flex-direction: row;
  295. align-items: center;
  296. justify-content: center;
  297. }
  298. .uni-indexed-list__alert {
  299. width: 80px;
  300. height: 80px;
  301. border-radius: 80px;
  302. text-align: center;
  303. line-height: 80px;
  304. font-size: 35px;
  305. color: #fff;
  306. background-color: rgba(0, 0, 0, 0.5);
  307. }
  308. </style>