uni-data-select.vue 8.6 KB

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