uni-data-select.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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,'uni-select--border':border}">
  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. border: {
  90. type: Boolean,
  91. default: true
  92. },
  93. // 格式化输出 用法 field="_id as value, version as text, uni_platform as label" format="{label} - {text}"
  94. format: {
  95. type: String,
  96. default: ''
  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. &--border {
  360. border: 1px solid $uni-border-3;
  361. border-bottom: solid 1px $uni-border-3;
  362. }
  363. }
  364. .uni-select__label {
  365. font-size: 16px;
  366. // line-height: 22px;
  367. height: 35px;
  368. padding-right: 10px;
  369. color: $uni-secondary-color;
  370. }
  371. .uni-select__input-box {
  372. height: 35px;
  373. position: relative;
  374. /* #ifndef APP-NVUE */
  375. display: flex;
  376. /* #endif */
  377. flex: 1;
  378. flex-direction: row;
  379. align-items: center;
  380. }
  381. .uni-select__input {
  382. flex: 1;
  383. font-size: 14px;
  384. height: 22px;
  385. line-height: 22px;
  386. }
  387. .uni-select__input-plac {
  388. font-size: 14px;
  389. color: $uni-secondary-color;
  390. }
  391. .uni-select__selector {
  392. /* #ifndef APP-NVUE */
  393. box-sizing: border-box;
  394. /* #endif */
  395. position: absolute;
  396. top: calc(100% + 12px);
  397. left: 0;
  398. width: 100%;
  399. background-color: #FFFFFF;
  400. border: 1px solid #EBEEF5;
  401. border-radius: 6px;
  402. box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  403. z-index: 3;
  404. padding: 4px 0;
  405. }
  406. .uni-select__selector-scroll {
  407. /* #ifndef APP-NVUE */
  408. max-height: 200px;
  409. box-sizing: border-box;
  410. /* #endif */
  411. }
  412. /* #ifdef H5 */
  413. @media (min-width: 768px) {
  414. .uni-select__selector-scroll {
  415. max-height: 600px;
  416. }
  417. }
  418. /* #endif */
  419. .uni-select__selector-empty,
  420. .uni-select__selector-item {
  421. /* #ifndef APP-NVUE */
  422. display: flex;
  423. cursor: pointer;
  424. /* #endif */
  425. line-height: 35px;
  426. font-size: 14px;
  427. text-align: center;
  428. /* border-bottom: solid 1px $uni-border-3; */
  429. padding: 0px 10px;
  430. }
  431. .uni-select__selector-item:hover {
  432. background-color: #f9f9f9;
  433. }
  434. .uni-select__selector-empty:last-child,
  435. .uni-select__selector-item:last-child {
  436. /* #ifndef APP-NVUE */
  437. border-bottom: none;
  438. /* #endif */
  439. }
  440. .uni-select__selector__disabled {
  441. opacity: 0.4;
  442. cursor: default;
  443. }
  444. /* picker 弹出层通用的指示小三角 */
  445. .uni-popper__arrow,
  446. .uni-popper__arrow::after {
  447. position: absolute;
  448. display: block;
  449. width: 0;
  450. height: 0;
  451. border-color: transparent;
  452. border-style: solid;
  453. border-width: 6px;
  454. }
  455. .uni-popper__arrow {
  456. filter: drop-shadow(0 2px 12px rgba(0, 0, 0, 0.03));
  457. top: -6px;
  458. left: 10%;
  459. margin-right: 3px;
  460. border-top-width: 0;
  461. border-bottom-color: #EBEEF5;
  462. }
  463. .uni-popper__arrow::after {
  464. content: " ";
  465. top: 1px;
  466. margin-left: -6px;
  467. border-top-width: 0;
  468. border-bottom-color: #fff;
  469. }
  470. .uni-select__input-text {
  471. // width: 280px;
  472. width: 100%;
  473. color: $uni-main-color;
  474. white-space: nowrap;
  475. text-overflow: ellipsis;
  476. -o-text-overflow: ellipsis;
  477. overflow: hidden;
  478. }
  479. .uni-select__input-placeholder {
  480. color: $uni-base-color;
  481. font-size: 12px;
  482. }
  483. .uni-select--mask {
  484. position: fixed;
  485. top: 0;
  486. bottom: 0;
  487. right: 0;
  488. left: 0;
  489. z-index: 2;
  490. }
  491. </style>