uni-data-picker.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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.modelValue = null;
  143. this._dispatchEvent([]);
  144. },
  145. onPropsChange() {
  146. this._treeData = [];
  147. this.selectedIndex = 0;
  148. this.load();
  149. },
  150. load() {
  151. if (this.readonly) {
  152. this._processReadonly(this.localdata, this.dataValue);
  153. return;
  154. }
  155. // 回显本地数据
  156. if (this.isLocalData) {
  157. this.loadData();
  158. this.inputSelected = this.selected.slice(0);
  159. } else if (this.isCloudDataList || this.isCloudDataTree) { // 回显 Cloud 数据
  160. this.loading = true;
  161. this.getCloudDataValue().then((res) => {
  162. this.loading = false;
  163. this.inputSelected = res;
  164. }).catch((err) => {
  165. this.loading = false;
  166. this.errorMessage = err;
  167. })
  168. }
  169. },
  170. show() {
  171. this.isOpened = true
  172. setTimeout(() => {
  173. this.$refs.pickerView.updateData({
  174. treeData: this._treeData,
  175. selected: this.selected,
  176. selectedIndex: this.selectedIndex
  177. })
  178. }, 200)
  179. this.$emit('popupopened')
  180. },
  181. hide() {
  182. this.isOpened = false
  183. this.$emit('popupclosed')
  184. },
  185. handleInput() {
  186. if (this.readonly) {
  187. this.$emit('inputclick')
  188. return
  189. }
  190. this.show()
  191. },
  192. handleClose(e) {
  193. this.hide()
  194. },
  195. onnodeclick(e) {
  196. this.$emit('nodeclick', e)
  197. },
  198. ondatachange(e) {
  199. this._treeData = this.$refs.pickerView._treeData
  200. },
  201. onchange(e) {
  202. this.hide()
  203. this.$nextTick(() => {
  204. this.inputSelected = e;
  205. })
  206. this._dispatchEvent(e)
  207. },
  208. _processReadonly(dataList, value) {
  209. var isTree = dataList.findIndex((item) => {
  210. return item.children
  211. })
  212. if (isTree > -1) {
  213. let inputValue
  214. if (Array.isArray(value)) {
  215. inputValue = value[value.length - 1]
  216. if (typeof inputValue === 'object' && inputValue.value) {
  217. inputValue = inputValue.value
  218. }
  219. } else {
  220. inputValue = value
  221. }
  222. this.inputSelected = this._findNodePath(inputValue, this.localdata)
  223. return
  224. }
  225. if (!this.hasValue) {
  226. this.inputSelected = []
  227. return
  228. }
  229. let result = []
  230. for (let i = 0; i < value.length; i++) {
  231. var val = value[i]
  232. var item = dataList.find((v) => {
  233. return v.value == val
  234. })
  235. if (item) {
  236. result.push(item)
  237. }
  238. }
  239. if (result.length) {
  240. this.inputSelected = result
  241. }
  242. },
  243. _filterForArray(data, valueArray) {
  244. var result = []
  245. for (let i = 0; i < valueArray.length; i++) {
  246. var value = valueArray[i]
  247. var found = data.find((item) => {
  248. return item.value == value
  249. })
  250. if (found) {
  251. result.push(found)
  252. }
  253. }
  254. return result
  255. },
  256. _dispatchEvent(selected) {
  257. let item = {}
  258. if (selected.length) {
  259. var value = new Array(selected.length)
  260. for (var i = 0; i < selected.length; i++) {
  261. value[i] = selected[i].value
  262. }
  263. item = selected[selected.length - 1]
  264. } else {
  265. item.value = ''
  266. }
  267. if (this.formItem) {
  268. this.formItem.setValue(item.value)
  269. }
  270. this.$emit('input', item.value)
  271. this.$emit('update:modelValue', item.value)
  272. this.$emit('change', {
  273. detail: {
  274. value: selected
  275. }
  276. })
  277. }
  278. }
  279. }
  280. </script>
  281. <style>
  282. .uni-data-tree {
  283. flex: 1;
  284. position: relative;
  285. font-size: 14px;
  286. }
  287. .error-text {
  288. color: #DD524D;
  289. }
  290. .input-value {
  291. /* #ifndef APP-NVUE */
  292. display: flex;
  293. /* #endif */
  294. flex-direction: row;
  295. align-items: center;
  296. flex-wrap: nowrap;
  297. font-size: 14px;
  298. /* line-height: 35px; */
  299. padding: 0 10px;
  300. padding-right: 5px;
  301. overflow: hidden;
  302. height: 35px;
  303. /* #ifndef APP-NVUE */
  304. box-sizing: border-box;
  305. /* #endif */
  306. }
  307. .input-value-border {
  308. border: 1px solid #e5e5e5;
  309. border-radius: 5px;
  310. }
  311. .selected-area {
  312. flex: 1;
  313. overflow: hidden;
  314. /* #ifndef APP-NVUE */
  315. display: flex;
  316. /* #endif */
  317. flex-direction: row;
  318. }
  319. .load-more {
  320. /* #ifndef APP-NVUE */
  321. margin-right: auto;
  322. /* #endif */
  323. /* #ifdef APP-NVUE */
  324. width: 40px;
  325. /* #endif */
  326. }
  327. .selected-list {
  328. /* #ifndef APP-NVUE */
  329. display: flex;
  330. /* #endif */
  331. flex-direction: row;
  332. flex-wrap: nowrap;
  333. /* padding: 0 5px; */
  334. }
  335. .selected-item {
  336. flex-direction: row;
  337. /* padding: 0 1px; */
  338. /* #ifndef APP-NVUE */
  339. white-space: nowrap;
  340. /* #endif */
  341. }
  342. .text-color {
  343. color: #333;
  344. }
  345. .placeholder {
  346. color: grey;
  347. font-size: 12px;
  348. }
  349. .input-split-line {
  350. opacity: .5;
  351. }
  352. .arrow-area {
  353. position: relative;
  354. width: 20px;
  355. /* #ifndef APP-NVUE */
  356. margin-bottom: 5px;
  357. margin-left: auto;
  358. display: flex;
  359. /* #endif */
  360. justify-content: center;
  361. transform: rotate(-45deg);
  362. transform-origin: center;
  363. }
  364. .input-arrow {
  365. width: 7px;
  366. height: 7px;
  367. border-left: 1px solid #999;
  368. border-bottom: 1px solid #999;
  369. }
  370. .uni-data-tree-cover {
  371. position: fixed;
  372. left: 0;
  373. top: 0;
  374. right: 0;
  375. bottom: 0;
  376. background-color: rgba(0, 0, 0, .4);
  377. /* #ifndef APP-NVUE */
  378. display: flex;
  379. /* #endif */
  380. flex-direction: column;
  381. z-index: 100;
  382. }
  383. .uni-data-tree-dialog {
  384. position: fixed;
  385. left: 0;
  386. /* #ifndef APP-NVUE */
  387. top: 20%;
  388. /* #endif */
  389. /* #ifdef APP-NVUE */
  390. top: 200px;
  391. /* #endif */
  392. right: 0;
  393. bottom: 0;
  394. background-color: #FFFFFF;
  395. border-top-left-radius: 10px;
  396. border-top-right-radius: 10px;
  397. /* #ifndef APP-NVUE */
  398. display: flex;
  399. /* #endif */
  400. flex-direction: column;
  401. z-index: 102;
  402. overflow: hidden;
  403. /* #ifdef APP-NVUE */
  404. width: 750rpx;
  405. /* #endif */
  406. }
  407. .dialog-caption {
  408. position: relative;
  409. /* #ifndef APP-NVUE */
  410. display: flex;
  411. /* #endif */
  412. flex-direction: row;
  413. /* border-bottom: 1px solid #f0f0f0; */
  414. }
  415. .title-area {
  416. /* #ifndef APP-NVUE */
  417. display: flex;
  418. /* #endif */
  419. align-items: center;
  420. /* #ifndef APP-NVUE */
  421. margin: auto;
  422. /* #endif */
  423. padding: 0 10px;
  424. }
  425. .dialog-title {
  426. /* font-weight: bold; */
  427. line-height: 44px;
  428. }
  429. .dialog-close {
  430. position: absolute;
  431. top: 0;
  432. right: 0;
  433. bottom: 0;
  434. /* #ifndef APP-NVUE */
  435. display: flex;
  436. /* #endif */
  437. flex-direction: row;
  438. align-items: center;
  439. padding: 0 15px;
  440. }
  441. .dialog-close-plus {
  442. width: 16px;
  443. height: 2px;
  444. background-color: #666;
  445. border-radius: 2px;
  446. transform: rotate(45deg);
  447. }
  448. .dialog-close-rotate {
  449. position: absolute;
  450. transform: rotate(-45deg);
  451. }
  452. .picker-view {
  453. flex: 1;
  454. overflow: hidden;
  455. }
  456. .icon-clear {
  457. display: flex;
  458. align-items: center;
  459. }
  460. /* #ifdef H5 */
  461. @media all and (min-width: 768px) {
  462. .uni-data-tree-cover {
  463. background-color: transparent;
  464. }
  465. .uni-data-tree-dialog {
  466. position: absolute;
  467. top: 55px;
  468. height: auto;
  469. min-height: 400px;
  470. max-height: 50vh;
  471. background-color: #fff;
  472. border: 1px solid #EBEEF5;
  473. box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  474. border-radius: 4px;
  475. overflow: unset;
  476. }
  477. .dialog-caption {
  478. display: none;
  479. }
  480. .icon-clear {
  481. /* margin-right: 5px; */
  482. }
  483. }
  484. /* #endif */
  485. /* picker 弹出层通用的指示小三角, todo:扩展至上下左右方向定位 */
  486. /* #ifndef APP-NVUE */
  487. .uni-popper__arrow,
  488. .uni-popper__arrow::after {
  489. position: absolute;
  490. display: block;
  491. width: 0;
  492. height: 0;
  493. border-color: transparent;
  494. border-style: solid;
  495. border-width: 6px;
  496. }
  497. .uni-popper__arrow {
  498. filter: drop-shadow(0 2px 12px rgba(0, 0, 0, 0.03));
  499. top: -6px;
  500. left: 10%;
  501. margin-right: 3px;
  502. border-top-width: 0;
  503. border-bottom-color: #EBEEF5;
  504. }
  505. .uni-popper__arrow::after {
  506. content: " ";
  507. top: 1px;
  508. margin-left: -6px;
  509. border-top-width: 0;
  510. border-bottom-color: #fff;
  511. }
  512. /* #endif */
  513. </style>