uni-data-picker.vue 13 KB

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