uni-data-picker.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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>{{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="#e1e1e1" size="14"></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. this.$nextTick(() => {
  182. this.$refs.pickerView.updateData({
  183. treeData: this._treeData,
  184. selected: this.selected,
  185. selectedIndex: this.selectedIndex
  186. })
  187. })
  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.inputSelected = e
  212. this._dispatchEvent(e)
  213. },
  214. _processReadonly(dataList, value) {
  215. var isTree = dataList.findIndex((item) => {
  216. return item.children
  217. })
  218. if (isTree > -1) {
  219. let inputValue
  220. if (Array.isArray(value)) {
  221. inputValue = value[value.length - 1]
  222. if (typeof inputValue === 'object' && inputValue.value) {
  223. inputValue = inputValue.value
  224. }
  225. } else {
  226. inputValue = value
  227. }
  228. this.inputSelected = this._findNodePath(inputValue, this.localdata)
  229. return
  230. }
  231. if (!this.hasValue) {
  232. this.inputSelected = []
  233. return
  234. }
  235. let result = []
  236. for (let i = 0; i < value.length; i++) {
  237. var val = value[i]
  238. var item = dataList.find((v) => {
  239. return v.value == val
  240. })
  241. if (item) {
  242. result.push(item)
  243. }
  244. }
  245. if (result.length) {
  246. this.inputSelected = result
  247. }
  248. },
  249. _filterForArray(data, valueArray) {
  250. var result = []
  251. for (let i = 0; i < valueArray.length; i++) {
  252. var value = valueArray[i]
  253. var found = data.find((item) => {
  254. return item.value == value
  255. })
  256. if (found) {
  257. result.push(found)
  258. }
  259. }
  260. return result
  261. },
  262. _dispatchEvent(selected) {
  263. let item = {}
  264. if (selected.length) {
  265. var value = new Array(selected.length)
  266. for (var i = 0; i < selected.length; i++) {
  267. value[i] = selected[i].value
  268. }
  269. item = selected[selected.length - 1]
  270. } else {
  271. item.value = ''
  272. }
  273. if (this.formItem) {
  274. this.formItem.setValue(item.value)
  275. }
  276. this.$emit('input', item.value)
  277. this.$emit('update:modelValue', item.value)
  278. this.$emit('change', {
  279. detail: {
  280. value: selected
  281. }
  282. })
  283. }
  284. }
  285. }
  286. </script>
  287. <style >
  288. .uni-data-tree {
  289. position: relative;
  290. font-size: 14px;
  291. }
  292. .error-text {
  293. color: #DD524D;
  294. }
  295. .input-value {
  296. /* #ifndef APP-NVUE */
  297. display: flex;
  298. /* #endif */
  299. flex-direction: row;
  300. align-items: center;
  301. flex-wrap: nowrap;
  302. font-size: 14px;
  303. line-height: 38px;
  304. padding: 0 5px;
  305. overflow: hidden;
  306. /* #ifdef APP-NVUE */
  307. height: 40px;
  308. /* #endif */
  309. }
  310. .input-value-border {
  311. border: 1px solid #e5e5e5;
  312. border-radius: 5px;
  313. }
  314. .selected-area {
  315. flex: 1;
  316. overflow: hidden;
  317. /* #ifndef APP-NVUE */
  318. display: flex;
  319. /* #endif */
  320. flex-direction: row;
  321. }
  322. .load-more {
  323. /* #ifndef APP-NVUE */
  324. margin-right: auto;
  325. /* #endif */
  326. /* #ifdef APP-NVUE */
  327. width: 40px;
  328. /* #endif */
  329. }
  330. .selected-list {
  331. /* #ifndef APP-NVUE */
  332. display: flex;
  333. /* #endif */
  334. flex-direction: row;
  335. flex-wrap: nowrap;
  336. padding: 0 5px;
  337. }
  338. .selected-item {
  339. flex-direction: row;
  340. padding: 0 1px;
  341. /* #ifndef APP-NVUE */
  342. white-space: nowrap;
  343. /* #endif */
  344. }
  345. .placeholder {
  346. color: grey;
  347. }
  348. .input-split-line {
  349. opacity: .5;
  350. }
  351. .arrow-area {
  352. position: relative;
  353. width: 20px;
  354. /* #ifndef APP-NVUE */
  355. margin-bottom: 5px;
  356. margin-left: auto;
  357. display: flex;
  358. /* #endif */
  359. justify-content: center;
  360. transform: rotate(-45deg);
  361. transform-origin: center;
  362. }
  363. .input-arrow {
  364. width: 7px;
  365. height: 7px;
  366. border-left: 1px solid #999;
  367. border-bottom: 1px solid #999;
  368. }
  369. .uni-data-tree-cover {
  370. position: fixed;
  371. left: 0;
  372. top: 0;
  373. right: 0;
  374. bottom: 0;
  375. background-color: rgba(0, 0, 0, .4);
  376. /* #ifndef APP-NVUE */
  377. display: flex;
  378. /* #endif */
  379. flex-direction: column;
  380. z-index: 100;
  381. }
  382. .uni-data-tree-dialog {
  383. position: fixed;
  384. left: 0;
  385. top: 20%;
  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. /* #ifdef H5 */
  451. @media all and (min-width: 768px) {
  452. .uni-data-tree-cover {
  453. background-color: transparent;
  454. }
  455. .uni-data-tree-dialog {
  456. position: absolute;
  457. top: 55px;
  458. height: auto;
  459. min-height: 400px;
  460. max-height: 50vh;
  461. background-color: #fff;
  462. border: 1px solid #EBEEF5;
  463. box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  464. border-radius: 4px;
  465. overflow: unset;
  466. }
  467. .dialog-caption {
  468. display: none;
  469. }
  470. .icon-clear {
  471. margin-right: 5px;
  472. }
  473. }
  474. /* #endif */
  475. /* picker 弹出层通用的指示小三角, todo:扩展至上下左右方向定位 */
  476. /* #ifndef APP-NVUE */
  477. .uni-popper__arrow,
  478. .uni-popper__arrow::after {
  479. position: absolute;
  480. display: block;
  481. width: 0;
  482. height: 0;
  483. border-color: transparent;
  484. border-style: solid;
  485. border-width: 6px;
  486. }
  487. .uni-popper__arrow {
  488. filter: drop-shadow(0 2px 12px rgba(0, 0, 0, 0.03));
  489. top: -6px;
  490. left: 10%;
  491. margin-right: 3px;
  492. border-top-width: 0;
  493. border-bottom-color: #EBEEF5;
  494. }
  495. .uni-popper__arrow::after {
  496. content: " ";
  497. top: 1px;
  498. margin-left: -6px;
  499. border-top-width: 0;
  500. border-bottom-color: #fff;
  501. }
  502. /* #endif */
  503. </style>