uni-indexed-list.vue 7.4 KB

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