uni-data-picker.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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" :map="map"
  43. :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','inputclick'],
  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.$nextTick(() => {
  129. this.load();
  130. })
  131. },
  132. watch: {
  133. localdata: {
  134. handler() {
  135. this.load()
  136. },
  137. deep: true
  138. },
  139. },
  140. methods: {
  141. clear() {
  142. this._dispatchEvent([]);
  143. },
  144. onPropsChange() {
  145. this._treeData = [];
  146. this.selectedIndex = 0;
  147. this.load();
  148. },
  149. load() {
  150. if (this.readonly) {
  151. this._processReadonly(this.localdata, this.dataValue);
  152. return;
  153. }
  154. // 回显本地数据
  155. if (this.isLocalData) {
  156. this.loadData();
  157. this.inputSelected = this.selected.slice(0);
  158. } else if (this.isCloudDataList || this.isCloudDataTree) { // 回显 Cloud 数据
  159. this.loading = true;
  160. this.getCloudDataValue().then((res) => {
  161. this.loading = false;
  162. this.inputSelected = res;
  163. }).catch((err) => {
  164. this.loading = false;
  165. this.errorMessage = err;
  166. })
  167. }
  168. },
  169. show() {
  170. this.isOpened = true
  171. setTimeout(() => {
  172. this.$refs.pickerView.updateData({
  173. treeData: this._treeData,
  174. selected: this.selected,
  175. selectedIndex: this.selectedIndex
  176. })
  177. }, 200)
  178. this.$emit('popupopened')
  179. },
  180. hide() {
  181. this.isOpened = false
  182. this.$emit('popupclosed')
  183. },
  184. handleInput() {
  185. if (this.readonly) {
  186. this.$emit('inputclick')
  187. return
  188. }
  189. this.show()
  190. },
  191. handleClose(e) {
  192. this.hide()
  193. },
  194. onnodeclick(e) {
  195. this.$emit('nodeclick', e)
  196. },
  197. ondatachange(e) {
  198. this._treeData = this.$refs.pickerView._treeData
  199. },
  200. onchange(e) {
  201. this.hide()
  202. this.$nextTick(() => {
  203. this.inputSelected = e;
  204. })
  205. this._dispatchEvent(e)
  206. },
  207. _processReadonly(dataList, value) {
  208. var isTree = dataList.findIndex((item) => {
  209. return item.children
  210. })
  211. if (isTree > -1) {
  212. let inputValue
  213. if (Array.isArray(value)) {
  214. inputValue = value[value.length - 1]
  215. if (typeof inputValue === 'object' && inputValue.value) {
  216. inputValue = inputValue.value
  217. }
  218. } else {
  219. inputValue = value
  220. }
  221. this.inputSelected = this._findNodePath(inputValue, this.localdata)
  222. return
  223. }
  224. if (!this.hasValue) {
  225. this.inputSelected = []
  226. return
  227. }
  228. let result = []
  229. for (let i = 0; i < value.length; i++) {
  230. var val = value[i]
  231. var item = dataList.find((v) => {
  232. return v.value == val
  233. })
  234. if (item) {
  235. result.push(item)
  236. }
  237. }
  238. if (result.length) {
  239. this.inputSelected = result
  240. }
  241. },
  242. _filterForArray(data, valueArray) {
  243. var result = []
  244. for (let i = 0; i < valueArray.length; i++) {
  245. var value = valueArray[i]
  246. var found = data.find((item) => {
  247. return item.value == value
  248. })
  249. if (found) {
  250. result.push(found)
  251. }
  252. }
  253. return result
  254. },
  255. _dispatchEvent(selected) {
  256. let item = {}
  257. if (selected.length) {
  258. var value = new Array(selected.length)
  259. for (var i = 0; i < selected.length; i++) {
  260. value[i] = selected[i].value
  261. }
  262. item = selected[selected.length - 1]
  263. } else {
  264. item.value = ''
  265. }
  266. if (this.formItem) {
  267. this.formItem.setValue(item.value)
  268. }
  269. this.$emit('input', item.value)
  270. this.$emit('update:modelValue', item.value)
  271. this.$emit('change', {
  272. detail: {
  273. value: selected
  274. }
  275. })
  276. }
  277. }
  278. }
  279. </script>
  280. <style>
  281. .uni-data-tree {
  282. flex: 1;
  283. position: relative;
  284. font-size: 14px;
  285. }
  286. .error-text {
  287. color: #DD524D;
  288. }
  289. .input-value {
  290. /* #ifndef APP-NVUE */
  291. display: flex;
  292. /* #endif */
  293. flex-direction: row;
  294. align-items: center;
  295. flex-wrap: nowrap;
  296. font-size: 14px;
  297. /* line-height: 35px; */
  298. padding: 0 10px;
  299. padding-right: 5px;
  300. overflow: hidden;
  301. height: 35px;
  302. /* #ifndef APP-NVUE */
  303. box-sizing: border-box;
  304. /* #endif */
  305. }
  306. .input-value-border {
  307. border: 1px solid #e5e5e5;
  308. border-radius: 5px;
  309. }
  310. .selected-area {
  311. flex: 1;
  312. overflow: hidden;
  313. /* #ifndef APP-NVUE */
  314. display: flex;
  315. /* #endif */
  316. flex-direction: row;
  317. }
  318. .load-more {
  319. /* #ifndef APP-NVUE */
  320. margin-right: auto;
  321. /* #endif */
  322. /* #ifdef APP-NVUE */
  323. width: 40px;
  324. /* #endif */
  325. }
  326. .selected-list {
  327. /* #ifndef APP-NVUE */
  328. display: flex;
  329. /* #endif */
  330. flex-direction: row;
  331. flex-wrap: nowrap;
  332. /* padding: 0 5px; */
  333. }
  334. .selected-item {
  335. flex-direction: row;
  336. /* padding: 0 1px; */
  337. /* #ifndef APP-NVUE */
  338. white-space: nowrap;
  339. /* #endif */
  340. }
  341. .text-color {
  342. color: #333;
  343. }
  344. .placeholder {
  345. color: grey;
  346. font-size: 12px;
  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. /* #ifndef APP-NVUE */
  386. top: 20%;
  387. /* #endif */
  388. /* #ifdef APP-NVUE */
  389. top: 200px;
  390. /* #endif */
  391. right: 0;
  392. bottom: 0;
  393. background-color: #FFFFFF;
  394. border-top-left-radius: 10px;
  395. border-top-right-radius: 10px;
  396. /* #ifndef APP-NVUE */
  397. display: flex;
  398. /* #endif */
  399. flex-direction: column;
  400. z-index: 102;
  401. overflow: hidden;
  402. /* #ifdef APP-NVUE */
  403. width: 750rpx;
  404. /* #endif */
  405. }
  406. .dialog-caption {
  407. position: relative;
  408. /* #ifndef APP-NVUE */
  409. display: flex;
  410. /* #endif */
  411. flex-direction: row;
  412. /* border-bottom: 1px solid #f0f0f0; */
  413. }
  414. .title-area {
  415. /* #ifndef APP-NVUE */
  416. display: flex;
  417. /* #endif */
  418. align-items: center;
  419. /* #ifndef APP-NVUE */
  420. margin: auto;
  421. /* #endif */
  422. padding: 0 10px;
  423. }
  424. .dialog-title {
  425. /* font-weight: bold; */
  426. line-height: 44px;
  427. }
  428. .dialog-close {
  429. position: absolute;
  430. top: 0;
  431. right: 0;
  432. bottom: 0;
  433. /* #ifndef APP-NVUE */
  434. display: flex;
  435. /* #endif */
  436. flex-direction: row;
  437. align-items: center;
  438. padding: 0 15px;
  439. }
  440. .dialog-close-plus {
  441. width: 16px;
  442. height: 2px;
  443. background-color: #666;
  444. border-radius: 2px;
  445. transform: rotate(45deg);
  446. }
  447. .dialog-close-rotate {
  448. position: absolute;
  449. transform: rotate(-45deg);
  450. }
  451. .picker-view {
  452. flex: 1;
  453. overflow: hidden;
  454. }
  455. .icon-clear {
  456. display: flex;
  457. align-items: center;
  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. /* #ifndef APP-NVUE */
  486. .uni-popper__arrow,
  487. .uni-popper__arrow::after {
  488. position: absolute;
  489. display: block;
  490. width: 0;
  491. height: 0;
  492. border-color: transparent;
  493. border-style: solid;
  494. border-width: 6px;
  495. }
  496. .uni-popper__arrow {
  497. filter: drop-shadow(0 2px 12px rgba(0, 0, 0, 0.03));
  498. top: -6px;
  499. left: 10%;
  500. margin-right: 3px;
  501. border-top-width: 0;
  502. border-bottom-color: #EBEEF5;
  503. }
  504. .uni-popper__arrow::after {
  505. content: " ";
  506. top: 1px;
  507. margin-left: -6px;
  508. border-top-width: 0;
  509. border-bottom-color: #fff;
  510. }
  511. /* #endif */
  512. </style>