uni-data-select.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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',{'studentStatus':selectName=='studentStatus'}]">
  8. <!-- 学生名单状态 -->
  9. <template v-if="selectName=='studentStatus'">
  10. <view class="status_icon normal_icon" v-if="current == '正常'"></view>
  11. <view class="status_icon not_included_icon" v-if="current == '不计入'"></view>
  12. <view class="status_icon no_release_icon" v-if="current == '缺考'"></view>
  13. <view class="status_icon ready_close_icon" v-if="current == '违纪'"></view>
  14. <text class="input_text">{{textShow}}</text>
  15. </template>
  16. <template v-else>{{textShow}}</template>
  17. </view>
  18. <view v-else class="uni-select__input-text uni-select__input-placeholder">{{typePlaceholder}}</view>
  19. <view key="clear-button" v-if="current && clear && !disabled" @click.stop="clearVal">
  20. <uni-icons type="clear" color="#c0c4cc" size="24" />
  21. </view>
  22. <view key="arrow-button" v-else>
  23. <uni-icons :type="showSelector? 'top' : 'bottom'" size="14" color="#999" />
  24. </view>
  25. </view>
  26. <view class="uni-select--mask" v-if="showSelector" @click="toggleSelector" />
  27. <view class="uni-select__selector" :style="getOffsetByPlacement" v-if="showSelector">
  28. <view :class="placement=='bottom'?'uni-popper__arrow_bottom':'uni-popper__arrow_top'"></view>
  29. <scroll-view scroll-y="true" class="uni-select__selector-scroll">
  30. <view class="uni-select__selector-empty" v-if="mixinDatacomResData.length === 0">
  31. <text>{{emptyTips}}</text>
  32. </view>
  33. <view v-else :class="['uni-select__selector-item',{'uni-select__selector-item_active':current===item.text}]" v-for="(item,index) in mixinDatacomResData" :key="index"
  34. @click="change(item)">
  35. <view class="uni-select__selector-item_text">
  36. <!-- 学生名单状态 -->
  37. <template v-if="selectName=='studentStatus'">
  38. <view class="status_icon normal_icon" v-if="item.value == '1'"></view>
  39. <view class="status_icon not_included_icon" v-if="item.value == '3'"></view>
  40. <view class="status_icon no_release_icon" v-if="item.value == '0'"></view>
  41. <view class="status_icon ready_close_icon" v-if="item.value == '2'"></view>
  42. </template>
  43. <text :class="['uni-select__selector_text',{'uni-select__selector__disabled': item.disable}]">{{formatItemName(item)}}</text>
  44. </view>
  45. <uni-icons v-if="current===item.text" type="checkmarkempty" color="#2E64FA" style="font-size: 28rpx;" />
  46. </view>
  47. </scroll-view>
  48. </view>
  49. </view>
  50. </view>
  51. </view>
  52. </template>
  53. <script>
  54. import uniIcons from '@/uni_modules/uni-icons/components/uni-icons/uni-icons.vue';
  55. /**
  56. * DataChecklist 数据选择器
  57. * @description 通过数据渲染的下拉框组件
  58. * @tutorial https://uniapp.dcloud.io/component/uniui/uni-data-select
  59. * @property {String} value 默认值
  60. * @property {Array} localdata 本地数据 ,格式 [{text:'',value:''}]
  61. * @property {Boolean} clear 是否可以清空已选项
  62. * @property {Boolean} emptyText 没有数据时显示的文字 ,本地数据无效
  63. * @property {String} label 左侧标题
  64. * @property {String} placeholder 输入框的提示文字
  65. * @property {Boolean} disabled 是否禁用
  66. * @property {String} placement 弹出位置
  67. * @property {String} selectName 下拉框名称 2025.8.1 新增 处理下拉框样式
  68. * @value top 顶部弹出
  69. * @value bottom 底部弹出(default)
  70. * @event {Function} change 选中发生变化触发
  71. */
  72. export default {
  73. name: "uni-data-select",
  74. mixins: [uniCloud.mixinDatacom || {}],
  75. props: {
  76. localdata: {
  77. type: Array,
  78. default () {
  79. return []
  80. }
  81. },
  82. value: {
  83. type: [String, Number],
  84. default: ''
  85. },
  86. modelValue: {
  87. type: [String, Number],
  88. default: ''
  89. },
  90. label: {
  91. type: String,
  92. default: ''
  93. },
  94. placeholder: {
  95. type: String,
  96. default: '请选择'
  97. },
  98. emptyTips: {
  99. type: String,
  100. default: '无选项'
  101. },
  102. clear: {
  103. type: Boolean,
  104. default: true
  105. },
  106. defItem: {
  107. type: Number,
  108. default: 0
  109. },
  110. disabled: {
  111. type: Boolean,
  112. default: false
  113. },
  114. // 格式化输出 用法 field="_id as value, version as text, uni_platform as label" format="{label} - {text}"
  115. format: {
  116. type: String,
  117. default: ''
  118. },
  119. placement: {
  120. type: String,
  121. default: 'bottom'
  122. },
  123. selectName: {
  124. type: String,
  125. default: ''
  126. },
  127. },
  128. data() {
  129. return {
  130. showSelector: false,
  131. current: '',
  132. mixinDatacomResData: [],
  133. apps: [],
  134. channels: [],
  135. cacheKey: "uni-data-select-lastSelectedValue",
  136. };
  137. },
  138. created() {
  139. this.debounceGet = this.debounce(() => {
  140. this.query();
  141. }, 300);
  142. if (this.collection && !this.localdata.length) {
  143. this.debounceGet();
  144. }
  145. },
  146. computed: {
  147. typePlaceholder() {
  148. const text = {
  149. 'opendb-stat-app-versions': '版本',
  150. 'opendb-app-channels': '渠道',
  151. 'opendb-app-list': '应用'
  152. }
  153. const common = this.placeholder
  154. const placeholder = text[this.collection]
  155. return placeholder ?
  156. common + placeholder :
  157. common
  158. },
  159. valueCom() {
  160. // #ifdef VUE3
  161. return this.modelValue;
  162. // #endif
  163. // #ifndef VUE3
  164. return this.value;
  165. // #endif
  166. },
  167. textShow() {
  168. // 长文本显示
  169. let text = this.current;
  170. return text;
  171. },
  172. getOffsetByPlacement() {
  173. switch (this.placement) {
  174. case 'top':
  175. return "bottom:calc(100% + 12px);";
  176. case 'bottom':
  177. return "top:calc(100% + 12px);";
  178. }
  179. }
  180. },
  181. watch: {
  182. localdata: {
  183. immediate: true,
  184. handler(val, old) {
  185. if (Array.isArray(val) && old !== val) {
  186. this.mixinDatacomResData = val
  187. }
  188. }
  189. },
  190. valueCom(val, old) {
  191. this.initDefVal()
  192. },
  193. mixinDatacomResData: {
  194. immediate: true,
  195. handler(val) {
  196. if (val.length) {
  197. this.initDefVal()
  198. }
  199. }
  200. },
  201. },
  202. methods: {
  203. debounce(fn, time = 100) {
  204. let timer = null
  205. return function(...args) {
  206. if (timer) clearTimeout(timer)
  207. timer = setTimeout(() => {
  208. fn.apply(this, args)
  209. }, time)
  210. }
  211. },
  212. // 执行数据库查询
  213. query() {
  214. this.mixinDatacomEasyGet();
  215. },
  216. // 监听查询条件变更事件
  217. onMixinDatacomPropsChange() {
  218. if (this.collection) {
  219. this.debounceGet();
  220. }
  221. },
  222. initDefVal() {
  223. let defValue = ''
  224. if ((this.valueCom || this.valueCom === 0) && !this.isDisabled(this.valueCom)) {
  225. defValue = this.valueCom
  226. } else {
  227. let strogeValue
  228. if (this.collection) {
  229. strogeValue = this.getCache()
  230. }
  231. if (strogeValue || strogeValue === 0) {
  232. defValue = strogeValue
  233. } else {
  234. let defItem = ''
  235. if (this.defItem > 0 && this.defItem <= this.mixinDatacomResData.length) {
  236. defItem = this.mixinDatacomResData[this.defItem - 1].value
  237. }
  238. defValue = defItem
  239. }
  240. if (defValue || defValue === 0) {
  241. this.emit(defValue)
  242. }
  243. }
  244. const def = this.mixinDatacomResData.find(item => item.value === defValue)
  245. this.current = def ? this.formatItemName(def) : ''
  246. },
  247. /**
  248. * @param {[String, Number]} value
  249. * 判断用户给的 value 是否同时为禁用状态
  250. */
  251. isDisabled(value) {
  252. let isDisabled = false;
  253. this.mixinDatacomResData.forEach(item => {
  254. if (item.value === value) {
  255. isDisabled = item.disable
  256. }
  257. })
  258. return isDisabled;
  259. },
  260. clearVal() {
  261. this.emit('')
  262. this.current = ''
  263. if (this.collection) {
  264. this.removeCache()
  265. }
  266. },
  267. change(item) {
  268. if (!item.disable) {
  269. this.showSelector = false
  270. this.current = this.formatItemName(item)
  271. this.emit(item.value)
  272. }
  273. },
  274. emit(val) {
  275. this.$emit('input', val)
  276. this.$emit('update:modelValue', val)
  277. this.$emit('change', val)
  278. if (this.collection) {
  279. this.setCache(val);
  280. }
  281. },
  282. toggleSelector() {
  283. if (this.disabled) {
  284. return
  285. }
  286. this.showSelector = !this.showSelector
  287. },
  288. formatItemName(item) {
  289. let {
  290. text,
  291. value,
  292. channel_code
  293. } = item
  294. channel_code = channel_code ? `(${channel_code})` : ''
  295. if (this.format) {
  296. // 格式化输出
  297. let str = "";
  298. str = this.format;
  299. for (let key in item) {
  300. str = str.replace(new RegExp(`{${key}}`, "g"), item[key]);
  301. }
  302. return str;
  303. } else {
  304. return this.collection.indexOf('app-list') > 0 ?
  305. `${text}(${value})` :
  306. (
  307. text ?
  308. text :
  309. `未命名${channel_code}`
  310. )
  311. }
  312. },
  313. // 获取当前加载的数据
  314. getLoadData() {
  315. return this.mixinDatacomResData;
  316. },
  317. // 获取当前缓存key
  318. getCurrentCacheKey() {
  319. return this.collection;
  320. },
  321. // 获取缓存
  322. getCache(name = this.getCurrentCacheKey()) {
  323. let cacheData = uni.getStorageSync(this.cacheKey) || {};
  324. return cacheData[name];
  325. },
  326. // 设置缓存
  327. setCache(value, name = this.getCurrentCacheKey()) {
  328. let cacheData = uni.getStorageSync(this.cacheKey) || {};
  329. cacheData[name] = value;
  330. uni.setStorageSync(this.cacheKey, cacheData);
  331. },
  332. // 删除缓存
  333. removeCache(name = this.getCurrentCacheKey()) {
  334. let cacheData = uni.getStorageSync(this.cacheKey) || {};
  335. delete cacheData[name];
  336. uni.setStorageSync(this.cacheKey, cacheData);
  337. },
  338. }
  339. }
  340. </script>
  341. <style lang="scss">
  342. $uni-base-color: #6a6a6a !default;
  343. $uni-main-color: #333 !default;
  344. $uni-secondary-color: #909399 !default;
  345. $uni-border-3: #e5e5e5;
  346. /* #ifndef APP-NVUE */
  347. @media screen and (max-width: 500px) {
  348. .hide-on-phone {
  349. display: none;
  350. }
  351. }
  352. /* #endif */
  353. .uni-stat__select {
  354. display: flex;
  355. align-items: center;
  356. // padding: 15px;
  357. /* #ifdef H5 */
  358. cursor: pointer;
  359. /* #endif */
  360. width: 100%;
  361. flex: 1;
  362. box-sizing: border-box;
  363. }
  364. .uni-stat-box {
  365. background-color: #fff;
  366. width: 100%;
  367. flex: 1;
  368. }
  369. .uni-stat__actived {
  370. width: 100%;
  371. flex: 1;
  372. // outline: 1px solid #2979ff;
  373. }
  374. .uni-label-text {
  375. font-size: 14px;
  376. font-weight: bold;
  377. color: $uni-base-color;
  378. margin: auto 0;
  379. margin-right: 5px;
  380. }
  381. .uni-select {
  382. font-size: 14px;
  383. border: 1px solid $uni-border-3;
  384. box-sizing: border-box;
  385. border-radius: 4px;
  386. padding: 0 5px;
  387. padding-left: 10px;
  388. position: relative;
  389. /* #ifndef APP-NVUE */
  390. display: flex;
  391. user-select: none;
  392. /* #endif */
  393. flex-direction: row;
  394. align-items: center;
  395. border-bottom: solid 1px $uni-border-3;
  396. width: 100%;
  397. flex: 1;
  398. height: 35px;
  399. &--disabled {
  400. background-color: #f5f7fa;
  401. cursor: not-allowed;
  402. }
  403. }
  404. .uni-select__label {
  405. font-size: 16px;
  406. // line-height: 22px;
  407. height: 35px;
  408. padding-right: 10px;
  409. color: $uni-secondary-color;
  410. }
  411. .uni-select__input-box {
  412. height: 35px;
  413. width: 0px;
  414. position: relative;
  415. /* #ifndef APP-NVUE */
  416. display: flex;
  417. /* #endif */
  418. flex: 1;
  419. flex-direction: row;
  420. align-items: center;
  421. }
  422. .uni-select__input {
  423. flex: 1;
  424. font-size: 14px;
  425. height: 22px;
  426. line-height: 22px;
  427. }
  428. .uni-select__input-plac {
  429. font-size: 14px;
  430. color: $uni-secondary-color;
  431. }
  432. .uni-select__selector {
  433. /* #ifndef APP-NVUE */
  434. box-sizing: border-box;
  435. /* #endif */
  436. position: absolute;
  437. left: 0;
  438. width: 100%;
  439. background-color: #FFFFFF;
  440. border: 1px solid #EBEEF5;
  441. border-radius: 6px;
  442. box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  443. z-index: 3;
  444. padding: 4px 0;
  445. }
  446. .uni-select__selector-scroll {
  447. /* #ifndef APP-NVUE */
  448. max-height: 200px;
  449. box-sizing: border-box;
  450. /* #endif */
  451. }
  452. /* #ifdef H5 */
  453. @media (min-width: 768px) {
  454. .uni-select__selector-scroll {
  455. max-height: 600px;
  456. }
  457. }
  458. /* #endif */
  459. .uni-select__selector-empty,
  460. .uni-select__selector-item {
  461. /* #ifndef APP-NVUE */
  462. display: flex;
  463. cursor: pointer;
  464. /* #endif */
  465. line-height: 35px;
  466. font-size: 14px;
  467. text-align: center;
  468. /* border-bottom: solid 1px $uni-border-3; */
  469. padding: 0px 10px;
  470. }
  471. .uni-select__selector-item{
  472. justify-content: space-between;
  473. align-items: center;
  474. }
  475. .uni-select__selector-item_text{
  476. flex:1;
  477. overflow: hidden;
  478. text-align: left;
  479. display: flex;
  480. align-items: center;
  481. .status_icon{
  482. width:16rpx;
  483. height:16rpx;
  484. border-radius: 50%;
  485. margin-right:20rpx;
  486. flex-shrink: 0;
  487. &.normal_icon {
  488. background: #2bc644;
  489. }
  490. &.not_included_icon {
  491. background: #C0C4CC;
  492. }
  493. &.no_release_icon{
  494. background: #fb9f34;
  495. }
  496. &.ready_close_icon{
  497. background: #f56c6c;
  498. }
  499. }
  500. }
  501. .uni-select__selector_text{
  502. flex:1;
  503. white-space: nowrap;
  504. overflow: hidden;
  505. text-overflow: ellipsis;
  506. text-align: left;
  507. }
  508. .uni-select__selector-item_active{
  509. color: #2E64FA !important;
  510. font-weight: 400;
  511. }
  512. .uni-select__selector-item:hover {
  513. background-color: #f9f9f9;
  514. }
  515. .uni-select__selector-empty:last-child,
  516. .uni-select__selector-item:last-child {
  517. /* #ifndef APP-NVUE */
  518. border-bottom: none;
  519. /* #endif */
  520. }
  521. .uni-select__selector__disabled {
  522. opacity: 0.4;
  523. cursor: default;
  524. }
  525. /* picker 弹出层通用的指示小三角 */
  526. .uni-popper__arrow_bottom,
  527. .uni-popper__arrow_bottom::after,
  528. .uni-popper__arrow_top,
  529. .uni-popper__arrow_top::after,
  530. {
  531. position: absolute;
  532. display: block;
  533. width: 0;
  534. height: 0;
  535. border-color: transparent;
  536. border-style: solid;
  537. border-width: 6px;
  538. }
  539. .uni-popper__arrow_bottom {
  540. filter: drop-shadow(0 2px 12px rgba(0, 0, 0, 0.03));
  541. top: -6px;
  542. left: 10%;
  543. margin-right: 3px;
  544. border-top-width: 0;
  545. border-bottom-color: #EBEEF5;
  546. }
  547. .uni-popper__arrow_bottom::after {
  548. content: " ";
  549. top: 1px;
  550. margin-left: -6px;
  551. border-top-width: 0;
  552. border-bottom-color: #fff;
  553. }
  554. .uni-popper__arrow_top {
  555. filter: drop-shadow(0 2px 12px rgba(0, 0, 0, 0.03));
  556. bottom: -6px;
  557. left: 10%;
  558. margin-right: 3px;
  559. border-bottom-width: 0;
  560. border-top-color: #EBEEF5;
  561. }
  562. .uni-popper__arrow_top::after {
  563. content: " ";
  564. bottom: 1px;
  565. margin-left: -6px;
  566. border-bottom-width: 0;
  567. border-top-color: #fff;
  568. }
  569. .uni-select__input-text {
  570. // width: 280px;
  571. width: 100%;
  572. color: $uni-main-color;
  573. white-space: nowrap;
  574. text-overflow: ellipsis;
  575. -o-text-overflow: ellipsis;
  576. overflow: hidden;
  577. &.studentStatus{
  578. display: flex;
  579. align-items: center;
  580. }
  581. .status_icon{
  582. width:16rpx;
  583. height:16rpx;
  584. border-radius: 50%;
  585. margin-right:10rpx;
  586. flex-shrink: 0;
  587. &.normal_icon {
  588. background: #2bc644;
  589. }
  590. &.not_included_icon {
  591. background: #C0C4CC;
  592. }
  593. &.no_release_icon{
  594. background: #fb9f34;
  595. }
  596. &.ready_close_icon{
  597. background: #f56c6c;
  598. }
  599. }
  600. }
  601. .uni-select__input-placeholder {
  602. color: $uni-base-color;
  603. font-size: 12px;
  604. }
  605. .uni-select--mask {
  606. position: fixed;
  607. top: 0;
  608. bottom: 0;
  609. right: 0;
  610. left: 0;
  611. z-index: 2;
  612. }
  613. </style>