uni-data-pickerview.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. * DataPickerview
  31. * @description uni-data-pickerview
  32. * @tutorial https://ext.dcloud.net.cn/plugin?id=3796
  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._isTreeView()) {
  106. this.onSelectedChange(node, true)
  107. return
  108. }
  109. if (this.isLocaldata && (!hasNodes || isleaf)) {
  110. this.onSelectedChange(node, true)
  111. return
  112. }
  113. if (!isleaf && !hasNodes) {
  114. this._loadNodeData((data) => {
  115. if (!data.length) {
  116. node.isleaf = true
  117. } else {
  118. this._treeData.push(...data)
  119. this._updateBindData(node)
  120. }
  121. this.onSelectedChange(node, node.isleaf)
  122. }, this._nodeWhere())
  123. return
  124. }
  125. this.onSelectedChange(node, false)
  126. },
  127. updateData(data) {
  128. this._treeData = data.treeData
  129. this.selected = data.selected
  130. if (!this._treeData.length) {
  131. this.loadData()
  132. } else {
  133. //this.selected = data.selected
  134. this._updateBindData()
  135. }
  136. },
  137. onDataChange() {
  138. this.$emit('datachange')
  139. },
  140. onSelectedChange(node, isleaf) {
  141. if (isleaf) {
  142. this._dispatchEvent()
  143. }
  144. if (node) {
  145. this.$emit('nodeclick', node)
  146. }
  147. },
  148. _dispatchEvent() {
  149. this.$emit('change', this.selected.slice(0))
  150. }
  151. }
  152. }
  153. </script>
  154. <style scoped>
  155. .uni-data-pickerview {
  156. flex: 1;
  157. /* #ifndef APP-NVUE */
  158. display: flex;
  159. /* #endif */
  160. flex-direction: column;
  161. overflow: hidden;
  162. height: 100%;
  163. }
  164. .error-text {
  165. color: #DD524D;
  166. }
  167. .loading-cover {
  168. position: absolute;
  169. left: 0;
  170. top: 0;
  171. right: 0;
  172. bottom: 0;
  173. background-color: rgba(255, 255, 255, .5);
  174. /* #ifndef APP-NVUE */
  175. display: flex;
  176. /* #endif */
  177. flex-direction: column;
  178. align-items: center;
  179. z-index: 1001;
  180. }
  181. .load-more {
  182. /* #ifndef APP-NVUE */
  183. margin: auto;
  184. /* #endif */
  185. }
  186. .error-message {
  187. background-color: #fff;
  188. position: absolute;
  189. left: 0;
  190. top: 0;
  191. right: 0;
  192. bottom: 0;
  193. padding: 15px;
  194. opacity: .9;
  195. z-index: 102;
  196. }
  197. /* #ifdef APP-NVUE */
  198. .selected-area {
  199. width: 750rpx;
  200. }
  201. /* #endif */
  202. .selected-list {
  203. /* #ifndef APP-NVUE */
  204. display: flex;
  205. /* #endif */
  206. flex-direction: row;
  207. flex-wrap: nowrap;
  208. padding: 0 5px;
  209. border-bottom: 1px solid #f8f8f8;
  210. }
  211. .selected-item {
  212. margin-left: 10px;
  213. margin-right: 10px;
  214. padding: 12px 0;
  215. /* #ifndef APP-NVUE */
  216. white-space: nowrap;
  217. /* #endif */
  218. }
  219. .selected-item-active {
  220. border-bottom: 2px solid #007aff;
  221. }
  222. .selected-item-text {
  223. color: #007aff;
  224. }
  225. .tab-c {
  226. position: relative;
  227. flex: 1;
  228. /* #ifndef APP-NVUE */
  229. display: flex;
  230. /* #endif */
  231. flex-direction: row;
  232. overflow: hidden;
  233. }
  234. .list {
  235. flex: 1;
  236. }
  237. .item {
  238. padding: 12px 15px;
  239. border-bottom: 1px solid #f0f0f0;
  240. /* #ifndef APP-NVUE */
  241. display: flex;
  242. /* #endif */
  243. flex-direction: row;
  244. }
  245. .is-disabled {
  246. opacity: .5;
  247. }
  248. .item-text {
  249. flex: 1;
  250. color: #333333;
  251. }
  252. .check {
  253. margin-right: 5px;
  254. border: 2px solid #007aff;
  255. border-left: 0;
  256. border-top: 0;
  257. height: 12px;
  258. width: 6px;
  259. transform-origin: center;
  260. /* #ifndef APP-NVUE */
  261. transition: all 0.3s;
  262. /* #endif */
  263. transform: rotate(45deg);
  264. }
  265. </style>