uni-data-select.vue 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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">{{current}}</view>
  8. <view v-else class="uni-select__input-text uni-select__input-placeholder">{{typePlaceholder}}</view>
  9. <uni-icons v-if="current && clear" type="clear" color="#c0c4cc" size="24" @click="clearVal" />
  10. <uni-icons v-else :type="showSelector? 'top' : 'bottom'" size="14" color="#999" />
  11. </view>
  12. <view class="uni-select--mask" v-if="showSelector" @click="toggleSelector" />
  13. <view class="uni-select__selector" v-if="showSelector">
  14. <view class="uni-popper__arrow"></view>
  15. <scroll-view scroll-y="true" class="uni-select__selector-scroll">
  16. <view class="uni-select__selector-empty" v-if="mixinDatacomResData.length === 0">
  17. <text>{{emptyTips}}</text>
  18. </view>
  19. <view v-else class="uni-select__selector-item" v-for="(item,index) in mixinDatacomResData"
  20. :key="index" @click="change(item)">
  21. <text :class="{'uni-select__selector__disabled': item.disable}">
  22. <slot name="label" :data="formatItemName(item)" v-if="labelSlot"></slot>
  23. <template v-else>{{formatItemName(item)}}</template>
  24. </text>
  25. </view>
  26. </scroll-view>
  27. </view>
  28. </view>
  29. </view>
  30. </view>
  31. </template>
  32. <script>
  33. /**
  34. * DataChecklist 数据选择器
  35. * @description 通过数据渲染的下拉框组件
  36. * @tutorial https://uniapp.dcloud.io/component/uniui/uni-data-select
  37. * @property {String} value 默认值
  38. * @property {Array} localdata 本地数据 ,格式 [{text:'',value:''}]
  39. * @property {Boolean} clear 是否可以清空已选项
  40. * @property {Boolean} emptyText 没有数据时显示的文字 ,本地数据无效
  41. * @property {String} label 左侧标题
  42. * @property {String} placeholder 输入框的提示文字
  43. * @property {Boolean} disabled 是否禁用
  44. * @event {Function} change 选中发生变化触发
  45. */
  46. export default {
  47. name: "uni-stat-select",
  48. mixins: [uniCloud.mixinDatacom || {}],
  49. data() {
  50. return {
  51. showSelector: false,
  52. current: '',
  53. mixinDatacomResData: [],
  54. apps: [],
  55. channels: []
  56. };
  57. },
  58. props: {
  59. localdata: {
  60. type: Array,
  61. default () {
  62. return []
  63. }
  64. },
  65. value: {
  66. type: [String, Number],
  67. default: ''
  68. },
  69. labelSlot : {
  70. type : Boolean,
  71. default : false
  72. },
  73. modelValue: {
  74. type: [String, Number],
  75. default: ''
  76. },
  77. label: {
  78. type: String,
  79. default: ''
  80. },
  81. placeholder: {
  82. type: String,
  83. default: '请选择'
  84. },
  85. emptyTips: {
  86. type: String,
  87. default: '无选项'
  88. },
  89. clear: {
  90. type: Boolean,
  91. default: true
  92. },
  93. defItem: {
  94. type: Number,
  95. default: 0
  96. },
  97. disabled: {
  98. type: Boolean,
  99. default: false
  100. }
  101. },
  102. created() {
  103. this.last = `${this.collection}_last_selected_option_value`
  104. if (this.collection && !this.localdata.length) {
  105. this.mixinDatacomEasyGet()
  106. }
  107. },
  108. computed: {
  109. typePlaceholder() {
  110. const text = {
  111. 'opendb-stat-app-versions': '版本',
  112. 'opendb-app-channels': '渠道',
  113. 'opendb-app-list': '应用'
  114. }
  115. const common = this.placeholder
  116. const placeholder = text[this.collection]
  117. return placeholder ?
  118. common + placeholder :
  119. common
  120. }
  121. },
  122. watch: {
  123. localdata: {
  124. immediate: true,
  125. handler(val, old) {
  126. if (Array.isArray(val) && old !== val) {
  127. this.mixinDatacomResData = val
  128. }
  129. }
  130. },
  131. // #ifndef VUE3
  132. value() {
  133. this.initDefVal()
  134. },
  135. // #endif
  136. // #ifdef VUE3
  137. modelValue() {
  138. this.initDefVal()
  139. },
  140. // #endif
  141. mixinDatacomResData: {
  142. immediate: true,
  143. handler(val) {
  144. if (val.length) {
  145. this.initDefVal()
  146. }
  147. }
  148. }
  149. },
  150. methods: {
  151. initDefVal() {
  152. let defValue = ''
  153. if ((this.value || this.value === 0) && !this.isDisabled(this.value)) {
  154. defValue = this.value
  155. } else if ((this.modelValue || this.modelValue === 0) && !this.isDisabled(this.modelValue)) {
  156. defValue = this.modelValue
  157. } else {
  158. let strogeValue
  159. if (this.collection) {
  160. strogeValue = uni.getStorageSync(this.last)
  161. }
  162. if (strogeValue || strogeValue === 0) {
  163. defValue = strogeValue
  164. } else {
  165. let defItem = ''
  166. if (this.defItem > 0 && this.defItem < this.mixinDatacomResData.length) {
  167. defItem = this.mixinDatacomResData[this.defItem - 1].value
  168. }
  169. defValue = defItem
  170. }
  171. this.emit(defValue)
  172. }
  173. const def = this.mixinDatacomResData.find(item => item.value === defValue)
  174. this.current = def ? this.formatItemName(def) : ''
  175. },
  176. /**
  177. * @param {[String, Number]} value
  178. * 判断用户给的 value 是否同时为禁用状态
  179. */
  180. isDisabled(value) {
  181. let isDisabled = false;
  182. this.mixinDatacomResData.forEach(item => {
  183. if (item.value === value) {
  184. isDisabled = item.disable
  185. }
  186. })
  187. return isDisabled;
  188. },
  189. clearVal() {
  190. this.emit('')
  191. if (this.collection) {
  192. uni.removeStorageSync(this.last)
  193. }
  194. },
  195. change(item) {
  196. if (!item.disable) {
  197. this.showSelector = false
  198. this.current = this.formatItemName(item)
  199. this.emit(item.value)
  200. }
  201. },
  202. emit(val) {
  203. this.$emit('change', val)
  204. this.$emit('input', val)
  205. this.$emit('update:modelValue', val)
  206. if (this.collection) {
  207. uni.setStorageSync(this.last, val)
  208. }
  209. },
  210. toggleSelector() {
  211. if(this.disabled){
  212. return
  213. }
  214. this.showSelector = !this.showSelector
  215. },
  216. formatItemName(item) {
  217. let {
  218. text,
  219. value,
  220. channel_code
  221. } = item
  222. channel_code = channel_code ? `(${channel_code})` : ''
  223. return this.collection.indexOf('app-list') > 0 ?
  224. `${text}(${value})` :
  225. (
  226. text ?
  227. text :
  228. `未命名${channel_code}`
  229. )
  230. }
  231. }
  232. }
  233. </script>
  234. <style lang="scss">
  235. $uni-base-color: #6a6a6a !default;
  236. $uni-main-color: #333 !default;
  237. $uni-secondary-color: #909399 !default;
  238. $uni-border-3: #e5e5e5;
  239. /* #ifndef APP-NVUE */
  240. @media screen and (max-width: 500px) {
  241. .hide-on-phone {
  242. display: none;
  243. }
  244. }
  245. /* #endif */
  246. .uni-stat__select {
  247. display: flex;
  248. align-items: center;
  249. // padding: 15px;
  250. cursor: pointer;
  251. width: 100%;
  252. flex: 1;
  253. box-sizing: border-box;
  254. }
  255. .uni-stat-box {
  256. width: 100%;
  257. flex: 1;
  258. }
  259. .uni-stat__actived {
  260. width: 100%;
  261. flex: 1;
  262. // outline: 1px solid #2979ff;
  263. }
  264. .uni-label-text {
  265. font-size: 14px;
  266. font-weight: bold;
  267. color: $uni-base-color;
  268. margin: auto 0;
  269. margin-right: 5px;
  270. }
  271. .uni-select {
  272. font-size: 14px;
  273. border: 1px solid $uni-border-3;
  274. box-sizing: border-box;
  275. border-radius: 4px;
  276. padding: 0 5px;
  277. padding-left: 10px;
  278. position: relative;
  279. /* #ifndef APP-NVUE */
  280. display: flex;
  281. user-select: none;
  282. /* #endif */
  283. flex-direction: row;
  284. align-items: center;
  285. border-bottom: solid 1px $uni-border-3;
  286. width: 100%;
  287. flex: 1;
  288. height: 35px;
  289. &--disabled{
  290. background-color: #f5f7fa;
  291. cursor: not-allowed;
  292. }
  293. }
  294. .uni-select__label {
  295. font-size: 16px;
  296. // line-height: 22px;
  297. height: 35px;
  298. padding-right: 10px;
  299. color: $uni-secondary-color;
  300. }
  301. .uni-select__input-box {
  302. height: 35px;
  303. position: relative;
  304. /* #ifndef APP-NVUE */
  305. display: flex;
  306. /* #endif */
  307. flex: 1;
  308. flex-direction: row;
  309. align-items: center;
  310. }
  311. .uni-select__input {
  312. flex: 1;
  313. font-size: 14px;
  314. height: 22px;
  315. line-height: 22px;
  316. }
  317. .uni-select__input-plac {
  318. font-size: 14px;
  319. color: $uni-secondary-color;
  320. }
  321. .uni-select__selector {
  322. /* #ifndef APP-NVUE */
  323. box-sizing: border-box;
  324. /* #endif */
  325. position: absolute;
  326. top: calc(100% + 12px);
  327. left: 0;
  328. width: 100%;
  329. background-color: #FFFFFF;
  330. border: 1px solid #EBEEF5;
  331. border-radius: 6px;
  332. box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  333. z-index: 3;
  334. padding: 4px 0;
  335. }
  336. .uni-select__selector-scroll {
  337. /* #ifndef APP-NVUE */
  338. max-height: 200px;
  339. box-sizing: border-box;
  340. /* #endif */
  341. }
  342. .uni-select__selector-empty,
  343. .uni-select__selector-item {
  344. /* #ifndef APP-NVUE */
  345. display: flex;
  346. cursor: pointer;
  347. /* #endif */
  348. line-height: 35px;
  349. font-size: 14px;
  350. text-align: center;
  351. /* border-bottom: solid 1px $uni-border-3; */
  352. padding: 0px 10px;
  353. }
  354. .uni-select__selector-item:hover {
  355. background-color: #f9f9f9;
  356. }
  357. .uni-select__selector-empty:last-child,
  358. .uni-select__selector-item:last-child {
  359. /* #ifndef APP-NVUE */
  360. border-bottom: none;
  361. /* #endif */
  362. }
  363. .uni-select__selector__disabled {
  364. opacity: 0.4;
  365. cursor: default;
  366. }
  367. /* picker 弹出层通用的指示小三角 */
  368. .uni-popper__arrow,
  369. .uni-popper__arrow::after {
  370. position: absolute;
  371. display: block;
  372. width: 0;
  373. height: 0;
  374. border-color: transparent;
  375. border-style: solid;
  376. border-width: 6px;
  377. }
  378. .uni-popper__arrow {
  379. filter: drop-shadow(0 2px 12px rgba(0, 0, 0, 0.03));
  380. top: -6px;
  381. left: 10%;
  382. margin-right: 3px;
  383. border-top-width: 0;
  384. border-bottom-color: #EBEEF5;
  385. }
  386. .uni-popper__arrow::after {
  387. content: " ";
  388. top: 1px;
  389. margin-left: -6px;
  390. border-top-width: 0;
  391. border-bottom-color: #fff;
  392. }
  393. .uni-select__input-text {
  394. // width: 280px;
  395. width: 100%;
  396. color: $uni-main-color;
  397. white-space: nowrap;
  398. text-overflow: ellipsis;
  399. -o-text-overflow: ellipsis;
  400. overflow: hidden;
  401. }
  402. .uni-select__input-placeholder {
  403. color: $uni-base-color;
  404. font-size: 12px;
  405. }
  406. .uni-select--mask {
  407. position: fixed;
  408. top: 0;
  409. bottom: 0;
  410. right: 0;
  411. left: 0;
  412. }
  413. </style>