uni-data-select.vue 8.3 KB

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