uni-data-select.vue 11 KB

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