uni-data-picker.vue 12 KB

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