uni-data-picker.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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. /**
  53. * DataPicker 级联选择
  54. * @description 支持单列、和多列级联选择。列数没有限制,如果屏幕显示不全,顶部tab区域会左右滚动。
  55. * @tutorial https://ext.dcloud.net.cn/plugin?id=3796
  56. * @property {String} popup-title 弹出窗口标题
  57. * @property {Array} localdata 本地数据,参考
  58. * @property {Boolean} border = [true|false] 是否有边框
  59. * @property {Boolean} readonly = [true|false] 是否仅读
  60. * @property {Boolean} preload = [true|false] 是否预加载数据
  61. * @value true 开启预加载数据,点击弹出窗口后显示已加载数据
  62. * @value false 关闭预加载数据,点击弹出窗口后开始加载数据
  63. * @property {Boolean} step-searh = [true|false] 是否分布查询
  64. * @value true 启用分布查询,仅查询当前选中节点
  65. * @value false 关闭分布查询,一次查询出所有数据
  66. * @property {String|DBFieldString} self-field 分布查询当前字段名称
  67. * @property {String|DBFieldString} parent-field 分布查询父字段名称
  68. * @property {String|DBCollectionString} collection 表名
  69. * @property {String|DBFieldString} field 查询字段,多个字段用 `,` 分割
  70. * @property {String} orderby 排序字段及正序倒叙设置
  71. * @property {String|JQLString} where 查询条件
  72. * @event {Function} popupshow 弹出的选择窗口打开时触发此事件
  73. * @event {Function} popuphide 弹出的选择窗口关闭时触发此事件
  74. */
  75. export default {
  76. name: 'UniDataPicker',
  77. emits: ['popupopened', 'popupclosed', 'nodeclick', 'input', 'change', 'update:modelValue'],
  78. mixins: [dataPicker],
  79. components: {
  80. DataPickerView
  81. },
  82. props: {
  83. options: {
  84. type: [Object, Array],
  85. default () {
  86. return {}
  87. }
  88. },
  89. popupTitle: {
  90. type: String,
  91. default: '请选择'
  92. },
  93. placeholder: {
  94. type: String,
  95. default: '请选择'
  96. },
  97. heightMobile: {
  98. type: String,
  99. default: ''
  100. },
  101. readonly: {
  102. type: Boolean,
  103. default: false
  104. },
  105. clearIcon: {
  106. type: Boolean,
  107. default: true
  108. },
  109. border: {
  110. type: Boolean,
  111. default: true
  112. },
  113. split: {
  114. type: String,
  115. default: '/'
  116. },
  117. ellipsis: {
  118. type: Boolean,
  119. default: true
  120. }
  121. },
  122. data() {
  123. return {
  124. isOpened: false,
  125. inputSelected: []
  126. }
  127. },
  128. created() {
  129. this.form = this.getForm('uniForms')
  130. this.formItem = this.getForm('uniFormsItem')
  131. if (this.formItem) {
  132. if (this.formItem.name) {
  133. this.rename = this.formItem.name
  134. this.form.inputChildrens.push(this)
  135. }
  136. }
  137. this.$nextTick(() => {
  138. this.load()
  139. })
  140. },
  141. methods: {
  142. clear() {
  143. this.inputSelected.splice(0)
  144. this._dispatchEvent([])
  145. },
  146. onPropsChange() {
  147. this._treeData = []
  148. this.selectedIndex = 0
  149. this.load()
  150. },
  151. load() {
  152. if (this.readonly) {
  153. this._processReadonly(this.localdata, this.dataValue)
  154. return
  155. }
  156. if (this.isLocaldata) {
  157. this.loadData()
  158. this.inputSelected = this.selected.slice(0)
  159. } else if (!this.parentField && !this.selfField && this.hasValue) {
  160. this.getNodeData(() => {
  161. this.inputSelected = this.selected.slice(0)
  162. })
  163. } else if (this.hasValue) {
  164. this.getTreePath(() => {
  165. this.inputSelected = this.selected.slice(0)
  166. })
  167. }
  168. },
  169. getForm(name = 'uniForms') {
  170. let parent = this.$parent;
  171. let parentName = parent.$options.name;
  172. while (parentName !== name) {
  173. parent = parent.$parent;
  174. if (!parent) return false;
  175. parentName = parent.$options.name;
  176. }
  177. return parent;
  178. },
  179. show() {
  180. this.isOpened = true
  181. setTimeout(() => {
  182. this.$refs.pickerView.updateData({
  183. treeData: this._treeData,
  184. selected: this.selected,
  185. selectedIndex: this.selectedIndex
  186. })
  187. }, 200)
  188. this.$emit('popupopened')
  189. },
  190. hide() {
  191. this.isOpened = false
  192. this.$emit('popupclosed')
  193. },
  194. handleInput() {
  195. if (this.readonly) {
  196. return
  197. }
  198. this.show()
  199. },
  200. handleClose(e) {
  201. this.hide()
  202. },
  203. onnodeclick(e) {
  204. this.$emit('nodeclick', e)
  205. },
  206. ondatachange(e) {
  207. this._treeData = this.$refs.pickerView._treeData
  208. },
  209. onchange(e) {
  210. this.hide()
  211. this.$nextTick(() => {
  212. this.inputSelected = e;
  213. })
  214. this._dispatchEvent(e)
  215. },
  216. _processReadonly(dataList, value) {
  217. var isTree = dataList.findIndex((item) => {
  218. return item.children
  219. })
  220. if (isTree > -1) {
  221. let inputValue
  222. if (Array.isArray(value)) {
  223. inputValue = value[value.length - 1]
  224. if (typeof inputValue === 'object' && inputValue.value) {
  225. inputValue = inputValue.value
  226. }
  227. } else {
  228. inputValue = value
  229. }
  230. this.inputSelected = this._findNodePath(inputValue, this.localdata)
  231. return
  232. }
  233. if (!this.hasValue) {
  234. this.inputSelected = []
  235. return
  236. }
  237. let result = []
  238. for (let i = 0; i < value.length; i++) {
  239. var val = value[i]
  240. var item = dataList.find((v) => {
  241. return v.value == val
  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. /* #ifdef APP-NVUE */
  312. /* #endif */
  313. box-sizing: border-box;
  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. top: 20%;
  395. right: 0;
  396. bottom: 0;
  397. background-color: #FFFFFF;
  398. border-top-left-radius: 10px;
  399. border-top-right-radius: 10px;
  400. /* #ifndef APP-NVUE */
  401. display: flex;
  402. /* #endif */
  403. flex-direction: column;
  404. z-index: 102;
  405. overflow: hidden;
  406. /* #ifdef APP-NVUE */
  407. width: 750rpx;
  408. /* #endif */
  409. }
  410. .dialog-caption {
  411. position: relative;
  412. /* #ifndef APP-NVUE */
  413. display: flex;
  414. /* #endif */
  415. flex-direction: row;
  416. /* border-bottom: 1px solid #f0f0f0; */
  417. }
  418. .title-area {
  419. /* #ifndef APP-NVUE */
  420. display: flex;
  421. /* #endif */
  422. align-items: center;
  423. /* #ifndef APP-NVUE */
  424. margin: auto;
  425. /* #endif */
  426. padding: 0 10px;
  427. }
  428. .dialog-title {
  429. /* font-weight: bold; */
  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>