uni-data-pickerview.vue 7.5 KB

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