uni-data-select.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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">{{textShow}}</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. textShow(){
  135. // 长文本显示
  136. let text = this.current;
  137. if (text.length > 10) {
  138. return text.slice(0, 25) + '...';
  139. }
  140. return text;
  141. }
  142. },
  143. watch: {
  144. localdata: {
  145. immediate: true,
  146. handler(val, old) {
  147. if (Array.isArray(val) && old !== val) {
  148. this.mixinDatacomResData = val
  149. }
  150. }
  151. },
  152. valueCom(val, old) {
  153. this.initDefVal()
  154. },
  155. mixinDatacomResData: {
  156. immediate: true,
  157. handler(val) {
  158. if (val.length) {
  159. this.initDefVal()
  160. }
  161. }
  162. },
  163. },
  164. methods: {
  165. debounce(fn, time = 100){
  166. let timer = null
  167. return function(...args) {
  168. if (timer) clearTimeout(timer)
  169. timer = setTimeout(() => {
  170. fn.apply(this, args)
  171. }, time)
  172. }
  173. },
  174. // 执行数据库查询
  175. query(){
  176. this.mixinDatacomEasyGet();
  177. },
  178. // 监听查询条件变更事件
  179. onMixinDatacomPropsChange(){
  180. if (this.collection) {
  181. this.debounceGet();
  182. }
  183. },
  184. initDefVal() {
  185. let defValue = ''
  186. if ((this.valueCom || this.valueCom === 0) && !this.isDisabled(this.valueCom)) {
  187. defValue = this.valueCom
  188. } else {
  189. let strogeValue
  190. if (this.collection) {
  191. strogeValue = this.getCache()
  192. }
  193. if (strogeValue || strogeValue === 0) {
  194. defValue = strogeValue
  195. } else {
  196. let defItem = ''
  197. if (this.defItem > 0 && this.defItem <= this.mixinDatacomResData.length) {
  198. defItem = this.mixinDatacomResData[this.defItem - 1].value
  199. }
  200. defValue = defItem
  201. }
  202. if (defValue || defValue === 0) {
  203. this.emit(defValue)
  204. }
  205. }
  206. const def = this.mixinDatacomResData.find(item => item.value === defValue)
  207. this.current = def ? this.formatItemName(def) : ''
  208. },
  209. /**
  210. * @param {[String, Number]} value
  211. * 判断用户给的 value 是否同时为禁用状态
  212. */
  213. isDisabled(value) {
  214. let isDisabled = false;
  215. this.mixinDatacomResData.forEach(item => {
  216. if (item.value === value) {
  217. isDisabled = item.disable
  218. }
  219. })
  220. return isDisabled;
  221. },
  222. clearVal() {
  223. this.emit('')
  224. if (this.collection) {
  225. this.removeCache()
  226. }
  227. },
  228. change(item) {
  229. if (!item.disable) {
  230. this.showSelector = false
  231. this.current = this.formatItemName(item)
  232. this.emit(item.value)
  233. }
  234. },
  235. emit(val) {
  236. this.$emit('input', val)
  237. this.$emit('update:modelValue', val)
  238. this.$emit('change', val)
  239. if (this.collection) {
  240. this.setCache(val);
  241. }
  242. },
  243. toggleSelector() {
  244. if (this.disabled) {
  245. return
  246. }
  247. this.showSelector = !this.showSelector
  248. },
  249. formatItemName(item) {
  250. let {
  251. text,
  252. value,
  253. channel_code
  254. } = item
  255. channel_code = channel_code ? `(${channel_code})` : ''
  256. if (this.format) {
  257. // 格式化输出
  258. let str = "";
  259. str = this.format;
  260. for (let key in item) {
  261. str = str.replace(new RegExp(`{${key}}`,"g"),item[key]);
  262. }
  263. return str;
  264. } else {
  265. return this.collection.indexOf('app-list') > 0 ?
  266. `${text}(${value})` :
  267. (
  268. text ?
  269. text :
  270. `未命名${channel_code}`
  271. )
  272. }
  273. },
  274. // 获取当前加载的数据
  275. getLoadData(){
  276. return this.mixinDatacomResData;
  277. },
  278. // 获取当前缓存key
  279. getCurrentCacheKey(){
  280. return this.collection;
  281. },
  282. // 获取缓存
  283. getCache(name=this.getCurrentCacheKey()){
  284. let cacheData = uni.getStorageSync(this.cacheKey) || {};
  285. return cacheData[name];
  286. },
  287. // 设置缓存
  288. setCache(value, name=this.getCurrentCacheKey()){
  289. let cacheData = uni.getStorageSync(this.cacheKey) || {};
  290. cacheData[name] = value;
  291. uni.setStorageSync(this.cacheKey, cacheData);
  292. },
  293. // 删除缓存
  294. removeCache(name=this.getCurrentCacheKey()){
  295. let cacheData = uni.getStorageSync(this.cacheKey) || {};
  296. delete cacheData[name];
  297. uni.setStorageSync(this.cacheKey, cacheData);
  298. },
  299. }
  300. }
  301. </script>
  302. <style lang="scss">
  303. $uni-base-color: #6a6a6a !default;
  304. $uni-main-color: #333 !default;
  305. $uni-secondary-color: #909399 !default;
  306. $uni-border-3: #e5e5e5;
  307. /* #ifndef APP-NVUE */
  308. @media screen and (max-width: 500px) {
  309. .hide-on-phone {
  310. display: none;
  311. }
  312. }
  313. /* #endif */
  314. .uni-stat__select {
  315. display: flex;
  316. align-items: center;
  317. // padding: 15px;
  318. /* #ifdef H5 */
  319. cursor: pointer;
  320. /* #endif */
  321. width: 100%;
  322. flex: 1;
  323. box-sizing: border-box;
  324. }
  325. .uni-stat-box {
  326. width: 100%;
  327. flex: 1;
  328. }
  329. .uni-stat__actived {
  330. width: 100%;
  331. flex: 1;
  332. // outline: 1px solid #2979ff;
  333. }
  334. .uni-label-text {
  335. font-size: 14px;
  336. font-weight: bold;
  337. color: $uni-base-color;
  338. margin: auto 0;
  339. margin-right: 5px;
  340. }
  341. .uni-select {
  342. font-size: 14px;
  343. // border: 1px solid $uni-border-3;
  344. box-sizing: border-box;
  345. border-radius: 4px;
  346. padding: 0 5px;
  347. padding-left: 10px;
  348. position: relative;
  349. /* #ifndef APP-NVUE */
  350. display: flex;
  351. user-select: none;
  352. /* #endif */
  353. flex-direction: row;
  354. align-items: center;
  355. // border-bottom: solid 1px $uni-border-3;
  356. width: 100%;
  357. flex: 1;
  358. height: 35px;
  359. &--disabled {
  360. background-color: #f5f7fa;
  361. cursor: not-allowed;
  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>