uni-data-picker.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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-show="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. :addType="addType" :addIndex="addIndex"
  43. :preload="preload" :collection="collection" :field="field" :orderby="orderby" :where="where"
  44. :step-searh="stepSearh" :self-field="selfField" :parent-field="parentField" :managed-mode="true"
  45. :map="map" :ellipsis="ellipsis" @change="onchange" @datachange="ondatachange" @nodeclick="onnodeclick">
  46. </data-picker-view>
  47. </view>
  48. </view>
  49. </template>
  50. <script>
  51. import dataPicker from "../uni-data-pickerview/uni-data-picker.js"
  52. import DataPickerView from "../uni-data-pickerview/uni-data-pickerview.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. },
  83. props: {
  84. addType:{},
  85. addIndex:{},
  86. options: {
  87. type: [Object, Array],
  88. default () {
  89. return {
  90. pageType:true
  91. }
  92. }
  93. },
  94. popupTitle: {
  95. type: String,
  96. default: '请选择'
  97. },
  98. placeholder: {
  99. type: String,
  100. default: '请选择'
  101. },
  102. heightMobile: {
  103. type: String,
  104. default: ''
  105. },
  106. readonly: {
  107. type: Boolean,
  108. default: false
  109. },
  110. clearIcon: {
  111. type: Boolean,
  112. default: true
  113. },
  114. border: {
  115. type: Boolean,
  116. default: true
  117. },
  118. split: {
  119. type: String,
  120. default: '/'
  121. },
  122. ellipsis: {
  123. type: Boolean,
  124. default: true
  125. }
  126. },
  127. data() {
  128. return {
  129. isOpened: false,
  130. inputSelected: []
  131. }
  132. },
  133. created() {
  134. this.form = this.getForm('uniForms')
  135. this.formItem = this.getForm('uniFormsItem')
  136. if (this.formItem) {
  137. if (this.formItem.name) {
  138. this.rename = this.formItem.name
  139. this.form.inputChildrens.push(this)
  140. }
  141. }
  142. this.$nextTick(() => {
  143. this.load()
  144. })
  145. },
  146. methods: {
  147. pageRefresh(){
  148. this.show();
  149. },
  150. clear() {
  151. this.inputSelected.splice(0)
  152. this._dispatchEvent([])
  153. },
  154. onPropsChange() {
  155. this._treeData = []
  156. this.selectedIndex = 0
  157. this.load()
  158. },
  159. load() {
  160. if (this.readonly) {
  161. this._processReadonly(this.localdata, this.dataValue)
  162. return
  163. }
  164. if (this.isLocaldata) {
  165. this.loadData()
  166. this.inputSelected = this.selected.slice(0)
  167. } else if (!this.parentField && !this.selfField && this.hasValue) {
  168. this.getNodeData(() => {
  169. this.inputSelected = this.selected.slice(0)
  170. })
  171. } else if (this.hasValue) {
  172. this.getTreePath(() => {
  173. this.inputSelected = this.selected.slice(0)
  174. })
  175. }
  176. },
  177. getForm(name = 'uniForms') {
  178. let parent = this.$parent;
  179. let parentName = parent.$options.name;
  180. while (parentName !== name) {
  181. parent = parent.$parent;
  182. if (!parent) return false;
  183. parentName = parent.$options.name;
  184. }
  185. return parent;
  186. },
  187. show() {
  188. this.isOpened = true
  189. this.$nextTick(() => {
  190. this.$refs.pickerView.updateData({
  191. treeData: this._treeData,
  192. selected: this.selected,
  193. selectedIndex: this.selectedIndex
  194. })
  195. })
  196. this.$emit('popupopened')
  197. },
  198. hide() {
  199. this.isOpened = false
  200. this.$emit('popupclosed')
  201. },
  202. handleInput() {
  203. if (this.readonly) {
  204. return
  205. }
  206. this.show()
  207. },
  208. handleClose(e) {
  209. this.hide()
  210. },
  211. onnodeclick(e) {
  212. this.$emit('nodeclick', e)
  213. },
  214. ondatachange(e) {
  215. this._treeData = this.$refs.pickerView._treeData
  216. },
  217. onchange(e) {
  218. this.hide()
  219. this.inputSelected = e
  220. this._dispatchEvent(e)
  221. },
  222. _processReadonly(dataList, value) {
  223. var isTree = dataList.findIndex((item) => {
  224. return item.children
  225. })
  226. if (isTree > -1) {
  227. let inputValue
  228. if (Array.isArray(value)) {
  229. inputValue = value[value.length - 1]
  230. if (typeof inputValue === 'object' && inputValue.value) {
  231. inputValue = inputValue.value
  232. }
  233. } else {
  234. inputValue = value
  235. }
  236. this.inputSelected = this._findNodePath(inputValue, this.localdata)
  237. return
  238. }
  239. if (!this.hasValue) {
  240. this.inputSelected = []
  241. return
  242. }
  243. let result = []
  244. for (let i = 0; i < value.length; i++) {
  245. var val = value[i]
  246. var item = dataList.find((v) => {
  247. return v.value == val
  248. })
  249. if (item) {
  250. result.push(item)
  251. }
  252. }
  253. if (result.length) {
  254. this.inputSelected = result
  255. }
  256. },
  257. _filterForArray(data, valueArray) {
  258. var result = []
  259. for (let i = 0; i < valueArray.length; i++) {
  260. var value = valueArray[i]
  261. var found = data.find((item) => {
  262. return item.value == value
  263. })
  264. if (found) {
  265. result.push(found)
  266. }
  267. }
  268. return result
  269. },
  270. _dispatchEvent(selected) {
  271. let item = {}
  272. if (selected.length) {
  273. var value = new Array(selected.length)
  274. for (var i = 0; i < selected.length; i++) {
  275. value[i] = selected[i].value
  276. }
  277. item = selected[selected.length - 1]
  278. } else {
  279. item.value = ''
  280. }
  281. if (this.formItem) {
  282. this.formItem.setValue(item.value)
  283. }
  284. this.$emit('input', item.value)
  285. this.$emit('update:modelValue', item.value)
  286. this.$emit('change', {
  287. detail: {
  288. value: selected
  289. }
  290. })
  291. }
  292. }
  293. }
  294. </script>
  295. <style scoped>
  296. .uni-data-tree {
  297. position: relative;
  298. font-size: 14px;
  299. }
  300. .error-text {
  301. color: #DD524D;
  302. }
  303. .input-value {
  304. /* #ifndef APP-NVUE */
  305. display: flex;
  306. /* #endif */
  307. flex-direction: row;
  308. align-items: center;
  309. flex-wrap: nowrap;
  310. font-size: 14px;
  311. line-height: 38px;
  312. padding: 0 5px;
  313. overflow: hidden;
  314. /* #ifdef APP-NVUE */
  315. height: 40px;
  316. /* #endif */
  317. }
  318. .input-value-border {
  319. border: 1px solid #e5e5e5;
  320. border-radius: 5px;
  321. }
  322. .selected-area {
  323. flex: 1;
  324. overflow: hidden;
  325. /* #ifndef APP-NVUE */
  326. display: flex;
  327. /* #endif */
  328. flex-direction: row;
  329. }
  330. .load-more {
  331. /* #ifndef APP-NVUE */
  332. margin-right: auto;
  333. /* #endif */
  334. /* #ifdef APP-NVUE */
  335. width: 40px;
  336. /* #endif */
  337. }
  338. .selected-list {
  339. /* #ifndef APP-NVUE */
  340. display: flex;
  341. /* #endif */
  342. flex-direction: row;
  343. flex-wrap: nowrap;
  344. padding: 0 5px;
  345. }
  346. .selected-item {
  347. flex-direction: row;
  348. padding: 0 1px;
  349. /* #ifndef APP-NVUE */
  350. white-space: nowrap;
  351. /* #endif */
  352. }
  353. .placeholder {
  354. color: grey;
  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. top:0;
  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. /* #ifdef H5 */
  460. @media all and (min-width: 768px) {
  461. .uni-data-tree-cover {
  462. background-color: transparent;
  463. }
  464. .uni-data-tree-dialog {
  465. position: absolute;
  466. top: 55px;
  467. height: auto;
  468. min-height: 400px;
  469. max-height: 50vh;
  470. background-color: #fff;
  471. border: 1px solid #EBEEF5;
  472. box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  473. border-radius: 4px;
  474. overflow: unset;
  475. }
  476. .dialog-caption {
  477. display: none;
  478. }
  479. .icon-clear {
  480. margin-right: 5px;
  481. }
  482. }
  483. /* #endif */
  484. /* picker 弹出层通用的指示小三角, todo:扩展至上下左右方向定位 */
  485. .uni-popper__arrow,
  486. .uni-popper__arrow::after {
  487. position: absolute;
  488. display: block;
  489. width: 0;
  490. height: 0;
  491. border-color: transparent;
  492. border-style: solid;
  493. border-width: 6px;
  494. }
  495. .uni-popper__arrow {
  496. filter: drop-shadow(0 2px 12px rgba(0, 0, 0, 0.03));
  497. top: -6px;
  498. left: 10%;
  499. margin-right: 3px;
  500. border-top-width: 0;
  501. border-bottom-color: #EBEEF5;
  502. }
  503. .uni-popper__arrow::after {
  504. content: " ";
  505. top: 1px;
  506. margin-left: -6px;
  507. border-top-width: 0;
  508. border-bottom-color: #fff;
  509. }
  510. </style>