uni-data-select.vue 12 KB

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