1
0

mySelectTree.vue 12 KB

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