uni-data-select.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. <template>
  2. <view class="uni-stat__select">
  3. <span v-if="label" class="uni-label-text hide-on-phone">{{
  4. label + ':'
  5. }}</span>
  6. <view class="uni-stat-box" :class="{ 'uni-stat__actived': current }">
  7. <view class="uni-select" :class="{ 'uni-select--disabled': disabled }">
  8. <view class="uni-select__input-box" @click="toggleSelector">
  9. <view v-if="current" class="uni-select__input-text">{{
  10. current
  11. }}</view>
  12. <view
  13. v-else
  14. class="uni-select__input-text uni-select__input-placeholder"
  15. >{{ typePlaceholder }}</view
  16. >
  17. <uni-icons
  18. v-if="current && clear && !disabled"
  19. type="clear"
  20. color="#c0c4cc"
  21. size="24"
  22. @click="clearVal"
  23. />
  24. <uni-icons
  25. v-else
  26. :type="showSelector ? 'top' : 'bottom'"
  27. size="14"
  28. color="#999"
  29. />
  30. </view>
  31. <view
  32. class="uni-select--mask"
  33. v-if="showSelector"
  34. @click="toggleSelector"
  35. />
  36. <view class="uni-select__selector" v-if="showSelector">
  37. <view class="uni-popper__arrow"></view>
  38. <scroll-view scroll-y="true" class="uni-select__selector-scroll">
  39. <view
  40. class="uni-select__selector-empty"
  41. v-if="mixinDatacomResData.length === 0"
  42. >
  43. <text>{{ emptyTips }}</text>
  44. </view>
  45. <view
  46. v-else
  47. class="uni-select__selector-item"
  48. v-for="(item, index) in mixinDatacomResData"
  49. :key="index"
  50. @click="change(item)"
  51. >
  52. <text
  53. :class="{ 'uni-select__selector__disabled': item.disable }"
  54. >{{ formatItemName(item) }}</text
  55. >
  56. </view>
  57. </scroll-view>
  58. </view>
  59. </view>
  60. </view>
  61. </view>
  62. </template>
  63. <script>
  64. /**
  65. * DataChecklist 数据选择器
  66. * @description 通过数据渲染的下拉框组件
  67. * @tutorial https://uniapp.dcloud.io/component/uniui/uni-data-select
  68. * @property {String} value 默认值
  69. * @property {Array} localdata 本地数据 ,格式 [{text:'',value:''}]
  70. * @property {Boolean} clear 是否可以清空已选项
  71. * @property {Boolean} emptyText 没有数据时显示的文字 ,本地数据无效
  72. * @property {String} label 左侧标题
  73. * @property {String} placeholder 输入框的提示文字
  74. * @property {Boolean} disabled 是否禁用
  75. * @event {Function} change 选中发生变化触发
  76. */
  77. export default {
  78. name: 'uni-stat-select',
  79. mixins: [uniCloud.mixinDatacom || {}],
  80. props: {
  81. localdata: {
  82. type: Array,
  83. default () {
  84. return []
  85. }
  86. },
  87. value: {
  88. type: [String, Number],
  89. default: ''
  90. },
  91. modelValue: {
  92. type: [String, Number],
  93. default: ''
  94. },
  95. label: {
  96. type: String,
  97. default: ''
  98. },
  99. placeholder: {
  100. type: String,
  101. default: '请选择'
  102. },
  103. emptyTips: {
  104. type: String,
  105. default: '无选项'
  106. },
  107. clear: {
  108. type: Boolean,
  109. default: true
  110. },
  111. defItem: {
  112. type: Number,
  113. default: 0
  114. },
  115. disabled: {
  116. type: Boolean,
  117. default: false
  118. },
  119. // 格式化输出 用法 field="_id as value, version as text, uni_platform as label" format="{label} - {text}"
  120. format: {
  121. type: String,
  122. default: ''
  123. }
  124. },
  125. data () {
  126. return {
  127. showSelector: false,
  128. current: '',
  129. mixinDatacomResData: [],
  130. apps: [],
  131. channels: [],
  132. cacheKey: 'uni-data-select-lastSelectedValue'
  133. }
  134. },
  135. created () {
  136. this.debounceGet = this.debounce(() => {
  137. this.query()
  138. }, 300)
  139. if (this.collection && !this.localdata.length) {
  140. this.debounceGet()
  141. }
  142. },
  143. computed: {
  144. typePlaceholder () {
  145. const text = {
  146. 'opendb-stat-app-versions': '版本',
  147. 'opendb-app-channels': '渠道',
  148. 'opendb-app-list': '应用'
  149. }
  150. const common = this.placeholder
  151. const placeholder = text[this.collection]
  152. return placeholder ? common + placeholder : common
  153. },
  154. valueCom () {
  155. // #ifdef VUE3
  156. return this.modelValue
  157. // #endif
  158. // #ifndef VUE3
  159. return this.value
  160. // #endif
  161. }
  162. },
  163. watch: {
  164. localdata: {
  165. immediate: true,
  166. handler (val, old) {
  167. if (Array.isArray(val) && old !== val) {
  168. this.mixinDatacomResData = val
  169. }
  170. }
  171. },
  172. valueCom (val, old) {
  173. this.initDefVal()
  174. },
  175. mixinDatacomResData: {
  176. immediate: true,
  177. handler (val) {
  178. if (val.length) {
  179. this.initDefVal()
  180. }
  181. }
  182. }
  183. },
  184. methods: {
  185. debounce (fn, time = 100) {
  186. let timer = null
  187. return function (...args) {
  188. if (timer) clearTimeout(timer)
  189. timer = setTimeout(() => {
  190. fn.apply(this, args)
  191. }, time)
  192. }
  193. },
  194. // 执行数据库查询
  195. query () {
  196. this.mixinDatacomEasyGet()
  197. },
  198. // 监听查询条件变更事件
  199. onMixinDatacomPropsChange () {
  200. if (this.collection) {
  201. this.debounceGet()
  202. }
  203. },
  204. initDefVal () {
  205. let defValue = ''
  206. if (
  207. (this.valueCom || this.valueCom === 0) &&
  208. !this.isDisabled(this.valueCom)
  209. ) {
  210. defValue = this.valueCom
  211. } else {
  212. let strogeValue
  213. if (this.collection) {
  214. strogeValue = this.getCache()
  215. }
  216. if (strogeValue || strogeValue === 0) {
  217. defValue = strogeValue
  218. } else {
  219. let defItem = ''
  220. if (
  221. this.defItem > 0 &&
  222. this.defItem <= this.mixinDatacomResData.length
  223. ) {
  224. defItem = this.mixinDatacomResData[this.defItem - 1].value
  225. }
  226. defValue = defItem
  227. }
  228. if (defValue || defValue === 0) {
  229. this.emit(defValue)
  230. }
  231. }
  232. const def = this.mixinDatacomResData.find(item => item.value === defValue)
  233. this.current = def ? this.formatItemName(def) : ''
  234. },
  235. /**
  236. * @param {[String, Number]} value
  237. * 判断用户给的 value 是否同时为禁用状态
  238. */
  239. isDisabled (value) {
  240. let isDisabled = false
  241. this.mixinDatacomResData.forEach(item => {
  242. if (item.value === value) {
  243. isDisabled = item.disable
  244. }
  245. })
  246. return isDisabled
  247. },
  248. clearVal () {
  249. this.emit('')
  250. if (this.collection) {
  251. this.removeCache()
  252. }
  253. },
  254. change (item) {
  255. if (!item.disable) {
  256. this.showSelector = false
  257. this.current = this.formatItemName(item)
  258. this.emit(item.value)
  259. }
  260. },
  261. emit (val) {
  262. this.$emit('change', val)
  263. this.$emit('input', val)
  264. this.$emit('update:modelValue', val)
  265. if (this.collection) {
  266. this.setCache(val)
  267. }
  268. },
  269. toggleSelector () {
  270. if (this.disabled) {
  271. return
  272. }
  273. this.showSelector = !this.showSelector
  274. },
  275. formatItemName (item) {
  276. let { text, value, channel_code } = item
  277. channel_code = channel_code ? `(${channel_code})` : ''
  278. if (this.format) {
  279. // 格式化输出
  280. let str = ''
  281. str = this.format
  282. for (let key in item) {
  283. str = str.replace(new RegExp(`{${key}}`, 'g'), item[key])
  284. }
  285. return str
  286. } else {
  287. return this.collection.indexOf('app-list') > 0
  288. ? `${text}(${value})`
  289. : text
  290. ? text
  291. : `未命名${channel_code}`
  292. }
  293. },
  294. // 获取当前加载的数据
  295. getLoadData () {
  296. return this.mixinDatacomResData
  297. },
  298. // 获取当前缓存key
  299. getCurrentCacheKey () {
  300. return this.collection
  301. },
  302. // 获取缓存
  303. getCache (name = this.getCurrentCacheKey()) {
  304. let cacheData = uni.getStorageSync(this.cacheKey) || {}
  305. return cacheData[name]
  306. },
  307. // 设置缓存
  308. setCache (value, name = this.getCurrentCacheKey()) {
  309. let cacheData = uni.getStorageSync(this.cacheKey) || {}
  310. cacheData[name] = value
  311. uni.setStorageSync(this.cacheKey, cacheData)
  312. },
  313. // 删除缓存
  314. removeCache (name = this.getCurrentCacheKey()) {
  315. let cacheData = uni.getStorageSync(this.cacheKey) || {}
  316. delete cacheData[name]
  317. uni.setStorageSync(this.cacheKey, cacheData)
  318. }
  319. }
  320. }
  321. </script>
  322. <style lang="scss">
  323. $uni-base-color: #6a6a6a !default;
  324. $uni-main-color: #333 !default;
  325. $uni-secondary-color: #909399 !default;
  326. $uni-border-3: #e5e5e5;
  327. /* #ifndef APP-NVUE */
  328. @media screen and (max-width: 500px) {
  329. .hide-on-phone {
  330. display: none;
  331. }
  332. }
  333. /* #endif */
  334. .uni-stat__select {
  335. display: flex;
  336. align-items: center;
  337. // padding: 15px;
  338. cursor: pointer;
  339. width: 100%;
  340. flex: 1;
  341. box-sizing: border-box;
  342. }
  343. .uni-stat-box {
  344. width: 100%;
  345. flex: 1;
  346. }
  347. .uni-stat__actived {
  348. width: 100%;
  349. flex: 1;
  350. // outline: 1px solid #2979ff;
  351. }
  352. .uni-label-text {
  353. font-size: 32rpx;
  354. font-weight: bold;
  355. color: $uni-base-color;
  356. margin: auto 0;
  357. margin-right: 5px;
  358. }
  359. .uni-select {
  360. font-size: 32rpx;
  361. border: 1px solid $uni-border-3;
  362. box-sizing: border-box;
  363. border-radius: 4px;
  364. padding: 0 5px;
  365. padding-left: 10px;
  366. position: relative;
  367. /* #ifndef APP-NVUE */
  368. display: flex;
  369. user-select: none;
  370. /* #endif */
  371. flex-direction: row;
  372. align-items: center;
  373. border-bottom: solid 1px $uni-border-3;
  374. width: 100%;
  375. flex: 1;
  376. height: 35px;
  377. &--disabled {
  378. background-color: #f5f7fa;
  379. cursor: not-allowed;
  380. }
  381. }
  382. .uni-select__label {
  383. font-size: 16px;
  384. // line-height: 22px;
  385. height: 35px;
  386. padding-right: 10px;
  387. color: $uni-secondary-color;
  388. }
  389. .uni-select__input-box {
  390. height: 35px;
  391. position: relative;
  392. /* #ifndef APP-NVUE */
  393. display: flex;
  394. /* #endif */
  395. flex: 1;
  396. flex-direction: row;
  397. align-items: center;
  398. }
  399. .uni-select__input {
  400. flex: 1;
  401. font-size: 32rpx;
  402. height: 22px;
  403. line-height: 22px;
  404. }
  405. .uni-select__input-plac {
  406. font-size: 32rpx;
  407. color: $uni-secondary-color;
  408. }
  409. .uni-select__selector {
  410. /* #ifndef APP-NVUE */
  411. box-sizing: border-box;
  412. /* #endif */
  413. position: absolute;
  414. top: calc(100% + 12px);
  415. left: 0;
  416. width: 100%;
  417. background-color: #ffffff;
  418. border: 1px solid #ebeef5;
  419. border-radius: 6px;
  420. box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  421. z-index: 3;
  422. padding: 4px 0;
  423. }
  424. .uni-select__selector-scroll {
  425. /* #ifndef APP-NVUE */
  426. max-height: 200px;
  427. box-sizing: border-box;
  428. /* #endif */
  429. }
  430. .uni-select__selector-empty,
  431. .uni-select__selector-item {
  432. /* #ifndef APP-NVUE */
  433. display: flex;
  434. cursor: pointer;
  435. /* #endif */
  436. line-height: 35px;
  437. font-size: 32rpx;
  438. text-align: center;
  439. /* border-bottom: solid 1px $uni-border-3; */
  440. padding: 0px 10px;
  441. }
  442. .uni-select__selector-item:hover {
  443. background-color: #f9f9f9;
  444. }
  445. .uni-select__selector-empty:last-child,
  446. .uni-select__selector-item:last-child {
  447. /* #ifndef APP-NVUE */
  448. border-bottom: none;
  449. /* #endif */
  450. }
  451. .uni-select__selector__disabled {
  452. opacity: 0.4;
  453. cursor: default;
  454. }
  455. /* picker 弹出层通用的指示小三角 */
  456. .uni-popper__arrow,
  457. .uni-popper__arrow::after {
  458. position: absolute;
  459. display: block;
  460. width: 0;
  461. height: 0;
  462. border-color: transparent;
  463. border-style: solid;
  464. border-width: 6px;
  465. }
  466. .uni-popper__arrow {
  467. filter: drop-shadow(0 2px 12px rgba(0, 0, 0, 0.03));
  468. top: -6px;
  469. left: 10%;
  470. margin-right: 3px;
  471. border-top-width: 0;
  472. border-bottom-color: #ebeef5;
  473. }
  474. .uni-popper__arrow::after {
  475. content: ' ';
  476. top: 1px;
  477. margin-left: -6px;
  478. border-top-width: 0;
  479. border-bottom-color: #fff;
  480. }
  481. .uni-select__input-text {
  482. // width: 280px;
  483. width: 100%;
  484. color: $uni-main-color;
  485. white-space: nowrap;
  486. text-overflow: ellipsis;
  487. -o-text-overflow: ellipsis;
  488. overflow: hidden;
  489. }
  490. .uni-select__input-placeholder {
  491. color: $uni-base-color;
  492. font-size: 12px;
  493. }
  494. .uni-select--mask {
  495. position: fixed;
  496. top: 0;
  497. bottom: 0;
  498. right: 0;
  499. left: 0;
  500. }
  501. </style>