uni-data-picker.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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="16"></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-right: 5px;
  299. overflow: hidden;
  300. height: 35px;
  301. /* #ifndef APP-NVUE */
  302. box-sizing: border-box;
  303. /* #endif */
  304. }
  305. .input-value-border {
  306. /* border: 1px solid #e5e5e5; */
  307. border-radius: 5px;
  308. }
  309. .selected-area {
  310. flex: 1;
  311. overflow: hidden;
  312. /* #ifndef APP-NVUE */
  313. display: flex;
  314. /* #endif */
  315. flex-direction: row;
  316. }
  317. .load-more {
  318. /* #ifndef APP-NVUE */
  319. margin-right: auto;
  320. /* #endif */
  321. /* #ifdef APP-NVUE */
  322. width: 40px;
  323. /* #endif */
  324. }
  325. .selected-list {
  326. /* #ifndef APP-NVUE */
  327. display: flex;
  328. /* #endif */
  329. flex-direction: row;
  330. flex-wrap: nowrap;
  331. /* padding: 0 5px; */
  332. }
  333. .selected-item {
  334. flex-direction: row;
  335. /* padding: 0 1px; */
  336. /* #ifndef APP-NVUE */
  337. white-space: nowrap;
  338. /* #endif */
  339. }
  340. .text-color {
  341. color: #333;
  342. }
  343. .placeholder {
  344. color: rgb(192, 196, 204);
  345. font-size: 14px;
  346. }
  347. .input-split-line {
  348. opacity: .5;
  349. }
  350. .arrow-area {
  351. position: relative;
  352. width: 20px;
  353. /* #ifndef APP-NVUE */
  354. margin-left: auto;
  355. display: flex;
  356. /* #endif */
  357. justify-content: center;
  358. transform: rotate(-135deg);
  359. transform-origin: center;
  360. }
  361. .input-arrow {
  362. width: 10px;
  363. height: 10px;
  364. border-left: 2px solid #999;
  365. border-bottom: 2px solid #999;
  366. }
  367. .uni-data-tree-cover {
  368. position: fixed;
  369. left: 0;
  370. top: 0;
  371. right: 0;
  372. bottom: 0;
  373. background-color: rgba(0, 0, 0, .4);
  374. /* #ifndef APP-NVUE */
  375. display: flex;
  376. /* #endif */
  377. flex-direction: column;
  378. z-index: 100;
  379. }
  380. .uni-data-tree-dialog {
  381. position: fixed;
  382. left: 0;
  383. /* #ifndef APP-NVUE */
  384. top: 20%;
  385. /* #endif */
  386. /* #ifdef APP-NVUE */
  387. top: 200px;
  388. /* #endif */
  389. right: 0;
  390. bottom: 0;
  391. background-color: #FFFFFF;
  392. border-top-left-radius: 10px;
  393. border-top-right-radius: 10px;
  394. /* #ifndef APP-NVUE */
  395. display: flex;
  396. /* #endif */
  397. flex-direction: column;
  398. z-index: 102;
  399. overflow: hidden;
  400. /* #ifdef APP-NVUE */
  401. width: 750rpx;
  402. /* #endif */
  403. }
  404. .dialog-caption {
  405. position: relative;
  406. /* #ifndef APP-NVUE */
  407. display: flex;
  408. /* #endif */
  409. flex-direction: row;
  410. /* border-bottom: 1px solid #f0f0f0; */
  411. }
  412. .title-area {
  413. /* #ifndef APP-NVUE */
  414. display: flex;
  415. /* #endif */
  416. align-items: center;
  417. /* #ifndef APP-NVUE */
  418. margin: auto;
  419. /* #endif */
  420. padding: 0 10px;
  421. }
  422. .dialog-title {
  423. /* font-weight: bold; */
  424. line-height: 44px;
  425. }
  426. .dialog-close {
  427. position: absolute;
  428. top: 0;
  429. right: 0;
  430. bottom: 0;
  431. /* #ifndef APP-NVUE */
  432. display: flex;
  433. /* #endif */
  434. flex-direction: row;
  435. align-items: center;
  436. padding: 0 15px;
  437. }
  438. .dialog-close-plus {
  439. width: 16px;
  440. height: 2px;
  441. background-color: #666;
  442. border-radius: 2px;
  443. transform: rotate(45deg);
  444. }
  445. .dialog-close-rotate {
  446. position: absolute;
  447. transform: rotate(-45deg);
  448. }
  449. .picker-view {
  450. flex: 1;
  451. overflow: hidden;
  452. }
  453. .icon-clear {
  454. display: flex;
  455. align-items: center;
  456. }
  457. /* #ifdef H5 */
  458. @media all and (min-width: 768px) {
  459. .uni-data-tree-cover {
  460. background-color: transparent;
  461. }
  462. .uni-data-tree-dialog {
  463. position: absolute;
  464. top: 55px;
  465. height: auto;
  466. min-height: 400px;
  467. max-height: 50vh;
  468. background-color: #fff;
  469. border: 1px solid #EBEEF5;
  470. box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  471. border-radius: 4px;
  472. overflow: unset;
  473. }
  474. .dialog-caption {
  475. display: none;
  476. }
  477. .icon-clear {
  478. /* margin-right: 5px; */
  479. }
  480. }
  481. /* #endif */
  482. /* picker 弹出层通用的指示小三角, todo:扩展至上下左右方向定位 */
  483. /* #ifndef APP-NVUE */
  484. .uni-popper__arrow,
  485. .uni-popper__arrow::after {
  486. position: absolute;
  487. display: block;
  488. width: 0;
  489. height: 0;
  490. border-color: transparent;
  491. border-style: solid;
  492. border-width: 6px;
  493. }
  494. .uni-popper__arrow {
  495. filter: drop-shadow(0 2px 12px rgba(0, 0, 0, 0.03));
  496. top: -6px;
  497. left: 10%;
  498. margin-right: 3px;
  499. border-top-width: 0;
  500. border-bottom-color: #EBEEF5;
  501. }
  502. .uni-popper__arrow::after {
  503. content: " ";
  504. top: 1px;
  505. margin-left: -6px;
  506. border-top-width: 0;
  507. border-bottom-color: #fff;
  508. }
  509. /* #endif */
  510. </style>