uni-data-picker.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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" class="input-split-line">{{split}}</text>
  14. </view>
  15. </view>
  16. </scroll-view>
  17. <text v-else class="selected-area placeholder">{{placeholder}}</text>
  18. <view class="arrow-area" v-if="!readonly">
  19. <view class="input-arrow"></view>
  20. </view>
  21. </view>
  22. </slot>
  23. </view>
  24. <view class="uni-data-tree-cover" v-if="isOpened" @click="handleClose"></view>
  25. <view class="uni-data-tree-dialog" v-if="isOpened">
  26. <view class="dialog-caption">
  27. <view class="title-area">
  28. <text class="dialog-title">{{popupTitle}}</text>
  29. </view>
  30. <view class="dialog-close" @click="handleClose">
  31. <view class="dialog-close-plus" data-id="close"></view>
  32. <view class="dialog-close-plus dialog-close-rotate" data-id="close"></view>
  33. </view>
  34. </view>
  35. <data-picker-view class="picker-view" ref="pickerView" v-model="value" :localdata="localdata" :preload="preload"
  36. :collection="collection" :field="field" :orderby="orderby" :where="where" :step-searh="stepSearh" :self-field="selfField"
  37. :parent-field="parentField" :managed-mode="true" @change="onchange" @datachange="ondatachange" @nodeclick="onnodeclick"></data-picker-view>
  38. </view>
  39. </view>
  40. </template>
  41. <script>
  42. import dataPicker from "../uni-data-pickerview/uni-data-picker.js"
  43. import DataPickerView from "../uni-data-pickerview/uni-data-pickerview.vue"
  44. /**
  45. * DataPicker 级联选择
  46. * @description 支持单列、和多列级联选择。列数没有限制,如果屏幕显示不全,顶部tab区域会左右滚动。
  47. * @tutorial https://ext.dcloud.net.cn/plugin?id=3796
  48. * @property {String} popup-title 弹出窗口标题
  49. * @property {Array} localdata 本地数据,参考
  50. * @property {Boolean} border = [true|false] 是否有边框
  51. * @property {Boolean} readonly = [true|false] 是否仅读
  52. * @property {Boolean} preload = [true|false] 是否预加载数据
  53. * @value true 开启预加载数据,点击弹出窗口后显示已加载数据
  54. * @value false 关闭预加载数据,点击弹出窗口后开始加载数据
  55. * @property {Boolean} step-searh = [true|false] 是否分布查询
  56. * @value true 启用分布查询,仅查询当前选中节点
  57. * @value false 关闭分布查询,一次查询出所有数据
  58. * @property {String|DBFieldString} self-field 分布查询当前字段名称
  59. * @property {String|DBFieldString} parent-field 分布查询父字段名称
  60. * @property {String|DBCollectionString} collection 表名
  61. * @property {String|DBFieldString} field 查询字段,多个字段用 `,` 分割
  62. * @property {String} orderby 排序字段及正序倒叙设置
  63. * @property {String|JQLString} where 查询条件
  64. * @event {Function} popupshow 弹出的选择窗口打开时触发此事件
  65. * @event {Function} popuphide 弹出的选择窗口关闭时触发此事件
  66. */
  67. export default {
  68. name: 'UniDataPicker',
  69. mixins: [dataPicker],
  70. components: {
  71. DataPickerView
  72. },
  73. props: {
  74. options: {
  75. type: [Object, Array],
  76. default () {
  77. return {}
  78. }
  79. },
  80. popupTitle: {
  81. type: String,
  82. default: '请选择'
  83. },
  84. placeholder: {
  85. type: String,
  86. default: '请选择'
  87. },
  88. heightMobile: {
  89. type: String,
  90. default: ''
  91. },
  92. readonly: {
  93. type: Boolean,
  94. default: false
  95. },
  96. border: {
  97. type: Boolean,
  98. default: true
  99. },
  100. split: {
  101. type: String,
  102. default: '/'
  103. }
  104. },
  105. data() {
  106. return {
  107. isOpened: false,
  108. inputSelected: []
  109. }
  110. },
  111. created() {
  112. this.form = this.getForm('uniForms')
  113. this.formItem = this.getForm('uniFormsItem')
  114. if (this.formItem) {
  115. if (this.formItem.name) {
  116. this.rename = this.formItem.name
  117. this.form.inputChildrens.push(this)
  118. }
  119. }
  120. this.$nextTick(() => {
  121. this.load()
  122. })
  123. },
  124. methods: {
  125. onPropsChange() {
  126. this._treeData = []
  127. this.selectedIndex = 0
  128. this.load()
  129. },
  130. load() {
  131. if (this.readonly) {
  132. this._processReadonly(this.localdata, this.value)
  133. return
  134. }
  135. if (this.isLocaldata) {
  136. this.loadData()
  137. this.inputSelected = this.selected.slice(0)
  138. } else if (!this.parentField && !this.selfField && this.value) {
  139. this.getNodeData(() => {
  140. this.inputSelected = this.selected.slice(0)
  141. })
  142. } else if (this.value.length) {
  143. this.getTreePath(() => {
  144. this.inputSelected = this.selected.slice(0)
  145. })
  146. }
  147. },
  148. getForm(name = 'uniForms') {
  149. let parent = this.$parent;
  150. let parentName = parent.$options.name;
  151. while (parentName !== name) {
  152. parent = parent.$parent;
  153. if (!parent) return false;
  154. parentName = parent.$options.name;
  155. }
  156. return parent;
  157. },
  158. show() {
  159. this.isOpened = true
  160. this.$nextTick(() => {
  161. this.$refs.pickerView.updateData({
  162. treeData: this._treeData,
  163. selected: this.selected,
  164. selectedIndex: this.selectedIndex
  165. })
  166. })
  167. this.$emit('popupopened')
  168. },
  169. hide() {
  170. this.isOpened = false
  171. this.$emit('popupclosed')
  172. },
  173. handleInput() {
  174. if (this.readonly) {
  175. return
  176. }
  177. this.show()
  178. },
  179. handleClose(e) {
  180. this.hide()
  181. },
  182. onnodeclick(e) {
  183. this.$emit('nodeclick', e)
  184. },
  185. ondatachange(e) {
  186. this._treeData = this.$refs.pickerView._treeData
  187. },
  188. onchange(e) {
  189. this.hide()
  190. this.inputSelected = e
  191. this._dispatchEvent(e)
  192. },
  193. _processReadonly(dataList, valueArray) {
  194. var isTree = dataList.findIndex((item) => {
  195. return item.children
  196. })
  197. if (isTree > -1) {
  198. if (Array.isArray(valueArray)) {
  199. let inputValue = valueArray[valueArray.length - 1]
  200. if (typeof inputValue === 'object' && inputValue.value) {
  201. inputValue = inputValue.value
  202. }
  203. }
  204. this.inputSelected = this._findNodePath(inputValue, this.localdata)
  205. return
  206. }
  207. let result = []
  208. for (let i = 0; i < valueArray.length; i++) {
  209. var value = valueArray[i]
  210. var item = dataList.find((v) => {
  211. return v.value == value
  212. })
  213. if (item) {
  214. result.push(item)
  215. }
  216. }
  217. if (result.length) {
  218. this.inputSelected = result
  219. }
  220. },
  221. _filterForArray(data, valueArray) {
  222. var result = []
  223. for (let i = 0; i < valueArray.length; i++) {
  224. var value = valueArray[i]
  225. var found = data.find((item) => {
  226. return item.value == value
  227. })
  228. if (found) {
  229. result.push(found)
  230. }
  231. }
  232. return result
  233. },
  234. _dispatchEvent(selected) {
  235. var value = new Array(selected.length)
  236. for (var i = 0; i < selected.length; i++) {
  237. value[i] = selected[i].value
  238. }
  239. if (this.formItem) {
  240. const item = selected[selected.length - 1]
  241. this.formItem.setValue(item.value)
  242. }
  243. this.$emit('change', {
  244. detail: {
  245. value: selected
  246. }
  247. })
  248. }
  249. }
  250. }
  251. </script>
  252. <style scoped>
  253. .uni-data-tree {
  254. position: relative;
  255. font-size: 14px;
  256. }
  257. .error-text {
  258. color: #DD524D;
  259. }
  260. .input-value {
  261. /* #ifndef APP-NVUE */
  262. display: flex;
  263. /* #endif */
  264. flex-direction: row;
  265. align-items: center;
  266. flex-wrap: nowrap;
  267. font-size: 14px;
  268. line-height: 38px;
  269. padding: 0 5px;
  270. overflow: hidden;
  271. /* #ifdef APP-NVUE */
  272. height: 40px;
  273. /* #endif */
  274. }
  275. .input-value-border {
  276. border: 1px solid #e5e5e5;
  277. border-radius: 5px;
  278. }
  279. .selected-area {
  280. flex: 1;
  281. overflow: hidden;
  282. /* #ifndef APP-NVUE */
  283. display: flex;
  284. /* #endif */
  285. flex-direction: row;
  286. }
  287. .load-more {
  288. /* #ifndef APP-NVUE */
  289. margin-right: auto;
  290. /* #endif */
  291. /* #ifdef APP-NVUE */
  292. width: 40px;
  293. /* #endif */
  294. }
  295. .selected-list {
  296. /* #ifndef APP-NVUE */
  297. display: flex;
  298. /* #endif */
  299. flex-direction: row;
  300. flex-wrap: nowrap;
  301. padding: 0 5px;
  302. }
  303. .selected-item {
  304. flex-direction: row;
  305. padding: 0 1px;
  306. /* #ifndef APP-NVUE */
  307. white-space: nowrap;
  308. /* #endif */
  309. }
  310. .placeholder {
  311. color: grey;
  312. }
  313. .input-split-line {
  314. opacity: .5;
  315. }
  316. .arrow-area {
  317. position: relative;
  318. width: 20px;
  319. /* #ifndef APP-NVUE */
  320. margin-left: auto;
  321. display: flex;
  322. /* #endif */
  323. justify-content: center;
  324. transform: rotate(-45deg);
  325. transform-origin: center;
  326. }
  327. .input-arrow {
  328. width: 7px;
  329. height: 7px;
  330. border-left: 1px solid #999;
  331. border-bottom: 1px solid #999;
  332. }
  333. .uni-data-tree-cover {
  334. position: fixed;
  335. left: 0;
  336. top: 0;
  337. right: 0;
  338. bottom: 0;
  339. background-color: rgba(0, 0, 0, .4);
  340. /* #ifndef APP-NVUE */
  341. display: flex;
  342. /* #endif */
  343. flex-direction: column;
  344. z-index: 100;
  345. }
  346. .uni-data-tree-dialog {
  347. position: fixed;
  348. left: 0;
  349. top: 20%;
  350. right: 0;
  351. bottom: 0;
  352. background-color: #FFFFFF;
  353. border-top-left-radius: 10px;
  354. border-top-right-radius: 10px;
  355. /* #ifndef APP-NVUE */
  356. display: flex;
  357. /* #endif */
  358. flex-direction: column;
  359. z-index: 102;
  360. overflow: hidden;
  361. /* #ifdef APP-NVUE */
  362. width: 750rpx;
  363. /* #endif */
  364. }
  365. .dialog-caption {
  366. position: relative;
  367. /* #ifndef APP-NVUE */
  368. display: flex;
  369. /* #endif */
  370. flex-direction: row;
  371. border-bottom: 1px solid #f0f0f0;
  372. }
  373. .title-area {
  374. /* #ifndef APP-NVUE */
  375. display: flex;
  376. /* #endif */
  377. align-items: center;
  378. /* #ifndef APP-NVUE */
  379. margin: auto;
  380. /* #endif */
  381. padding: 0 10px;
  382. }
  383. .dialog-title {
  384. font-weight: bold;
  385. line-height: 44px;
  386. }
  387. .dialog-close {
  388. position: absolute;
  389. top: 0;
  390. right: 0;
  391. bottom: 0;
  392. /* #ifndef APP-NVUE */
  393. display: flex;
  394. /* #endif */
  395. flex-direction: row;
  396. align-items: center;
  397. padding: 0 15px;
  398. }
  399. .dialog-close-plus {
  400. width: 16px;
  401. height: 2px;
  402. background-color: #666;
  403. border-radius: 2px;
  404. transform: rotate(45deg);
  405. }
  406. .dialog-close-rotate {
  407. position: absolute;
  408. transform: rotate(-45deg);
  409. }
  410. .picker-view {
  411. flex: 1;
  412. overflow: hidden;
  413. }
  414. /* #ifdef H5 */
  415. @media all and (min-width: 768px) {
  416. .uni-data-tree-cover {
  417. background-color: transparent;
  418. }
  419. .uni-data-tree-dialog {
  420. position: absolute;
  421. top: 100%;
  422. height: auto;
  423. min-height: 400px;
  424. max-height: 50vh;
  425. background-color: #fff;
  426. border-radius: 5px;
  427. box-shadow: 0 0 20px 5px rgba(0, 0, 0, .3);
  428. }
  429. .dialog-caption {
  430. display: none;
  431. }
  432. }
  433. /* #endif */
  434. </style>