uni-data-pickerview.vue 7.4 KB

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