uni-data-picker.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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="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"
  43. :map="map" :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'],
  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.form = this.getForm('uniForms')
  129. this.formItem = this.getForm('uniFormsItem')
  130. if (this.formItem) {
  131. if (this.formItem.name) {
  132. this.rename = this.formItem.name
  133. this.form.inputChildrens.push(this)
  134. }
  135. }
  136. this.$nextTick(() => {
  137. this.load()
  138. })
  139. },
  140. methods: {
  141. clear() {
  142. this.inputSelected.splice(0)
  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. if (this.isLocaldata) {
  156. this.loadData()
  157. this.inputSelected = this.selected.slice(0)
  158. } else if (!this.parentField && !this.selfField && this.dataValue) {
  159. this.getNodeData(() => {
  160. this.inputSelected = this.selected.slice(0)
  161. })
  162. } else if (this.dataValue.length) {
  163. this.getTreePath(() => {
  164. this.inputSelected = this.selected.slice(0)
  165. })
  166. }
  167. },
  168. getForm(name = 'uniForms') {
  169. let parent = this.$parent;
  170. let parentName = parent.$options.name;
  171. while (parentName !== name) {
  172. parent = parent.$parent;
  173. if (!parent) return false;
  174. parentName = parent.$options.name;
  175. }
  176. return parent;
  177. },
  178. show() {
  179. this.isOpened = true
  180. this.$nextTick(() => {
  181. this.$refs.pickerView.updateData({
  182. treeData: this._treeData,
  183. selected: this.selected,
  184. selectedIndex: this.selectedIndex
  185. })
  186. })
  187. this.$emit('popupopened')
  188. },
  189. hide() {
  190. this.isOpened = false
  191. this.$emit('popupclosed')
  192. },
  193. handleInput() {
  194. if (this.readonly) {
  195. return
  196. }
  197. this.show()
  198. },
  199. handleClose(e) {
  200. this.hide()
  201. },
  202. onnodeclick(e) {
  203. this.$emit('nodeclick', e)
  204. },
  205. ondatachange(e) {
  206. this._treeData = this.$refs.pickerView._treeData
  207. },
  208. onchange(e) {
  209. this.hide()
  210. this.inputSelected = e
  211. this._dispatchEvent(e)
  212. },
  213. _processReadonly(dataList, value) {
  214. var isTree = dataList.findIndex((item) => {
  215. return item.children
  216. })
  217. if (isTree > -1) {
  218. let inputValue
  219. if (Array.isArray(value)) {
  220. inputValue = value[value.length - 1]
  221. if (typeof inputValue === 'object' && inputValue.value) {
  222. inputValue = inputValue.value
  223. }
  224. } else {
  225. inputValue = value
  226. }
  227. this.inputSelected = this._findNodePath(inputValue, this.localdata)
  228. return
  229. }
  230. let result = []
  231. for (let i = 0; i < value.length; i++) {
  232. var val = value[i]
  233. var item = dataList.find((v) => {
  234. return v.value == val
  235. })
  236. if (item) {
  237. result.push(item)
  238. }
  239. }
  240. if (result.length) {
  241. this.inputSelected = result
  242. }
  243. },
  244. _filterForArray(data, valueArray) {
  245. var result = []
  246. for (let i = 0; i < valueArray.length; i++) {
  247. var value = valueArray[i]
  248. var found = data.find((item) => {
  249. return item.value == value
  250. })
  251. if (found) {
  252. result.push(found)
  253. }
  254. }
  255. return result
  256. },
  257. _dispatchEvent(selected) {
  258. let item = {}
  259. if (selected.length) {
  260. var value = new Array(selected.length)
  261. for (var i = 0; i < selected.length; i++) {
  262. value[i] = selected[i].value
  263. }
  264. item = selected[selected.length - 1]
  265. } else {
  266. item.value = ''
  267. }
  268. if (this.formItem) {
  269. this.formItem.setValue(item.value)
  270. }
  271. this.$emit('input', item.value)
  272. this.$emit('update:modelValue', item.value)
  273. this.$emit('change', {
  274. detail: {
  275. value: selected
  276. }
  277. })
  278. }
  279. }
  280. }
  281. </script>
  282. <style scoped>
  283. .uni-data-tree {
  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: 38px;
  299. padding: 0 5px;
  300. overflow: hidden;
  301. /* #ifdef APP-NVUE */
  302. height: 40px;
  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. .placeholder {
  341. color: grey;
  342. }
  343. .input-split-line {
  344. opacity: .5;
  345. }
  346. .arrow-area {
  347. position: relative;
  348. width: 20px;
  349. /* #ifndef APP-NVUE */
  350. margin-bottom: 5px;
  351. margin-left: auto;
  352. display: flex;
  353. /* #endif */
  354. justify-content: center;
  355. transform: rotate(-45deg);
  356. transform-origin: center;
  357. }
  358. .input-arrow {
  359. width: 7px;
  360. height: 7px;
  361. border-left: 1px solid #999;
  362. border-bottom: 1px solid #999;
  363. }
  364. .uni-data-tree-cover {
  365. position: fixed;
  366. left: 0;
  367. top: 0;
  368. right: 0;
  369. bottom: 0;
  370. background-color: rgba(0, 0, 0, .4);
  371. /* #ifndef APP-NVUE */
  372. display: flex;
  373. /* #endif */
  374. flex-direction: column;
  375. z-index: 100;
  376. }
  377. .uni-data-tree-dialog {
  378. position: fixed;
  379. left: 0;
  380. top: 20%;
  381. right: 0;
  382. bottom: 0;
  383. background-color: #FFFFFF;
  384. border-top-left-radius: 10px;
  385. border-top-right-radius: 10px;
  386. /* #ifndef APP-NVUE */
  387. display: flex;
  388. /* #endif */
  389. flex-direction: column;
  390. z-index: 102;
  391. overflow: hidden;
  392. /* #ifdef APP-NVUE */
  393. width: 750rpx;
  394. /* #endif */
  395. }
  396. .dialog-caption {
  397. position: relative;
  398. /* #ifndef APP-NVUE */
  399. display: flex;
  400. /* #endif */
  401. flex-direction: row;
  402. border-bottom: 1px solid #f0f0f0;
  403. }
  404. .title-area {
  405. /* #ifndef APP-NVUE */
  406. display: flex;
  407. /* #endif */
  408. align-items: center;
  409. /* #ifndef APP-NVUE */
  410. margin: auto;
  411. /* #endif */
  412. padding: 0 10px;
  413. }
  414. .dialog-title {
  415. font-weight: bold;
  416. line-height: 44px;
  417. }
  418. .dialog-close {
  419. position: absolute;
  420. top: 0;
  421. right: 0;
  422. bottom: 0;
  423. /* #ifndef APP-NVUE */
  424. display: flex;
  425. /* #endif */
  426. flex-direction: row;
  427. align-items: center;
  428. padding: 0 15px;
  429. }
  430. .dialog-close-plus {
  431. width: 16px;
  432. height: 2px;
  433. background-color: #666;
  434. border-radius: 2px;
  435. transform: rotate(45deg);
  436. }
  437. .dialog-close-rotate {
  438. position: absolute;
  439. transform: rotate(-45deg);
  440. }
  441. .picker-view {
  442. flex: 1;
  443. overflow: hidden;
  444. }
  445. /* #ifdef H5 */
  446. @media all and (min-width: 768px) {
  447. .uni-data-tree-cover {
  448. background-color: transparent;
  449. }
  450. .uni-data-tree-dialog {
  451. position: absolute;
  452. top: 100%;
  453. height: auto;
  454. min-height: 400px;
  455. max-height: 50vh;
  456. background-color: #fff;
  457. border-radius: 5px;
  458. box-shadow: 0 0 20px 5px rgba(0, 0, 0, .3);
  459. }
  460. .dialog-caption {
  461. display: none;
  462. }
  463. .icon-clear {
  464. margin-right: 5px;
  465. }
  466. }
  467. /* #endif */
  468. </style>