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