uni-data-select.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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. width: 500rpx;
  323. flex: 1;
  324. box-sizing: border-box;
  325. }
  326. .uni-stat-box {
  327. width: 100%;
  328. flex: 1;
  329. }
  330. .uni-stat__actived {
  331. width: 100%;
  332. flex: 1;
  333. // outline: 1px solid #2979ff;
  334. }
  335. .uni-label-text {
  336. font-size: 14px;
  337. font-weight: bold;
  338. color: $uni-base-color;
  339. margin: auto 0;
  340. margin-right: 5px;
  341. }
  342. .uni-select {
  343. font-size: 14px;
  344. /*<!--border: 1px solid $uni-border-3;-->*/
  345. box-sizing: border-box;
  346. border-radius: 4px;
  347. padding: 0 5px;
  348. padding-left: 10px;
  349. position: relative;
  350. /* #ifndef APP-NVUE */
  351. display: flex;
  352. user-select: none;
  353. /* #endif */
  354. flex-direction: row;
  355. align-items: center;
  356. /*<!--border-bottom: solid 1px $uni-border-3;-->*/
  357. width: 100%;
  358. flex: 1;
  359. height: 35px;
  360. &--disabled {
  361. background-color: #f5f7fa;
  362. cursor: not-allowed;
  363. }
  364. }
  365. .uni-select__label {
  366. font-size: 16px;
  367. // line-height: 22px;
  368. height: 35px;
  369. padding-right: 10px;
  370. color: $uni-secondary-color;
  371. }
  372. .uni-select__input-box {
  373. height: 35px;
  374. position: relative;
  375. /* #ifndef APP-NVUE */
  376. display: flex;
  377. /* #endif */
  378. flex: 1;
  379. flex-direction: row;
  380. align-items: center;
  381. }
  382. .uni-select__input {
  383. flex: 1;
  384. font-size: 14px;
  385. height: 22px;
  386. line-height: 22px;
  387. }
  388. .uni-select__input-plac {
  389. font-size: 14px;
  390. color: $uni-secondary-color;
  391. }
  392. .uni-select__selector {
  393. /* #ifndef APP-NVUE */
  394. box-sizing: border-box;
  395. /* #endif */
  396. position: absolute;
  397. top: calc(100% + 12px);
  398. left: 0;
  399. width: 100%;
  400. background-color: #FFFFFF;
  401. border: 1px solid #EBEEF5;
  402. border-radius: 6px;
  403. box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  404. z-index: 3;
  405. padding: 4px 0;
  406. }
  407. .uni-select__selector-scroll {
  408. /* #ifndef APP-NVUE */
  409. max-height: 200px;
  410. box-sizing: border-box;
  411. /* #endif */
  412. }
  413. /* #ifdef H5 */
  414. @media (min-width: 768px) {
  415. .uni-select__selector-scroll {
  416. max-height: 600px;
  417. }
  418. }
  419. /* #endif */
  420. .uni-select__selector-empty,
  421. .uni-select__selector-item {
  422. /* #ifndef APP-NVUE */
  423. display: flex;
  424. cursor: pointer;
  425. /* #endif */
  426. line-height: 35px;
  427. font-size: 14px;
  428. text-align: center;
  429. /* border-bottom: solid 1px $uni-border-3; */
  430. padding: 0px 10px;
  431. }
  432. .uni-select__selector-item:hover {
  433. background-color: #f9f9f9;
  434. }
  435. .uni-select__selector-empty:last-child,
  436. .uni-select__selector-item:last-child {
  437. /* #ifndef APP-NVUE */
  438. border-bottom: none;
  439. /* #endif */
  440. }
  441. .uni-select__selector__disabled {
  442. opacity: 0.4;
  443. cursor: default;
  444. }
  445. /* picker 弹出层通用的指示小三角 */
  446. .uni-popper__arrow,
  447. .uni-popper__arrow::after {
  448. position: absolute;
  449. display: block;
  450. width: 0;
  451. height: 0;
  452. border-color: transparent;
  453. border-style: solid;
  454. border-width: 6px;
  455. }
  456. .uni-popper__arrow {
  457. filter: drop-shadow(0 2px 12px rgba(0, 0, 0, 0.03));
  458. top: -6px;
  459. left: 10%;
  460. margin-right: 3px;
  461. border-top-width: 0;
  462. border-bottom-color: #EBEEF5;
  463. }
  464. .uni-popper__arrow::after {
  465. content: " ";
  466. top: 1px;
  467. margin-left: -6px;
  468. border-top-width: 0;
  469. border-bottom-color: #fff;
  470. }
  471. .uni-select__input-text {
  472. // width: 280px;
  473. width: 100%;
  474. color: $uni-main-color;
  475. white-space: nowrap;
  476. text-overflow: ellipsis;
  477. -o-text-overflow: ellipsis;
  478. overflow: hidden;
  479. }
  480. .uni-select__input-placeholder {
  481. color: $uni-base-color;
  482. font-size: 12px;
  483. }
  484. .uni-select--mask {
  485. position: fixed;
  486. top: 0;
  487. bottom: 0;
  488. right: 0;
  489. left: 0;
  490. z-index: 2;
  491. }
  492. </style>