uni-data-picker.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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'],
  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. methods: {
  133. clear() {
  134. this.modelValue = null;
  135. this._dispatchEvent([]);
  136. },
  137. onPropsChange() {
  138. this._treeData = [];
  139. this.selectedIndex = 0;
  140. this.load();
  141. },
  142. load() {
  143. if (this.readonly) {
  144. this._processReadonly(this.localdata, this.dataValue);
  145. return;
  146. }
  147. if (!this.hasValue) {
  148. return;
  149. }
  150. // 回显本地数据
  151. if (this.isLocalData) {
  152. this.loadData();
  153. this.inputSelected = this.selected.slice(0);
  154. } else if (this.isCloudDataList || this.isCloudDataTree) { // 回显 Cloud 数据
  155. this.loading = true;
  156. this.getCloudDataValue().then((res) => {
  157. this.loading = false;
  158. this.inputSelected = res;
  159. }).catch((err) => {
  160. this.loading = false;
  161. this.errorMessage = err;
  162. })
  163. }
  164. },
  165. show() {
  166. this.isOpened = true
  167. setTimeout(() => {
  168. this.$refs.pickerView.updateData({
  169. treeData: this._treeData,
  170. selected: this.selected,
  171. selectedIndex: this.selectedIndex
  172. })
  173. }, 200)
  174. this.$emit('popupopened')
  175. },
  176. hide() {
  177. this.isOpened = false
  178. this.$emit('popupclosed')
  179. },
  180. handleInput() {
  181. if (this.readonly) {
  182. return
  183. }
  184. this.show()
  185. },
  186. handleClose(e) {
  187. this.hide()
  188. },
  189. onnodeclick(e) {
  190. this.$emit('nodeclick', e)
  191. },
  192. ondatachange(e) {
  193. this._treeData = this.$refs.pickerView._treeData
  194. },
  195. onchange(e) {
  196. this.hide()
  197. this.$nextTick(() => {
  198. this.inputSelected = e;
  199. })
  200. this._dispatchEvent(e)
  201. },
  202. _processReadonly(dataList, value) {
  203. var isTree = dataList.findIndex((item) => {
  204. return item.children
  205. })
  206. if (isTree > -1) {
  207. let inputValue
  208. if (Array.isArray(value)) {
  209. inputValue = value[value.length - 1]
  210. if (typeof inputValue === 'object' && inputValue.value) {
  211. inputValue = inputValue.value
  212. }
  213. } else {
  214. inputValue = value
  215. }
  216. this.inputSelected = this._findNodePath(inputValue, this.localdata)
  217. return
  218. }
  219. if (!this.hasValue) {
  220. this.inputSelected = []
  221. return
  222. }
  223. let result = []
  224. for (let i = 0; i < value.length; i++) {
  225. var val = value[i]
  226. var item = dataList.find((v) => {
  227. return v.value == val
  228. })
  229. if (item) {
  230. result.push(item)
  231. }
  232. }
  233. if (result.length) {
  234. this.inputSelected = result
  235. }
  236. },
  237. _filterForArray(data, valueArray) {
  238. var result = []
  239. for (let i = 0; i < valueArray.length; i++) {
  240. var value = valueArray[i]
  241. var found = data.find((item) => {
  242. return item.value == value
  243. })
  244. if (found) {
  245. result.push(found)
  246. }
  247. }
  248. return result
  249. },
  250. _dispatchEvent(selected) {
  251. let item = {}
  252. if (selected.length) {
  253. var value = new Array(selected.length)
  254. for (var i = 0; i < selected.length; i++) {
  255. value[i] = selected[i].value
  256. }
  257. item = selected[selected.length - 1]
  258. } else {
  259. item.value = ''
  260. }
  261. if (this.formItem) {
  262. this.formItem.setValue(item.value)
  263. }
  264. this.$emit('input', item.value)
  265. this.$emit('update:modelValue', item.value)
  266. this.$emit('change', {
  267. detail: {
  268. value: selected
  269. }
  270. })
  271. }
  272. }
  273. }
  274. </script>
  275. <style>
  276. .uni-data-tree {
  277. flex: 1;
  278. position: relative;
  279. font-size: 14px;
  280. }
  281. .error-text {
  282. color: #DD524D;
  283. }
  284. .input-value {
  285. /* #ifndef APP-NVUE */
  286. display: flex;
  287. /* #endif */
  288. flex-direction: row;
  289. align-items: center;
  290. flex-wrap: nowrap;
  291. font-size: 14px;
  292. /* line-height: 35px; */
  293. padding: 0 10px;
  294. padding-right: 5px;
  295. overflow: hidden;
  296. height: 35px;
  297. /* #ifndef APP-NVUE */
  298. box-sizing: border-box;
  299. /* #endif */
  300. }
  301. .input-value-border {
  302. border: 1px solid #e5e5e5;
  303. border-radius: 5px;
  304. }
  305. .selected-area {
  306. flex: 1;
  307. overflow: hidden;
  308. /* #ifndef APP-NVUE */
  309. display: flex;
  310. /* #endif */
  311. flex-direction: row;
  312. }
  313. .load-more {
  314. /* #ifndef APP-NVUE */
  315. margin-right: auto;
  316. /* #endif */
  317. /* #ifdef APP-NVUE */
  318. width: 40px;
  319. /* #endif */
  320. }
  321. .selected-list {
  322. /* #ifndef APP-NVUE */
  323. display: flex;
  324. /* #endif */
  325. flex-direction: row;
  326. flex-wrap: nowrap;
  327. /* padding: 0 5px; */
  328. }
  329. .selected-item {
  330. flex-direction: row;
  331. /* padding: 0 1px; */
  332. /* #ifndef APP-NVUE */
  333. white-space: nowrap;
  334. /* #endif */
  335. }
  336. .text-color {
  337. color: #333;
  338. }
  339. .placeholder {
  340. color: grey;
  341. font-size: 12px;
  342. }
  343. .input-split-line {
  344. opacity: .5;
  345. }
  346. .arrow-area {
  347. position: relative;
  348. width: 20px;
  349. /* #ifndef APP-NVUE */
  350. margin-bottom: 5px;
  351. margin-left: auto;
  352. display: flex;
  353. /* #endif */
  354. justify-content: center;
  355. transform: rotate(-45deg);
  356. transform-origin: center;
  357. }
  358. .input-arrow {
  359. width: 7px;
  360. height: 7px;
  361. border-left: 1px solid #999;
  362. border-bottom: 1px solid #999;
  363. }
  364. .uni-data-tree-cover {
  365. position: fixed;
  366. left: 0;
  367. top: 0;
  368. right: 0;
  369. bottom: 0;
  370. background-color: rgba(0, 0, 0, .4);
  371. /* #ifndef APP-NVUE */
  372. display: flex;
  373. /* #endif */
  374. flex-direction: column;
  375. z-index: 100;
  376. }
  377. .uni-data-tree-dialog {
  378. position: fixed;
  379. left: 0;
  380. /* #ifndef APP-NVUE */
  381. top: 20%;
  382. /* #endif */
  383. /* #ifdef APP-NVUE */
  384. top: 200px;
  385. /* #endif */
  386. right: 0;
  387. bottom: 0;
  388. background-color: #FFFFFF;
  389. border-top-left-radius: 10px;
  390. border-top-right-radius: 10px;
  391. /* #ifndef APP-NVUE */
  392. display: flex;
  393. /* #endif */
  394. flex-direction: column;
  395. z-index: 102;
  396. overflow: hidden;
  397. /* #ifdef APP-NVUE */
  398. width: 750rpx;
  399. /* #endif */
  400. }
  401. .dialog-caption {
  402. position: relative;
  403. /* #ifndef APP-NVUE */
  404. display: flex;
  405. /* #endif */
  406. flex-direction: row;
  407. /* border-bottom: 1px solid #f0f0f0; */
  408. }
  409. .title-area {
  410. /* #ifndef APP-NVUE */
  411. display: flex;
  412. /* #endif */
  413. align-items: center;
  414. /* #ifndef APP-NVUE */
  415. margin: auto;
  416. /* #endif */
  417. padding: 0 10px;
  418. }
  419. .dialog-title {
  420. /* font-weight: bold; */
  421. line-height: 44px;
  422. }
  423. .dialog-close {
  424. position: absolute;
  425. top: 0;
  426. right: 0;
  427. bottom: 0;
  428. /* #ifndef APP-NVUE */
  429. display: flex;
  430. /* #endif */
  431. flex-direction: row;
  432. align-items: center;
  433. padding: 0 15px;
  434. }
  435. .dialog-close-plus {
  436. width: 16px;
  437. height: 2px;
  438. background-color: #666;
  439. border-radius: 2px;
  440. transform: rotate(45deg);
  441. }
  442. .dialog-close-rotate {
  443. position: absolute;
  444. transform: rotate(-45deg);
  445. }
  446. .picker-view {
  447. flex: 1;
  448. overflow: hidden;
  449. }
  450. .icon-clear {
  451. display: flex;
  452. align-items: center;
  453. }
  454. /* #ifdef H5 */
  455. @media all and (min-width: 768px) {
  456. .uni-data-tree-cover {
  457. background-color: transparent;
  458. }
  459. .uni-data-tree-dialog {
  460. position: absolute;
  461. top: 55px;
  462. height: auto;
  463. min-height: 400px;
  464. max-height: 50vh;
  465. background-color: #fff;
  466. border: 1px solid #EBEEF5;
  467. box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  468. border-radius: 4px;
  469. overflow: unset;
  470. }
  471. .dialog-caption {
  472. display: none;
  473. }
  474. .icon-clear {
  475. /* margin-right: 5px; */
  476. }
  477. }
  478. /* #endif */
  479. /* picker 弹出层通用的指示小三角, todo:扩展至上下左右方向定位 */
  480. /* #ifndef APP-NVUE */
  481. .uni-popper__arrow,
  482. .uni-popper__arrow::after {
  483. position: absolute;
  484. display: block;
  485. width: 0;
  486. height: 0;
  487. border-color: transparent;
  488. border-style: solid;
  489. border-width: 6px;
  490. }
  491. .uni-popper__arrow {
  492. filter: drop-shadow(0 2px 12px rgba(0, 0, 0, 0.03));
  493. top: -6px;
  494. left: 10%;
  495. margin-right: 3px;
  496. border-top-width: 0;
  497. border-bottom-color: #EBEEF5;
  498. }
  499. .uni-popper__arrow::after {
  500. content: " ";
  501. top: 1px;
  502. margin-left: -6px;
  503. border-top-width: 0;
  504. border-bottom-color: #fff;
  505. }
  506. /* #endif */
  507. </style>