uni-data-picker.vue 11 KB

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