uni-data-pickerview.vue 7.8 KB

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