uni-data-pickerview.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <template>
  2. <view class="uni-data-pickerview">
  3. <scroll-view class="selected-area" scroll-x="true" scroll-y="false" :show-scrollbar="false">
  4. <view class="selected-list">
  5. <view class="selected-item" :class="{'selected-item-active':index==selectedIndex}" v-for="(item,index) in selected"
  6. :key="index" v-if="item.text" @click="handleSelect(index)">
  7. <text class="">{{item.text}}</text>
  8. </view>
  9. </view>
  10. </scroll-view>
  11. <view class="tab-c">
  12. <scroll-view class="list" v-for="(child, i) in dataList" :key="i" v-if="i==selectedIndex" :scroll-y="true">
  13. <view class="item" :class="{'is-disabled': !!item.disable}" v-for="(item, j) in child" :key="j" @click="handleNodeClick(item, i, j)">
  14. <text class="item-text">{{item.text}}</text>
  15. <view class="check" v-if="selected.length > i && item.value == selected[i].value"></view>
  16. </view>
  17. </scroll-view>
  18. <view class="loading-cover" v-if="loading">
  19. <uni-load-more class="load-more" :contentText="loadMore" status="loading"></uni-load-more>
  20. </view>
  21. <view class="error-message" v-if="errorMessage">
  22. <text class="error-text">{{errorMessage}}</text>
  23. </view>
  24. </view>
  25. </view>
  26. </template>
  27. <script>
  28. import dataPicker from "./uni-data-picker.js"
  29. /**
  30. * uni-data-pickerview
  31. * @description uni-data-pickerview
  32. * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-data-picker
  33. * @property {Array} localdata 本地数据,参考
  34. * @property {Boolean} step-searh = [true|false] 是否分布查询
  35. * @value true 启用分布查询,仅查询当前选中节点
  36. * @value false 关闭分布查询,一次查询出所有数据
  37. * @property {String|DBFieldString} self-field 分布查询当前字段名称
  38. * @property {String|DBFieldString} parent-field 分布查询父字段名称
  39. * @property {String|DBCollectionString} collection 表名
  40. * @property {String|DBFieldString} field 查询字段,多个字段用 `,` 分割
  41. * @property {String} orderby 排序字段及正序倒叙设置
  42. * @property {String|JQLString} where 查询条件
  43. */
  44. export default {
  45. name: 'UniDataPickerView',
  46. mixins: [dataPicker],
  47. props: {
  48. managedMode: {
  49. type: Boolean,
  50. default: false
  51. }
  52. },
  53. data() {
  54. return {}
  55. },
  56. created() {
  57. if (this.managedMode) {
  58. return
  59. }
  60. this.$nextTick(() => {
  61. this.load()
  62. })
  63. },
  64. methods: {
  65. onPropsChange() {
  66. this._treeData = []
  67. this.selectedIndex = 0
  68. this.load()
  69. },
  70. load() {
  71. if (this.isLocaldata) {
  72. this.loadData()
  73. } else if (this.value.length) {
  74. this.getTreePath((res) => {
  75. this.loadData()
  76. })
  77. }
  78. },
  79. handleSelect(index) {
  80. this.selectedIndex = index
  81. },
  82. handleNodeClick(item, i, j) {
  83. if (item.disable) {
  84. return
  85. }
  86. const node = this.dataList[i][j]
  87. const {
  88. value,
  89. text
  90. } = node
  91. if (i < this.selected.length - 1) {
  92. this.selected.splice(i, this.selected.length - i)
  93. this.selected.push(node)
  94. } else if (i === this.selected.length - 1) {
  95. this.selected[i] = node
  96. }
  97. if (node.isleaf) {
  98. this.onSelectedChange(node, node.isleaf)
  99. return
  100. }
  101. const {
  102. isleaf,
  103. hasNodes
  104. } = this._updateBindData()
  105. if (this.isLocaldata && (!hasNodes || isleaf)) {
  106. this.onSelectedChange(node, true)
  107. return
  108. }
  109. if (!isleaf && !hasNodes) {
  110. this._loadNodeData((data) => {
  111. if (!data.length) {
  112. node.isleaf = true
  113. } else {
  114. this._treeData.push(...data)
  115. this._updateBindData(node)
  116. }
  117. this.onSelectedChange(node, node.isleaf)
  118. }, this.nodeWhere)
  119. return
  120. }
  121. this.onSelectedChange(node, false)
  122. },
  123. updateData(data) {
  124. this._treeData = data.treeData
  125. this.selected = data.selected
  126. if (!this._treeData.length) {
  127. this.loadData()
  128. } else {
  129. //this.selected = data.selected
  130. this._updateBindData()
  131. }
  132. },
  133. onDataChange() {
  134. this.$emit('datachange')
  135. },
  136. onSelectedChange(node, isleaf) {
  137. if (isleaf) {
  138. this._dispatchEvent()
  139. } else if (node) {
  140. this.$emit('nodeclick', node)
  141. }
  142. },
  143. _dispatchEvent() {
  144. this.$emit('change', this.selected.slice(0))
  145. }
  146. }
  147. }
  148. </script>
  149. <style scoped>
  150. .uni-data-pickerview {
  151. flex: 1;
  152. display: flex;
  153. flex-direction: column;
  154. overflow: hidden;
  155. height: 100%;
  156. }
  157. .error-text {
  158. color: #DD524D;
  159. }
  160. .loading-cover {
  161. position: absolute;
  162. left: 0;
  163. top: 0;
  164. right: 0;
  165. bottom: 0;
  166. background-color: rgba(255, 255, 255, .5);
  167. display: flex;
  168. flex-direction: column;
  169. align-items: center;
  170. z-index: 1001;
  171. }
  172. .load-more {
  173. margin: auto;
  174. }
  175. .error-message {
  176. background-color: #fff;
  177. position: absolute;
  178. left: 0;
  179. top: 0;
  180. right: 0;
  181. bottom: 0;
  182. padding: 15px;
  183. opacity: .9;
  184. z-index: 102;
  185. }
  186. /* #ifdef APP-NVUE */
  187. .selected-area {
  188. width: 750rpx;
  189. }
  190. /* #endif */
  191. .selected-list {
  192. display: flex;
  193. flex-direction: row;
  194. flex-wrap: nowrap;
  195. padding: 0 5px;
  196. border-bottom: 1px solid #f8f8f8;
  197. }
  198. .selected-item {
  199. margin-left: 10px;
  200. margin-right: 10px;
  201. padding: 12px 0;
  202. white-space: nowrap;
  203. }
  204. .selected-item-active {
  205. border-bottom: 2px solid #007aff;
  206. }
  207. .selected-item-text {
  208. color: #007aff;
  209. }
  210. .tab-c {
  211. position: relative;
  212. flex: 1;
  213. display: flex;
  214. flex-direction: row;
  215. overflow: hidden;
  216. }
  217. .list {
  218. flex: 1;
  219. }
  220. .item {
  221. padding: 12px 15px;
  222. border-bottom: 1px solid #f0f0f0;
  223. display: flex;
  224. flex-direction: row;
  225. }
  226. .is-disabled {
  227. opacity: .5;
  228. }
  229. .item-text {
  230. flex: 1;
  231. color: #333333;
  232. }
  233. .check {
  234. margin-right: 5px;
  235. border: 2px solid #007aff;
  236. border-left: 0;
  237. border-top: 0;
  238. height: 12px;
  239. width: 6px;
  240. transform-origin: center;
  241. transition: all 0.3s;
  242. transform: rotate(45deg);
  243. }
  244. </style>