uni-data-select.vue 12 KB

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