uni-data-picker.vue 14 KB

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