uni-data-indexed-list.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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.value }}</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] }}</text>
  27. </view>
  28. </view>
  29. </template>
  30. <script>
  31. import uniIcons from '../uni-icons/uni-icons.vue'
  32. import uniIndexedListItem from './uni-data-indexed-list-item.vue'
  33. import clientdb from './clientdb.js'
  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. * @property {String|DBCollectionString} collection 表名
  81. * @property {String|ClientDBActionString} action 云端执行数据库查询的前或后,触发某个action函数操作,进行预处理或后处理
  82. * @property {String|DBFieldString} field 查询字段,多个字段用 `,` 分割
  83. * @property {String} orderby 排序字段及正序倒叙设置
  84. * @property {String|JQLString} where 查询条件
  85. * @event {Function} click 点击列表事件 ,返回当前选择项的事件对象
  86. * @example <uni-indexed-list options="" showSelect="false" @click=""></uni-indexed-list>
  87. */
  88. export default {
  89. name: 'UniDataIndexedList',
  90. mixins: [clientdb],
  91. components: {
  92. uniIcons,
  93. uniIndexedListItem
  94. },
  95. props: {
  96. options: {
  97. type: Array,
  98. default () {
  99. return []
  100. }
  101. },
  102. localdata: {
  103. type: Array,
  104. default () {
  105. return []
  106. }
  107. },
  108. showSelect: {
  109. type: Boolean,
  110. default: false
  111. }
  112. },
  113. data() {
  114. return {
  115. lists: [],
  116. winHeight: 0,
  117. itemHeight: 0,
  118. winOffsetY: 0,
  119. touchmove: false,
  120. touchmoveIndex: -1,
  121. scrollViewId: '',
  122. touchmoveTimeout: '',
  123. loaded: false
  124. }
  125. },
  126. watch: {
  127. options: {
  128. handler: function() {
  129. this.setList()
  130. },
  131. deep: true
  132. }
  133. },
  134. mounted() {
  135. if (this.localdata.length || this.options.length) {
  136. setTimeout(() => {
  137. this.setList()
  138. }, 50)
  139. setTimeout(() => {
  140. this.loaded = true
  141. }, 300);
  142. } else if (this.collection) {
  143. if (!this.manual) {
  144. this._execLoadData((data) => {
  145. this.lists = this.groupData(data);
  146. })
  147. }
  148. }
  149. },
  150. methods: {
  151. groupData(data) {
  152. let groups = {};
  153. for (let i = 0; i < data.length; i++) {
  154. let item = data[i];
  155. let group = item.group;
  156. if (!groups[group]) {
  157. groups[group] = {
  158. "title": group,
  159. "value": group,
  160. "itemIndex": i,
  161. "items": []
  162. }
  163. }
  164. groups[group].items.push(item);
  165. }
  166. let result = []
  167. for (let g in groups) {
  168. let group = groups[g];
  169. let items = group.items;
  170. for (let j = 0; j < items.length; j++) {
  171. items[j].itemIndex = j;
  172. }
  173. result.push(group);
  174. }
  175. return result;
  176. },
  177. setList(data) {
  178. let index = 0;
  179. this.lists = []
  180. this.options.forEach((value, index) => {
  181. if (value.data.length === 0) {
  182. return
  183. }
  184. let indexBefore = index
  185. let items = value.data.map(item => {
  186. let obj = {}
  187. obj['value'] = value.letter
  188. obj['text'] = item
  189. obj['itemIndex'] = index
  190. index++
  191. obj.checked = item.checked ? item.checked : false
  192. return obj
  193. })
  194. this.lists.push({
  195. title: value.letter,
  196. value: value.letter,
  197. items: items,
  198. itemIndex: indexBefore
  199. })
  200. })
  201. // #ifndef APP-NVUE
  202. uni.createSelectorQuery()
  203. .in(this)
  204. .select('#list')
  205. .boundingClientRect()
  206. .exec(ret => {
  207. this.winOffsetY = ret[0].top
  208. this.winHeight = ret[0].height
  209. this.itemHeight = this.winHeight / this.lists.length
  210. })
  211. // #endif
  212. // #ifdef APP-NVUE
  213. dom.getComponentRect(this.$refs['list'], (res) => {
  214. this.winOffsetY = res.size.top
  215. this.winHeight = res.size.height
  216. this.itemHeight = this.winHeight / this.lists.length
  217. })
  218. // #endif
  219. },
  220. touchStart(e) {
  221. this.touchmove = true
  222. let pageY = e.touches[0].pageY
  223. let index = Math.floor((pageY - this.winOffsetY) / this.itemHeight)
  224. let item = this.lists[index]
  225. if (item) {
  226. this.scrollViewId = 'uni-indexed-list-' + index
  227. this.touchmoveIndex = index
  228. // #ifdef APP-NVUE
  229. dom.scrollToElement(this.$refs['uni-indexed-list-' + index][0], {
  230. animated: false
  231. })
  232. // #endif
  233. }
  234. },
  235. touchMove(e) {
  236. // #ifndef APP-PLUS
  237. let pageY = e.touches[0].pageY
  238. let index = Math.floor((pageY - this.winOffsetY) / this.itemHeight)
  239. if (this.touchmoveIndex === index) {
  240. return false
  241. }
  242. let item = this.lists[index]
  243. if (item) {
  244. this.scrollViewId = 'uni-indexed-list-' + index
  245. this.touchmoveIndex = index
  246. }
  247. // #endif
  248. // #ifdef APP-PLUS
  249. throttleTouchMove.call(this, e)
  250. // #endif
  251. },
  252. touchEnd() {
  253. this.touchmove = false
  254. this.touchmoveIndex = -1
  255. },
  256. onClick(e) {
  257. let {
  258. idx,
  259. index
  260. } = e
  261. let obj = {}
  262. for (let key in this.lists[idx].items[index]) {
  263. obj[key] = this.lists[idx].items[index][key]
  264. }
  265. let select = []
  266. if (this.showSelect) {
  267. this.lists[idx].items[index].checked = !this.lists[idx].items[index].checked
  268. this.lists.forEach((value, idx) => {
  269. value.items.forEach((item, index) => {
  270. if (item.checked) {
  271. let obj = {}
  272. for (let key in this.lists[idx].items[index]) {
  273. obj[key] = this.lists[idx].items[index][key]
  274. }
  275. select.push(obj)
  276. }
  277. })
  278. })
  279. }
  280. this.$emit('click', {
  281. item: obj,
  282. select: select
  283. })
  284. }
  285. }
  286. }
  287. </script>
  288. <style scoped>
  289. .uni-indexed-list {
  290. position: absolute;
  291. left: 0;
  292. top: 0;
  293. right: 0;
  294. bottom: 0;
  295. /* #ifndef APP-NVUE */
  296. display: flex;
  297. /* #endif */
  298. flex-direction: row;
  299. }
  300. .uni-indexed-list__scroll {
  301. flex: 1;
  302. }
  303. .uni-indexed-list__menu {
  304. width: 24px;
  305. background-color: lightgrey;
  306. /* #ifndef APP-NVUE */
  307. display: flex;
  308. /* #endif */
  309. flex-direction: column;
  310. }
  311. .uni-indexed-list__menu-item {
  312. /* #ifndef APP-NVUE */
  313. display: flex;
  314. /* #endif */
  315. flex: 1;
  316. align-items: center;
  317. justify-content: center;
  318. }
  319. .uni-indexed-list__menu-text {
  320. line-height: 20px;
  321. font-size: 12px;
  322. text-align: center;
  323. color: #aaa;
  324. }
  325. .uni-indexed-list__menu--active {
  326. background-color: #c8c8c8;
  327. }
  328. .uni-indexed-list__menu-text--active {
  329. color: #007aff;
  330. }
  331. .uni-indexed-list__alert-wrapper {
  332. position: absolute;
  333. left: 0;
  334. top: 0;
  335. right: 0;
  336. bottom: 0;
  337. /* #ifndef APP-NVUE */
  338. display: flex;
  339. /* #endif */
  340. flex-direction: row;
  341. align-items: center;
  342. justify-content: center;
  343. }
  344. .uni-indexed-list__alert {
  345. width: 80px;
  346. height: 80px;
  347. border-radius: 80px;
  348. text-align: center;
  349. line-height: 80px;
  350. font-size: 35px;
  351. color: #fff;
  352. background-color: rgba(0, 0, 0, 0.5);
  353. }
  354. </style>