uni-data-pickerview.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. }
  140. if (node) {
  141. this.$emit('nodeclick', node)
  142. }
  143. },
  144. _dispatchEvent() {
  145. this.$emit('change', this.selected.slice(0))
  146. }
  147. }
  148. }
  149. </script>
  150. <style scoped>
  151. .uni-data-pickerview {
  152. flex: 1;
  153. /* #ifndef APP-NVUE */
  154. display: flex;
  155. /* #endif */
  156. flex-direction: column;
  157. overflow: hidden;
  158. height: 100%;
  159. }
  160. .error-text {
  161. color: #DD524D;
  162. }
  163. .loading-cover {
  164. position: absolute;
  165. left: 0;
  166. top: 0;
  167. right: 0;
  168. bottom: 0;
  169. background-color: rgba(255, 255, 255, .5);
  170. /* #ifndef APP-NVUE */
  171. display: flex;
  172. /* #endif */
  173. flex-direction: column;
  174. align-items: center;
  175. z-index: 1001;
  176. }
  177. .load-more {
  178. /* #ifndef APP-NVUE */
  179. margin: auto;
  180. /* #endif */
  181. }
  182. .error-message {
  183. background-color: #fff;
  184. position: absolute;
  185. left: 0;
  186. top: 0;
  187. right: 0;
  188. bottom: 0;
  189. padding: 15px;
  190. opacity: .9;
  191. z-index: 102;
  192. }
  193. /* #ifdef APP-NVUE */
  194. .selected-area {
  195. width: 750rpx;
  196. }
  197. /* #endif */
  198. .selected-list {
  199. /* #ifndef APP-NVUE */
  200. display: flex;
  201. /* #endif */
  202. flex-direction: row;
  203. flex-wrap: nowrap;
  204. padding: 0 5px;
  205. border-bottom: 1px solid #f8f8f8;
  206. }
  207. .selected-item {
  208. margin-left: 10px;
  209. margin-right: 10px;
  210. padding: 12px 0;
  211. /* #ifndef APP-NVUE */
  212. white-space: nowrap;
  213. /* #endif */
  214. }
  215. .selected-item-active {
  216. border-bottom: 2px solid #007aff;
  217. }
  218. .selected-item-text {
  219. color: #007aff;
  220. }
  221. .tab-c {
  222. position: relative;
  223. flex: 1;
  224. /* #ifndef APP-NVUE */
  225. display: flex;
  226. /* #endif */
  227. flex-direction: row;
  228. overflow: hidden;
  229. }
  230. .list {
  231. flex: 1;
  232. }
  233. .item {
  234. padding: 12px 15px;
  235. border-bottom: 1px solid #f0f0f0;
  236. /* #ifndef APP-NVUE */
  237. display: flex;
  238. /* #endif */
  239. flex-direction: row;
  240. }
  241. .is-disabled {
  242. opacity: .5;
  243. }
  244. .item-text {
  245. flex: 1;
  246. color: #333333;
  247. }
  248. .check {
  249. margin-right: 5px;
  250. border: 2px solid #007aff;
  251. border-left: 0;
  252. border-top: 0;
  253. height: 12px;
  254. width: 6px;
  255. transform-origin: center;
  256. /* #ifndef APP-NVUE */
  257. transition: all 0.3s;
  258. /* #endif */
  259. transform: rotate(45deg);
  260. }
  261. </style>