uni-data-select.vue 9.1 KB

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