爱简收旧衣按件回收前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

560 lines
14 KiB

2 months ago
  1. <template>
  2. <view class="uni-data-tree">
  3. <view class="uni-data-tree-input" @click="handleInput">
  4. <slot :options="options" :data="inputSelected" :error="errorMessage">
  5. <view class="input-value" :class="{'input-value-border': border}">
  6. <text v-if="errorMessage" class="selected-area error-text">{{errorMessage}}</text>
  7. <view v-else-if="loading && !isOpened" class="selected-area">
  8. <uni-load-more class="load-more" :contentText="loadMore" status="loading"></uni-load-more>
  9. </view>
  10. <scroll-view v-else-if="inputSelected.length" class="selected-area" scroll-x="true">
  11. <view class="selected-list">
  12. <view class="selected-item" v-for="(item,index) in inputSelected" :key="index">
  13. <text class="text-color">{{item.text}}</text><text v-if="index<inputSelected.length-1"
  14. class="input-split-line">{{split}}</text>
  15. </view>
  16. </view>
  17. </scroll-view>
  18. <text v-else class="selected-area placeholder">{{placeholder}}</text>
  19. <view v-if="clearIcon && !readonly && inputSelected.length" class="icon-clear" @click.stop="clear">
  20. <uni-icons type="clear" color="#c0c4cc" size="24"></uni-icons>
  21. </view>
  22. <view class="arrow-area" v-if="(!clearIcon || !inputSelected.length) && !readonly ">
  23. <view class="input-arrow"></view>
  24. </view>
  25. </view>
  26. </slot>
  27. </view>
  28. <view class="uni-data-tree-cover" v-if="isOpened" @click="handleClose"></view>
  29. <view class="uni-data-tree-dialog" v-if="isOpened">
  30. <view class="uni-popper__arrow"></view>
  31. <view class="dialog-caption">
  32. <view class="title-area">
  33. <text class="dialog-title">{{popupTitle}}</text>
  34. </view>
  35. <view class="dialog-close" @click="handleClose">
  36. <view class="dialog-close-plus" data-id="close"></view>
  37. <view class="dialog-close-plus dialog-close-rotate" data-id="close"></view>
  38. </view>
  39. </view>
  40. <data-picker-view class="picker-view" ref="pickerView" v-model="dataValue" :localdata="localdata"
  41. :preload="preload" :collection="collection" :field="field" :orderby="orderby" :where="where"
  42. :step-searh="stepSearh" :self-field="selfField" :parent-field="parentField" :managed-mode="true" :map="map"
  43. :ellipsis="ellipsis" @change="onchange" @datachange="ondatachange" @nodeclick="onnodeclick">
  44. </data-picker-view>
  45. </view>
  46. </view>
  47. </template>
  48. <script>
  49. import dataPicker from "../uni-data-pickerview/uni-data-picker.js"
  50. import DataPickerView from "../uni-data-pickerview/uni-data-pickerview.vue"
  51. /**
  52. * DataPicker 级联选择
  53. * @description 支持单列和多列级联选择列数没有限制如果屏幕显示不全顶部tab区域会左右滚动
  54. * @tutorial https://ext.dcloud.net.cn/plugin?id=3796
  55. * @property {String} popup-title 弹出窗口标题
  56. * @property {Array} localdata 本地数据参考
  57. * @property {Boolean} border = [true|false] 是否有边框
  58. * @property {Boolean} readonly = [true|false] 是否仅读
  59. * @property {Boolean} preload = [true|false] 是否预加载数据
  60. * @value true 开启预加载数据点击弹出窗口后显示已加载数据
  61. * @value false 关闭预加载数据点击弹出窗口后开始加载数据
  62. * @property {Boolean} step-searh = [true|false] 是否分布查询
  63. * @value true 启用分布查询仅查询当前选中节点
  64. * @value false 关闭分布查询一次查询出所有数据
  65. * @property {String|DBFieldString} self-field 分布查询当前字段名称
  66. * @property {String|DBFieldString} parent-field 分布查询父字段名称
  67. * @property {String|DBCollectionString} collection 表名
  68. * @property {String|DBFieldString} field 查询字段多个字段用 `,` 分割
  69. * @property {String} orderby 排序字段及正序倒叙设置
  70. * @property {String|JQLString} where 查询条件
  71. * @event {Function} popupshow 弹出的选择窗口打开时触发此事件
  72. * @event {Function} popuphide 弹出的选择窗口关闭时触发此事件
  73. */
  74. export default {
  75. name: 'UniDataPicker',
  76. emits: ['popupopened', 'popupclosed', 'nodeclick', 'input', 'change', 'update:modelValue','inputclick'],
  77. mixins: [dataPicker],
  78. components: {
  79. DataPickerView
  80. },
  81. props: {
  82. options: {
  83. type: [Object, Array],
  84. default () {
  85. return {}
  86. }
  87. },
  88. popupTitle: {
  89. type: String,
  90. default: '请选择'
  91. },
  92. placeholder: {
  93. type: String,
  94. default: '请选择'
  95. },
  96. heightMobile: {
  97. type: String,
  98. default: ''
  99. },
  100. readonly: {
  101. type: Boolean,
  102. default: false
  103. },
  104. clearIcon: {
  105. type: Boolean,
  106. default: true
  107. },
  108. border: {
  109. type: Boolean,
  110. default: true
  111. },
  112. split: {
  113. type: String,
  114. default: '/'
  115. },
  116. ellipsis: {
  117. type: Boolean,
  118. default: true
  119. }
  120. },
  121. data() {
  122. return {
  123. isOpened: false,
  124. inputSelected: []
  125. }
  126. },
  127. created() {
  128. this.$nextTick(() => {
  129. this.load();
  130. })
  131. },
  132. watch: {
  133. localdata: {
  134. handler() {
  135. this.load()
  136. },
  137. deep: true
  138. },
  139. },
  140. methods: {
  141. clear() {
  142. this._dispatchEvent([]);
  143. },
  144. onPropsChange() {
  145. this._treeData = [];
  146. this.selectedIndex = 0;
  147. this.load();
  148. },
  149. load() {
  150. if (this.readonly) {
  151. this._processReadonly(this.localdata, this.dataValue);
  152. return;
  153. }
  154. // 回显本地数据
  155. if (this.isLocalData) {
  156. this.loadData();
  157. this.inputSelected = this.selected.slice(0);
  158. } else if (this.isCloudDataList || this.isCloudDataTree) { // 回显 Cloud 数据
  159. this.loading = true;
  160. this.getCloudDataValue().then((res) => {
  161. this.loading = false;
  162. this.inputSelected = res;
  163. }).catch((err) => {
  164. this.loading = false;
  165. this.errorMessage = err;
  166. })
  167. }
  168. },
  169. show() {
  170. this.isOpened = true
  171. setTimeout(() => {
  172. this.$refs.pickerView.updateData({
  173. treeData: this._treeData,
  174. selected: this.selected,
  175. selectedIndex: this.selectedIndex
  176. })
  177. }, 200)
  178. this.$emit('popupopened')
  179. },
  180. hide() {
  181. this.isOpened = false
  182. this.$emit('popupclosed')
  183. },
  184. handleInput() {
  185. if (this.readonly) {
  186. this.$emit('inputclick')
  187. return
  188. }
  189. this.show()
  190. },
  191. handleClose(e) {
  192. this.hide()
  193. },
  194. onnodeclick(e) {
  195. this.$emit('nodeclick', e)
  196. },
  197. ondatachange(e) {
  198. this._treeData = this.$refs.pickerView._treeData
  199. },
  200. onchange(e) {
  201. this.hide()
  202. this.$nextTick(() => {
  203. this.inputSelected = e;
  204. })
  205. this._dispatchEvent(e)
  206. },
  207. _processReadonly(dataList, value) {
  208. var isTree = dataList.findIndex((item) => {
  209. return item.children
  210. })
  211. if (isTree > -1) {
  212. let inputValue
  213. if (Array.isArray(value)) {
  214. inputValue = value[value.length - 1]
  215. if (typeof inputValue === 'object' && inputValue.value) {
  216. inputValue = inputValue.value
  217. }
  218. } else {
  219. inputValue = value
  220. }
  221. this.inputSelected = this._findNodePath(inputValue, this.localdata)
  222. return
  223. }
  224. if (!this.hasValue) {
  225. this.inputSelected = []
  226. return
  227. }
  228. let result = []
  229. if (Array.isArray(value)) {
  230. for (let i = 0; i < value.length; i++) {
  231. var val = value[i]
  232. var item = dataList.find((v) => {
  233. return v.value == val
  234. })
  235. if (item) {
  236. result.push(item)
  237. }
  238. }
  239. } else {
  240. let item = dataList.find((v) => {
  241. return v.value == value;
  242. });
  243. if (item) {
  244. result.push(item);
  245. }
  246. }
  247. if (result.length) {
  248. this.inputSelected = result
  249. }
  250. },
  251. _filterForArray(data, valueArray) {
  252. var result = []
  253. for (let i = 0; i < valueArray.length; i++) {
  254. var value = valueArray[i]
  255. var found = data.find((item) => {
  256. return item.value == value
  257. })
  258. if (found) {
  259. result.push(found)
  260. }
  261. }
  262. return result
  263. },
  264. _dispatchEvent(selected) {
  265. let item = {}
  266. if (selected.length) {
  267. var value = new Array(selected.length)
  268. for (var i = 0; i < selected.length; i++) {
  269. value[i] = selected[i].value
  270. }
  271. item = selected[selected.length - 1]
  272. } else {
  273. item.value = ''
  274. }
  275. if (this.formItem) {
  276. this.formItem.setValue(item.value)
  277. }
  278. this.$emit('input', item.value)
  279. this.$emit('update:modelValue', item.value)
  280. this.$emit('change', {
  281. detail: {
  282. value: selected
  283. }
  284. })
  285. }
  286. }
  287. }
  288. </script>
  289. <style>
  290. .uni-data-tree {
  291. flex: 1;
  292. position: relative;
  293. font-size: 14px;
  294. }
  295. .error-text {
  296. color: #DD524D;
  297. }
  298. .input-value {
  299. /* #ifndef APP-NVUE */
  300. display: flex;
  301. /* #endif */
  302. flex-direction: row;
  303. align-items: center;
  304. flex-wrap: nowrap;
  305. font-size: 14px;
  306. /* line-height: 35px; */
  307. padding: 0 10px;
  308. padding-right: 5px;
  309. overflow: hidden;
  310. height: 35px;
  311. /* #ifndef APP-NVUE */
  312. box-sizing: border-box;
  313. /* #endif */
  314. }
  315. .input-value-border {
  316. border: 1px solid #e5e5e5;
  317. border-radius: 5px;
  318. }
  319. .selected-area {
  320. flex: 1;
  321. overflow: hidden;
  322. /* #ifndef APP-NVUE */
  323. display: flex;
  324. /* #endif */
  325. flex-direction: row;
  326. }
  327. .load-more {
  328. /* #ifndef APP-NVUE */
  329. margin-right: auto;
  330. /* #endif */
  331. /* #ifdef APP-NVUE */
  332. width: 40px;
  333. /* #endif */
  334. }
  335. .selected-list {
  336. /* #ifndef APP-NVUE */
  337. display: flex;
  338. /* #endif */
  339. flex-direction: row;
  340. flex-wrap: nowrap;
  341. /* padding: 0 5px; */
  342. }
  343. .selected-item {
  344. flex-direction: row;
  345. /* padding: 0 1px; */
  346. /* #ifndef APP-NVUE */
  347. white-space: nowrap;
  348. /* #endif */
  349. }
  350. .text-color {
  351. color: #333;
  352. }
  353. .placeholder {
  354. color: grey;
  355. font-size: 12px;
  356. }
  357. .input-split-line {
  358. opacity: .5;
  359. }
  360. .arrow-area {
  361. position: relative;
  362. width: 20px;
  363. /* #ifndef APP-NVUE */
  364. margin-bottom: 5px;
  365. margin-left: auto;
  366. display: flex;
  367. /* #endif */
  368. justify-content: center;
  369. transform: rotate(-45deg);
  370. transform-origin: center;
  371. }
  372. .input-arrow {
  373. width: 7px;
  374. height: 7px;
  375. border-left: 1px solid #999;
  376. border-bottom: 1px solid #999;
  377. }
  378. .uni-data-tree-cover {
  379. position: fixed;
  380. left: 0;
  381. top: 0;
  382. right: 0;
  383. bottom: 0;
  384. background-color: rgba(0, 0, 0, .4);
  385. /* #ifndef APP-NVUE */
  386. display: flex;
  387. /* #endif */
  388. flex-direction: column;
  389. z-index: 100;
  390. }
  391. .uni-data-tree-dialog {
  392. position: fixed;
  393. left: 0;
  394. /* #ifndef APP-NVUE */
  395. top: 20%;
  396. /* #endif */
  397. /* #ifdef APP-NVUE */
  398. top: 200px;
  399. /* #endif */
  400. right: 0;
  401. bottom: 0;
  402. background-color: #FFFFFF;
  403. border-top-left-radius: 10px;
  404. border-top-right-radius: 10px;
  405. /* #ifndef APP-NVUE */
  406. display: flex;
  407. /* #endif */
  408. flex-direction: column;
  409. z-index: 102;
  410. overflow: hidden;
  411. /* #ifdef APP-NVUE */
  412. width: 750rpx;
  413. /* #endif */
  414. }
  415. .dialog-caption {
  416. position: relative;
  417. /* #ifndef APP-NVUE */
  418. display: flex;
  419. /* #endif */
  420. flex-direction: row;
  421. /* border-bottom: 1px solid #f0f0f0; */
  422. }
  423. .title-area {
  424. /* #ifndef APP-NVUE */
  425. display: flex;
  426. /* #endif */
  427. align-items: center;
  428. /* #ifndef APP-NVUE */
  429. margin: auto;
  430. /* #endif */
  431. padding: 0 10px;
  432. }
  433. .dialog-title {
  434. /* font-weight: bold; */
  435. line-height: 44px;
  436. }
  437. .dialog-close {
  438. position: absolute;
  439. top: 0;
  440. right: 0;
  441. bottom: 0;
  442. /* #ifndef APP-NVUE */
  443. display: flex;
  444. /* #endif */
  445. flex-direction: row;
  446. align-items: center;
  447. padding: 0 15px;
  448. }
  449. .dialog-close-plus {
  450. width: 16px;
  451. height: 2px;
  452. background-color: #666;
  453. border-radius: 2px;
  454. transform: rotate(45deg);
  455. }
  456. .dialog-close-rotate {
  457. position: absolute;
  458. transform: rotate(-45deg);
  459. }
  460. .picker-view {
  461. flex: 1;
  462. overflow: hidden;
  463. }
  464. .icon-clear {
  465. display: flex;
  466. align-items: center;
  467. }
  468. /* #ifdef H5 */
  469. @media all and (min-width: 768px) {
  470. .uni-data-tree-cover {
  471. background-color: transparent;
  472. }
  473. .uni-data-tree-dialog {
  474. position: absolute;
  475. top: 55px;
  476. height: auto;
  477. min-height: 400px;
  478. max-height: 50vh;
  479. background-color: #fff;
  480. border: 1px solid #EBEEF5;
  481. box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  482. border-radius: 4px;
  483. overflow: unset;
  484. }
  485. .dialog-caption {
  486. display: none;
  487. }
  488. .icon-clear {
  489. /* margin-right: 5px; */
  490. }
  491. }
  492. /* #endif */
  493. /* picker 弹出层通用的指示小三角, todo:扩展至上下左右方向定位 */
  494. /* #ifndef APP-NVUE */
  495. .uni-popper__arrow,
  496. .uni-popper__arrow::after {
  497. position: absolute;
  498. display: block;
  499. width: 0;
  500. height: 0;
  501. border-color: transparent;
  502. border-style: solid;
  503. border-width: 6px;
  504. }
  505. .uni-popper__arrow {
  506. filter: drop-shadow(0 2px 12px rgba(0, 0, 0, 0.03));
  507. top: -6px;
  508. left: 10%;
  509. margin-right: 3px;
  510. border-top-width: 0;
  511. border-bottom-color: #EBEEF5;
  512. }
  513. .uni-popper__arrow::after {
  514. content: " ";
  515. top: 1px;
  516. margin-left: -6px;
  517. border-top-width: 0;
  518. border-bottom-color: #fff;
  519. }
  520. /* #endif */
  521. </style>