uni-data-pickerview.vue 6.8 KB

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