uni-data-select.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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. 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. if (this.collection) {
  237. this.removeCache()
  238. }
  239. },
  240. change(item) {
  241. if (!item.disable) {
  242. this.showSelector = false
  243. this.current = this.formatItemName(item)
  244. this.emit(item.value)
  245. }
  246. },
  247. emit(val) {
  248. this.$emit('input', val)
  249. this.$emit('update:modelValue', val)
  250. this.$emit('change', val)
  251. if (this.collection) {
  252. this.setCache(val);
  253. }
  254. },
  255. toggleSelector() {
  256. if (this.disabled) {
  257. return
  258. }
  259. this.showSelector = !this.showSelector
  260. },
  261. formatItemName(item) {
  262. let {
  263. text,
  264. value,
  265. channel_code
  266. } = item
  267. channel_code = channel_code ? `(${channel_code})` : ''
  268. if (this.format) {
  269. // 格式化输出
  270. let str = "";
  271. str = this.format;
  272. for (let key in item) {
  273. str = str.replace(new RegExp(`{${key}}`, "g"), item[key]);
  274. }
  275. return str;
  276. } else {
  277. return this.collection.indexOf('app-list') > 0 ?
  278. `${text}(${value})` :
  279. (
  280. text ?
  281. text :
  282. `未命名${channel_code}`
  283. )
  284. }
  285. },
  286. // 获取当前加载的数据
  287. getLoadData() {
  288. return this.mixinDatacomResData;
  289. },
  290. // 获取当前缓存key
  291. getCurrentCacheKey() {
  292. return this.collection;
  293. },
  294. // 获取缓存
  295. getCache(name = this.getCurrentCacheKey()) {
  296. let cacheData = uni.getStorageSync(this.cacheKey) || {};
  297. return cacheData[name];
  298. },
  299. // 设置缓存
  300. setCache(value, name = this.getCurrentCacheKey()) {
  301. let cacheData = uni.getStorageSync(this.cacheKey) || {};
  302. cacheData[name] = value;
  303. uni.setStorageSync(this.cacheKey, cacheData);
  304. },
  305. // 删除缓存
  306. removeCache(name = this.getCurrentCacheKey()) {
  307. let cacheData = uni.getStorageSync(this.cacheKey) || {};
  308. delete cacheData[name];
  309. uni.setStorageSync(this.cacheKey, cacheData);
  310. },
  311. }
  312. }
  313. </script>
  314. <style lang="scss">
  315. $uni-base-color: #6a6a6a !default;
  316. $uni-main-color: #333 !default;
  317. $uni-secondary-color: #909399 !default;
  318. $uni-border-3: #e5e5e5;
  319. /* #ifndef APP-NVUE */
  320. @media screen and (max-width: 500px) {
  321. .hide-on-phone {
  322. display: none;
  323. }
  324. }
  325. /* #endif */
  326. .uni-stat__select {
  327. display: flex;
  328. align-items: center;
  329. // padding: 15px;
  330. /* #ifdef H5 */
  331. cursor: pointer;
  332. /* #endif */
  333. width: 100%;
  334. flex: 1;
  335. box-sizing: border-box;
  336. }
  337. .uni-stat-box {
  338. width: 100%;
  339. flex: 1;
  340. }
  341. .uni-stat__actived {
  342. width: 100%;
  343. flex: 1;
  344. // outline: 1px solid #2979ff;
  345. }
  346. .uni-label-text {
  347. font-size: 14px;
  348. font-weight: bold;
  349. color: $uni-base-color;
  350. margin: auto 0;
  351. margin-right: 5px;
  352. }
  353. .uni-select {
  354. font-size: 14px;
  355. border: 1px solid $uni-border-3;
  356. box-sizing: border-box;
  357. border-radius: 4px;
  358. padding: 0 5px;
  359. padding-left: 10px;
  360. position: relative;
  361. /* #ifndef APP-NVUE */
  362. display: flex;
  363. user-select: none;
  364. /* #endif */
  365. flex-direction: row;
  366. align-items: center;
  367. border-bottom: solid 1px $uni-border-3;
  368. width: 100%;
  369. flex: 1;
  370. height: 35px;
  371. &--disabled {
  372. background-color: #f5f7fa;
  373. cursor: not-allowed;
  374. }
  375. }
  376. .uni-select__label {
  377. font-size: 16px;
  378. // line-height: 22px;
  379. height: 35px;
  380. padding-right: 10px;
  381. color: $uni-secondary-color;
  382. }
  383. .uni-select__input-box {
  384. height: 35px;
  385. width: 0px;
  386. position: relative;
  387. /* #ifndef APP-NVUE */
  388. display: flex;
  389. /* #endif */
  390. flex: 1;
  391. flex-direction: row;
  392. align-items: center;
  393. }
  394. .uni-select__input {
  395. flex: 1;
  396. font-size: 14px;
  397. height: 22px;
  398. line-height: 22px;
  399. }
  400. .uni-select__input-plac {
  401. font-size: 14px;
  402. color: $uni-secondary-color;
  403. }
  404. .uni-select__selector {
  405. /* #ifndef APP-NVUE */
  406. box-sizing: border-box;
  407. /* #endif */
  408. position: absolute;
  409. left: 0;
  410. width: 100%;
  411. background-color: #FFFFFF;
  412. border: 1px solid #EBEEF5;
  413. border-radius: 6px;
  414. box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  415. z-index: 3;
  416. padding: 4px 0;
  417. }
  418. .uni-select__selector-scroll {
  419. /* #ifndef APP-NVUE */
  420. max-height: 200px;
  421. box-sizing: border-box;
  422. /* #endif */
  423. }
  424. /* #ifdef H5 */
  425. @media (min-width: 768px) {
  426. .uni-select__selector-scroll {
  427. max-height: 600px;
  428. }
  429. }
  430. /* #endif */
  431. .uni-select__selector-empty,
  432. .uni-select__selector-item {
  433. /* #ifndef APP-NVUE */
  434. display: flex;
  435. cursor: pointer;
  436. /* #endif */
  437. line-height: 35px;
  438. font-size: 14px;
  439. text-align: center;
  440. /* border-bottom: solid 1px $uni-border-3; */
  441. padding: 0px 10px;
  442. }
  443. .uni-select__selector-item:hover {
  444. background-color: #f9f9f9;
  445. }
  446. .uni-select__selector-empty:last-child,
  447. .uni-select__selector-item:last-child {
  448. /* #ifndef APP-NVUE */
  449. border-bottom: none;
  450. /* #endif */
  451. }
  452. .uni-select__selector__disabled {
  453. opacity: 0.4;
  454. cursor: default;
  455. }
  456. /* picker 弹出层通用的指示小三角 */
  457. .uni-popper__arrow_bottom,
  458. .uni-popper__arrow_bottom::after,
  459. .uni-popper__arrow_top,
  460. .uni-popper__arrow_top::after,
  461. {
  462. position: absolute;
  463. display: block;
  464. width: 0;
  465. height: 0;
  466. border-color: transparent;
  467. border-style: solid;
  468. border-width: 6px;
  469. }
  470. .uni-popper__arrow_bottom {
  471. filter: drop-shadow(0 2px 12px rgba(0, 0, 0, 0.03));
  472. top: -6px;
  473. left: 10%;
  474. margin-right: 3px;
  475. border-top-width: 0;
  476. border-bottom-color: #EBEEF5;
  477. }
  478. .uni-popper__arrow_bottom::after {
  479. content: " ";
  480. top: 1px;
  481. margin-left: -6px;
  482. border-top-width: 0;
  483. border-bottom-color: #fff;
  484. }
  485. .uni-popper__arrow_top {
  486. filter: drop-shadow(0 2px 12px rgba(0, 0, 0, 0.03));
  487. bottom: -6px;
  488. left: 10%;
  489. margin-right: 3px;
  490. border-bottom-width: 0;
  491. border-top-color: #EBEEF5;
  492. }
  493. .uni-popper__arrow_top::after {
  494. content: " ";
  495. bottom: 1px;
  496. margin-left: -6px;
  497. border-bottom-width: 0;
  498. border-top-color: #fff;
  499. }
  500. .uni-select__input-text {
  501. // width: 280px;
  502. width: 100%;
  503. color: $uni-main-color;
  504. white-space: nowrap;
  505. text-overflow: ellipsis;
  506. -o-text-overflow: ellipsis;
  507. overflow: hidden;
  508. }
  509. .uni-select__input-placeholder {
  510. color: $uni-base-color;
  511. font-size: 12px;
  512. }
  513. .uni-select--mask {
  514. position: fixed;
  515. top: 0;
  516. bottom: 0;
  517. right: 0;
  518. left: 0;
  519. z-index: 2;
  520. }
  521. </style>