uni-data-select.vue 9.3 KB

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