uni-data-picker.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. * uni-data-picker
  46. * @description uni-data-picker
  47. * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-data-picker
  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.value.length) {
  139. this.getTreePath(() => {
  140. this.inputSelected = this.selected.slice(0)
  141. })
  142. }
  143. },
  144. getForm(name = 'uniForms') {
  145. let parent = this.$parent;
  146. let parentName = parent.$options.name;
  147. while (parentName !== name) {
  148. parent = parent.$parent;
  149. if (!parent) return false;
  150. parentName = parent.$options.name;
  151. }
  152. return parent;
  153. },
  154. show() {
  155. this.isOpened = true
  156. this.$nextTick(() => {
  157. this.$refs.pickerView.updateData({
  158. treeData: this._treeData,
  159. selected: this.selected,
  160. selectedIndex: this.selectedIndex
  161. })
  162. })
  163. this.$emit('popupopened')
  164. },
  165. hide() {
  166. this.isOpened = false
  167. this.$emit('popupclosed')
  168. },
  169. handleInput() {
  170. if (this.readonly) {
  171. return
  172. }
  173. this.show()
  174. },
  175. handleClose(e) {
  176. this.hide()
  177. },
  178. onnodeclick(e) {
  179. this.$emit('nodeclick', e)
  180. },
  181. ondatachange(e) {
  182. this._treeData = this.$refs.pickerView._treeData
  183. },
  184. onchange(e) {
  185. this.hide()
  186. this.inputSelected = e
  187. this._dispatchEvent(e)
  188. },
  189. _processReadonly(dataList, valueArray) {
  190. var isTree = dataList.findIndex((item) => {
  191. return item.children
  192. })
  193. if (isTree > -1) {
  194. if (Array.isArray(valueArray)) {
  195. let inputValue = valueArray[valueArray.length - 1]
  196. if (typeof inputValue === 'object' && inputValue.value) {
  197. inputValue = inputValue.value
  198. }
  199. }
  200. this.inputSelected = this._findNodePath(inputValue, this.localdata)
  201. return
  202. }
  203. let result = []
  204. for (let i = 0; i < valueArray.length; i++) {
  205. var value = valueArray[i]
  206. var item = dataList.find((v) => {
  207. return v.value == value
  208. })
  209. if (item) {
  210. result.push(item)
  211. }
  212. }
  213. if (result.length) {
  214. this.inputSelected = result
  215. }
  216. },
  217. _filterForArray(data, valueArray) {
  218. var result = []
  219. for (let i = 0; i < valueArray.length; i++) {
  220. var value = valueArray[i]
  221. var found = data.find((item) => {
  222. return item.value == value
  223. })
  224. if (found) {
  225. result.push(found)
  226. }
  227. }
  228. return result
  229. },
  230. _dispatchEvent(selected) {
  231. var value = new Array(selected.length)
  232. for (var i = 0; i < selected.length; i++) {
  233. value[i] = selected[i].value
  234. }
  235. if (this.formItem) {
  236. const item = selected[selected.length - 1]
  237. this.formItem.setValue(item.value)
  238. }
  239. this.$emit('change', {
  240. detail: {
  241. value: selected
  242. }
  243. })
  244. }
  245. }
  246. }
  247. </script>
  248. <style scoped>
  249. .uni-data-tree {
  250. position: relative;
  251. font-size: 14px;
  252. }
  253. .error-text {
  254. color: #DD524D;
  255. }
  256. .input-value {
  257. /* #ifndef APP-NVUE */
  258. display: flex;
  259. /* #endif */
  260. flex-direction: row;
  261. align-items: center;
  262. flex-wrap: nowrap;
  263. font-size: 14px;
  264. line-height: 38px;
  265. padding: 0 5px;
  266. overflow: hidden;
  267. /* #ifdef APP-NVUE */
  268. height: 40px;
  269. /* #endif */
  270. }
  271. .input-value-border {
  272. border: 1px solid #e5e5e5;
  273. border-radius: 5px;
  274. }
  275. .selected-area {
  276. flex: 1;
  277. overflow: hidden;
  278. /* #ifndef APP-NVUE */
  279. display: flex;
  280. /* #endif */
  281. flex-direction: row;
  282. }
  283. .load-more {
  284. /* #ifndef APP-NVUE */
  285. margin-right: auto;
  286. /* #endif */
  287. /* #ifdef APP-NVUE */
  288. width: 40px;
  289. /* #endif */
  290. }
  291. .selected-list {
  292. /* #ifndef APP-NVUE */
  293. display: flex;
  294. /* #endif */
  295. flex-direction: row;
  296. flex-wrap: nowrap;
  297. padding: 0 5px;
  298. }
  299. .selected-item {
  300. flex-direction: row;
  301. padding: 0 1px;
  302. /* #ifndef APP-NVUE */
  303. white-space: nowrap;
  304. /* #endif */
  305. }
  306. .placeholder {
  307. color: grey;
  308. }
  309. .input-split-line {
  310. opacity: .5;
  311. }
  312. .arrow-area {
  313. position: relative;
  314. width: 20px;
  315. /* #ifndef APP-NVUE */
  316. margin-left: auto;
  317. display: flex;
  318. /* #endif */
  319. justify-content: center;
  320. transform: rotate(-45deg);
  321. transform-origin: center;
  322. }
  323. .input-arrow {
  324. width: 7px;
  325. height: 7px;
  326. border-left: 1px solid #999;
  327. border-bottom: 1px solid #999;
  328. }
  329. .uni-data-tree-cover {
  330. position: fixed;
  331. left: 0;
  332. top: 0;
  333. right: 0;
  334. bottom: 0;
  335. background-color: rgba(0, 0, 0, .4);
  336. /* #ifndef APP-NVUE */
  337. display: flex;
  338. /* #endif */
  339. flex-direction: column;
  340. z-index: 100;
  341. }
  342. .uni-data-tree-dialog {
  343. position: fixed;
  344. left: 0;
  345. top: 20%;
  346. right: 0;
  347. bottom: 0;
  348. background-color: #FFFFFF;
  349. border-top-left-radius: 10px;
  350. border-top-right-radius: 10px;
  351. /* #ifndef APP-NVUE */
  352. display: flex;
  353. /* #endif */
  354. flex-direction: column;
  355. z-index: 102;
  356. overflow: hidden;
  357. /* #ifdef APP-NVUE */
  358. width: 750rpx;
  359. /* #endif */
  360. }
  361. .dialog-caption {
  362. position: relative;
  363. /* #ifndef APP-NVUE */
  364. display: flex;
  365. /* #endif */
  366. flex-direction: row;
  367. border-bottom: 1px solid #f0f0f0;
  368. }
  369. .title-area {
  370. /* #ifndef APP-NVUE */
  371. display: flex;
  372. /* #endif */
  373. align-items: center;
  374. /* #ifndef APP-NVUE */
  375. margin: auto;
  376. /* #endif */
  377. padding: 0 10px;
  378. }
  379. .dialog-title {
  380. font-weight: bold;
  381. line-height: 44px;
  382. }
  383. .dialog-close {
  384. position: absolute;
  385. top: 0;
  386. right: 0;
  387. bottom: 0;
  388. /* #ifndef APP-NVUE */
  389. display: flex;
  390. /* #endif */
  391. flex-direction: row;
  392. align-items: center;
  393. padding: 0 15px;
  394. }
  395. .dialog-close-plus {
  396. width: 16px;
  397. height: 2px;
  398. background-color: #666;
  399. border-radius: 2px;
  400. transform: rotate(45deg);
  401. }
  402. .dialog-close-rotate {
  403. position: absolute;
  404. transform: rotate(-45deg);
  405. }
  406. .picker-view {
  407. flex: 1;
  408. overflow: hidden;
  409. }
  410. /* #ifdef H5 */
  411. @media all and (min-width: 768px) {
  412. .uni-data-tree-cover {
  413. background-color: transparent;
  414. }
  415. .uni-data-tree-dialog {
  416. position: absolute;
  417. top: 100%;
  418. height: auto;
  419. min-height: 400px;
  420. max-height: 50vh;
  421. background-color: #fff;
  422. border-radius: 5px;
  423. box-shadow: 0 0 20px 5px rgba(0, 0, 0, .3);
  424. }
  425. .dialog-caption {
  426. display: none;
  427. }
  428. }
  429. /* #endif */
  430. </style>