song-data-picker.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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-if="clearIcon && !readonly && inputSelected.length" class="icon-clear" @click.stop="clear">
  20. <uni-icons type="clear" color="#e1e1e1" size="14"></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. <!-- search增强-start -->
  41. <view class="dialog-caption" v-if="openSearch">
  42. <view style="width: 80%; margin: 10px 10%;">
  43. <uni-easyinput type="text" suffixIcon="search" v-model="searchWord" placeholder="输入关键字搜索"
  44. @iconClick="searchBegin" @confirm="searchBegin"></uni-easyinput>
  45. </view>
  46. </view>
  47. <view class="picker-view" style="overflow-y:auto" v-if="showSearchResult">
  48. <view v-show="searchResult.length">
  49. <uni-list-item v-for="(item, index) in searchResult" :key="index" :title="item.text" clickable link
  50. @click="clickSearchResult(item)"></uni-list-item>
  51. </view>
  52. <view v-show="!searchResult.length" style="text-align: center;line-height:200px;"><text
  53. style="color: #999999">--未发现任何匹配数据,请重新输入--</text></view>
  54. </view>
  55. <!-- search增强-end -->
  56. <data-picker-view v-show="!showSearchResult" class="picker-view" ref="pickerView" v-model="dataValue" :localdata="localdata"
  57. :preload="preload" :collection="collection" :field="field" :orderby="orderby" :where="where"
  58. :step-searh="stepSearh" :self-field="selfField" :parent-field="parentField" :managed-mode="true"
  59. :map="map" :ellipsis="ellipsis" @change="onchange" @datachange="ondatachange" @nodeclick="onnodeclick">
  60. </data-picker-view>
  61. </view>
  62. </view>
  63. </template>
  64. <script>
  65. import dataPicker from "../song-data-pickerview/song-data-picker.js"
  66. import DataPickerView from "../song-data-pickerview/song-data-pickerview.vue"
  67. import searchMixin from './searchMixin.js'
  68. /**
  69. * DataPicker 级联选择
  70. * @description 支持单列、和多列级联选择。列数没有限制,如果屏幕显示不全,顶部tab区域会左右滚动。
  71. * @tutorial https://ext.dcloud.net.cn/plugin?id=3796
  72. * @property {String} popup-title 弹出窗口标题
  73. * @property {Array} localdata 本地数据,参考
  74. * @property {Boolean} border = [true|false] 是否有边框
  75. * @property {Boolean} readonly = [true|false] 是否仅读
  76. * @property {Boolean} preload = [true|false] 是否预加载数据
  77. * @value true 开启预加载数据,点击弹出窗口后显示已加载数据
  78. * @value false 关闭预加载数据,点击弹出窗口后开始加载数据
  79. * @property {Boolean} step-searh = [true|false] 是否分布查询
  80. * @value true 启用分布查询,仅查询当前选中节点
  81. * @value false 关闭分布查询,一次查询出所有数据
  82. * @property {String|DBFieldString} self-field 分布查询当前字段名称
  83. * @property {String|DBFieldString} parent-field 分布查询父字段名称
  84. * @property {String|DBCollectionString} collection 表名
  85. * @property {String|DBFieldString} field 查询字段,多个字段用 `,` 分割
  86. * @property {String} orderby 排序字段及正序倒叙设置
  87. * @property {String|JQLString} where 查询条件
  88. * @event {Function} popupshow 弹出的选择窗口打开时触发此事件
  89. * @event {Function} popuphide 弹出的选择窗口关闭时触发此事件
  90. */
  91. export default {
  92. name: 'UniDataPicker',
  93. emits: ['popupopened', 'popupclosed', 'nodeclick', 'input', 'change', 'update:modelValue'],
  94. mixins: [dataPicker, searchMixin],
  95. components: {
  96. DataPickerView
  97. },
  98. props: {
  99. options: {
  100. type: [Object, Array],
  101. default () {
  102. return {}
  103. }
  104. },
  105. popupTitle: {
  106. type: String,
  107. default: '请选择'
  108. },
  109. placeholder: {
  110. type: String,
  111. default: '请选择'
  112. },
  113. heightMobile: {
  114. type: String,
  115. default: ''
  116. },
  117. readonly: {
  118. type: Boolean,
  119. default: false
  120. },
  121. clearIcon: {
  122. type: Boolean,
  123. default: true
  124. },
  125. border: {
  126. type: Boolean,
  127. default: true
  128. },
  129. split: {
  130. type: String,
  131. default: '/'
  132. },
  133. ellipsis: {
  134. type: Boolean,
  135. default: true
  136. }
  137. },
  138. data() {
  139. return {
  140. isOpened: false,
  141. inputSelected: []
  142. }
  143. },
  144. created() {
  145. this.form = this.getForm('uniForms')
  146. this.formItem = this.getForm('uniFormsItem')
  147. if (this.formItem) {
  148. if (this.formItem.name) {
  149. this.rename = this.formItem.name
  150. this.form.inputChildrens.push(this)
  151. }
  152. }
  153. this.$nextTick(() => {
  154. this.load()
  155. })
  156. },
  157. methods: {
  158. clear() {
  159. this.inputSelected.splice(0)
  160. this._dispatchEvent([])
  161. },
  162. onPropsChange() {
  163. this._treeData = []
  164. this.selectedIndex = 0
  165. this.load()
  166. },
  167. load() {
  168. if (this.readonly) {
  169. this._processReadonly(this.localdata, this.dataValue)
  170. return
  171. }
  172. if (this.isLocaldata) {
  173. this.loadData()
  174. this.inputSelected = this.selected.slice(0)
  175. // 只在本地数据时搜索生效
  176. this.initSearchDatas();
  177. } else if (!this.parentField && !this.selfField && this.hasValue) {
  178. this.getNodeData(() => {
  179. this.inputSelected = this.selected.slice(0)
  180. })
  181. } else if (this.hasValue) {
  182. this.getTreePath(() => {
  183. this.inputSelected = this.selected.slice(0)
  184. })
  185. }
  186. },
  187. getForm(name = 'uniForms') {
  188. let parent = this.$parent;
  189. let parentName = parent.$options.name;
  190. while (parentName !== name) {
  191. parent = parent.$parent;
  192. if (!parent) return false;
  193. parentName = parent.$options.name;
  194. }
  195. return parent;
  196. },
  197. show() {
  198. this.isOpened = true
  199. setTimeout(() => {
  200. this.$refs.pickerView.updateData({
  201. treeData: this._treeData,
  202. selected: this.selected,
  203. selectedIndex: this.selectedIndex
  204. })
  205. }, 200)
  206. this.$emit('popupopened')
  207. },
  208. hide() {
  209. this.isOpened = false
  210. this.$emit('popupclosed')
  211. this.closeSearchDom();
  212. },
  213. handleInput() {
  214. if (this.readonly) {
  215. return
  216. }
  217. this.show()
  218. },
  219. handleClose(e) {
  220. this.hide()
  221. },
  222. onnodeclick(e) {
  223. this.$emit('nodeclick', e)
  224. },
  225. ondatachange(e) {
  226. this._treeData = this.$refs.pickerView._treeData
  227. },
  228. onchange(e) {
  229. this.hide()
  230. this.inputSelected = e
  231. this._dispatchEvent(e)
  232. },
  233. _processReadonly(dataList, value) {
  234. var isTree = dataList.findIndex((item) => {
  235. return item.children
  236. })
  237. if (isTree > -1) {
  238. let inputValue
  239. if (Array.isArray(value)) {
  240. inputValue = value[value.length - 1]
  241. if (typeof inputValue === 'object' && inputValue.value) {
  242. inputValue = inputValue.value
  243. }
  244. } else {
  245. inputValue = value
  246. }
  247. this.inputSelected = this._findNodePath(inputValue, this.localdata)
  248. return
  249. }
  250. if (!this.hasValue) {
  251. this.inputSelected = []
  252. return
  253. }
  254. let result = []
  255. for (let i = 0; i < value.length; i++) {
  256. var val = value[i]
  257. var item = dataList.find((v) => {
  258. return v.value == val
  259. })
  260. if (item) {
  261. result.push(item)
  262. }
  263. }
  264. if (result.length) {
  265. this.inputSelected = result
  266. }
  267. },
  268. _filterForArray(data, valueArray) {
  269. var result = []
  270. for (let i = 0; i < valueArray.length; i++) {
  271. var value = valueArray[i]
  272. var found = data.find((item) => {
  273. return item.value == value
  274. })
  275. if (found) {
  276. result.push(found)
  277. }
  278. }
  279. return result
  280. },
  281. _dispatchEvent(selected) {
  282. let item = {}
  283. if (selected.length) {
  284. var value = new Array(selected.length)
  285. for (var i = 0; i < selected.length; i++) {
  286. value[i] = selected[i].value
  287. }
  288. item = selected[selected.length - 1]
  289. } else {
  290. item.value = ''
  291. }
  292. if (this.formItem) {
  293. this.formItem.setValue(item.value)
  294. }
  295. this.$emit('input', item.value)
  296. this.$emit('update:modelValue', item.value)
  297. this.$emit('change', {
  298. detail: {
  299. value: selected
  300. }
  301. })
  302. }
  303. }
  304. }
  305. </script>
  306. <style>
  307. .uni-data-tree {
  308. position: relative;
  309. font-size: 14px;
  310. }
  311. .error-text {
  312. color: #DD524D;
  313. }
  314. .input-value {
  315. /* #ifndef APP-NVUE */
  316. display: flex;
  317. /* #endif */
  318. flex-direction: row;
  319. align-items: center;
  320. flex-wrap: nowrap;
  321. font-size: 14px;
  322. line-height: 38px;
  323. padding: 0 5px;
  324. overflow: hidden;
  325. /* #ifdef APP-NVUE */
  326. height: 40px;
  327. /* #endif */
  328. }
  329. .input-value-border {
  330. border: 1px solid #e5e5e5;
  331. border-radius: 5px;
  332. }
  333. .selected-area {
  334. flex: 1;
  335. overflow: hidden;
  336. /* #ifndef APP-NVUE */
  337. display: flex;
  338. /* #endif */
  339. flex-direction: row;
  340. }
  341. .load-more {
  342. /* #ifndef APP-NVUE */
  343. margin-right: auto;
  344. /* #endif */
  345. /* #ifdef APP-NVUE */
  346. width: 40px;
  347. /* #endif */
  348. }
  349. .selected-list {
  350. /* #ifndef APP-NVUE */
  351. display: flex;
  352. /* #endif */
  353. flex-direction: row;
  354. flex-wrap: nowrap;
  355. padding: 0 5px;
  356. }
  357. .selected-item {
  358. flex-direction: row;
  359. padding: 0 1px;
  360. /* #ifndef APP-NVUE */
  361. white-space: nowrap;
  362. /* #endif */
  363. }
  364. .placeholder {
  365. color: grey;
  366. }
  367. .input-split-line {
  368. opacity: .5;
  369. }
  370. .arrow-area {
  371. position: relative;
  372. width: 20px;
  373. /* #ifndef APP-NVUE */
  374. margin-bottom: 5px;
  375. margin-left: auto;
  376. display: flex;
  377. /* #endif */
  378. justify-content: center;
  379. transform: rotate(-45deg);
  380. transform-origin: center;
  381. }
  382. .input-arrow {
  383. width: 7px;
  384. height: 7px;
  385. border-left: 1px solid #999;
  386. border-bottom: 1px solid #999;
  387. }
  388. .uni-data-tree-cover {
  389. position: fixed;
  390. left: 0;
  391. top: 0;
  392. right: 0;
  393. bottom: 0;
  394. background-color: rgba(0, 0, 0, .4);
  395. /* #ifndef APP-NVUE */
  396. display: flex;
  397. /* #endif */
  398. flex-direction: column;
  399. z-index: 100;
  400. }
  401. .uni-data-tree-dialog {
  402. position: fixed;
  403. left: 0;
  404. top: 20%;
  405. right: 0;
  406. bottom: 0;
  407. background-color: #FFFFFF;
  408. border-top-left-radius: 10px;
  409. border-top-right-radius: 10px;
  410. /* #ifndef APP-NVUE */
  411. display: flex;
  412. /* #endif */
  413. flex-direction: column;
  414. z-index: 102;
  415. overflow: hidden;
  416. /* #ifdef APP-NVUE */
  417. width: 750rpx;
  418. /* #endif */
  419. }
  420. .dialog-caption {
  421. position: relative;
  422. /* #ifndef APP-NVUE */
  423. display: flex;
  424. /* #endif */
  425. flex-direction: row;
  426. /* border-bottom: 1px solid #f0f0f0; */
  427. }
  428. .title-area {
  429. /* #ifndef APP-NVUE */
  430. display: flex;
  431. /* #endif */
  432. align-items: center;
  433. /* #ifndef APP-NVUE */
  434. margin: auto;
  435. /* #endif */
  436. padding: 0 10px;
  437. }
  438. .dialog-title {
  439. /* font-weight: bold; */
  440. line-height: 44px;
  441. }
  442. .dialog-close {
  443. position: absolute;
  444. top: 0;
  445. right: 0;
  446. bottom: 0;
  447. /* #ifndef APP-NVUE */
  448. display: flex;
  449. /* #endif */
  450. flex-direction: row;
  451. align-items: center;
  452. padding: 0 15px;
  453. }
  454. .dialog-close-plus {
  455. width: 16px;
  456. height: 2px;
  457. background-color: #666;
  458. border-radius: 2px;
  459. transform: rotate(45deg);
  460. }
  461. .dialog-close-rotate {
  462. position: absolute;
  463. transform: rotate(-45deg);
  464. }
  465. .picker-view {
  466. flex: 1;
  467. overflow: hidden;
  468. }
  469. /* #ifdef H5 */
  470. @media all and (min-width: 768px) {
  471. .uni-data-tree-cover {
  472. background-color: transparent;
  473. }
  474. .uni-data-tree-dialog {
  475. position: absolute;
  476. top: 55px;
  477. height: auto;
  478. min-height: 400px;
  479. max-height: 50vh;
  480. background-color: #fff;
  481. border: 1px solid #EBEEF5;
  482. box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  483. border-radius: 4px;
  484. overflow: unset;
  485. }
  486. .dialog-caption {
  487. display: none;
  488. }
  489. .icon-clear {
  490. margin-right: 5px;
  491. }
  492. }
  493. /* #endif */
  494. /* picker 弹出层通用的指示小三角, todo:扩展至上下左右方向定位 */
  495. /* #ifndef APP-NVUE */
  496. .uni-popper__arrow,
  497. .uni-popper__arrow::after {
  498. position: absolute;
  499. display: block;
  500. width: 0;
  501. height: 0;
  502. border-color: transparent;
  503. border-style: solid;
  504. border-width: 6px;
  505. }
  506. .uni-popper__arrow {
  507. filter: drop-shadow(0 2px 12px rgba(0, 0, 0, 0.03));
  508. top: -6px;
  509. left: 10%;
  510. margin-right: 3px;
  511. border-top-width: 0;
  512. border-bottom-color: #EBEEF5;
  513. }
  514. .uni-popper__arrow::after {
  515. content: " ";
  516. top: 1px;
  517. margin-left: -6px;
  518. border-top-width: 0;
  519. border-bottom-color: #fff;
  520. }
  521. /* #endif */
  522. </style>