uni-data-pickerview.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. <template v-for="(item,index) in selected">
  6. <view class="selected-item"
  7. :class="{'selected-item-active':index==selectedIndex, 'selected-item-text-overflow': ellipsis}"
  8. v-if="item.text" @click="handleSelect(index)">
  9. <text class="">{{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-text-overflow">{{item[map.text]}}8</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','surechange'],
  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. data() {
  65. return {}
  66. },
  67. created() {
  68. if (this.managedMode) {
  69. return
  70. }
  71. this.$nextTick(() => {
  72. this.load()
  73. })
  74. },
  75. methods: {
  76. onPropsChange() {
  77. this._treeData = []
  78. this.selectedIndex = 0
  79. this.load()
  80. },
  81. load() {
  82. if (this.isLocaldata) {
  83. this.loadData()
  84. } else if (this.dataValue.length) {
  85. this.getTreePath((res) => {
  86. this.loadData()
  87. })
  88. }
  89. },
  90. handleSelect(index) {
  91. this.selectedIndex = index
  92. },
  93. handleNodeClick(item, i, j) {
  94. if (item.disable) {
  95. return
  96. }
  97. const node = this.dataList[i][j]
  98. const text = node[this.map.text]
  99. const value = node[this.map.value]
  100. if (i < this.selected.length - 1) {
  101. this.selected.splice(i, this.selected.length - i)
  102. this.selected.push({
  103. text,
  104. value
  105. })
  106. } else if (i === this.selected.length - 1) {
  107. this.selected.splice(i, 1, {
  108. text,
  109. value
  110. })
  111. }
  112. if (node.isleaf) {
  113. this.onSelectedChange(node, node.isleaf)
  114. return
  115. }
  116. const {
  117. isleaf,
  118. hasNodes
  119. } = this._updateBindData()
  120. if (!this._isTreeView() && !hasNodes) {
  121. this.onSelectedChange(node, true)
  122. return
  123. }
  124. if (this.isLocaldata && (!hasNodes || isleaf)) {
  125. this.onSelectedChange(node, true)
  126. return
  127. }
  128. if (!isleaf && !hasNodes) {
  129. this._loadNodeData((data) => {
  130. if (!data.length) {
  131. node.isleaf = true
  132. } else {
  133. this._treeData.push(...data)
  134. this._updateBindData(node)
  135. }
  136. this.onSelectedChange(node, node.isleaf)
  137. }, this._nodeWhere())
  138. return
  139. }
  140. this.onSelectedChange(node, false)
  141. },
  142. updateData(data) {
  143. this._treeData = data.treeData
  144. this.selected = data.selected
  145. if (!this._treeData.length) {
  146. this.loadData()
  147. } else {
  148. //this.selected = data.selected
  149. this._updateBindData()
  150. }
  151. },
  152. onDataChange() {
  153. this.$emit('datachange')
  154. },
  155. onSelectedChange(node, isleaf) {
  156. if (isleaf) {
  157. this._dispatchEvent()
  158. }
  159. if (node) {
  160. this.$emit('nodeclick', node);
  161. this.$emit('surechange', this.selected.slice(0))
  162. }
  163. },
  164. _dispatchEvent() {
  165. this.$emit('change', this.selected.slice(0))
  166. }
  167. }
  168. }
  169. </script>
  170. <style lang="scss">
  171. $uni-primary: #007aff !default;
  172. .uni-data-pickerview {
  173. flex: 1;
  174. /* #ifndef APP-NVUE */
  175. display: flex;
  176. /* #endif */
  177. flex-direction: column;
  178. overflow: hidden;
  179. height: 100%;
  180. }
  181. .error-text {
  182. color: #DD524D;
  183. }
  184. .loading-cover {
  185. position: absolute;
  186. left: 0;
  187. top: 0;
  188. right: 0;
  189. bottom: 0;
  190. background-color: rgba(255, 255, 255, .5);
  191. /* #ifndef APP-NVUE */
  192. display: flex;
  193. /* #endif */
  194. flex-direction: column;
  195. align-items: center;
  196. z-index: 1001;
  197. }
  198. .load-more {
  199. /* #ifndef APP-NVUE */
  200. margin: auto;
  201. /* #endif */
  202. }
  203. .error-message {
  204. background-color: #fff;
  205. position: absolute;
  206. left: 0;
  207. top: 0;
  208. right: 0;
  209. bottom: 0;
  210. padding: 15px;
  211. opacity: .9;
  212. z-index: 102;
  213. }
  214. /* #ifdef APP-NVUE */
  215. .selected-area {
  216. width: 750rpx;
  217. }
  218. /* #endif */
  219. .selected-list {
  220. /* #ifndef APP-NVUE */
  221. display: flex;
  222. /* #endif */
  223. flex-direction: row;
  224. flex-wrap: nowrap;
  225. padding: 0 5px;
  226. border-bottom: 1px solid #f8f8f8;
  227. }
  228. .selected-item {
  229. margin-left: 10px;
  230. margin-right: 10px;
  231. padding: 12px 0;
  232. text-align: center;
  233. /* #ifndef APP-NVUE */
  234. white-space: nowrap;
  235. /* #endif */
  236. }
  237. .selected-item-text-overflow {
  238. width: 168px;
  239. /* fix nvue */
  240. overflow: hidden;
  241. /* #ifndef APP-NVUE */
  242. width: 6em;
  243. white-space: nowrap;
  244. text-overflow: ellipsis;
  245. -o-text-overflow: ellipsis;
  246. /* #endif */
  247. }
  248. .selected-item-active {
  249. border-bottom: 2px solid $uni-primary;
  250. }
  251. .selected-item-text {
  252. color: $uni-primary;
  253. }
  254. .tab-c {
  255. position: relative;
  256. flex: 1;
  257. /* #ifndef APP-NVUE */
  258. display: flex;
  259. /* #endif */
  260. flex-direction: row;
  261. overflow: hidden;
  262. }
  263. .list {
  264. flex: 1;
  265. }
  266. .item {
  267. padding: 12px 15px;
  268. /* border-bottom: 1px solid #f0f0f0; */
  269. /* #ifndef APP-NVUE */
  270. display: flex;
  271. /* #endif */
  272. flex-direction: row;
  273. justify-content: space-between;
  274. }
  275. .is-disabled {
  276. opacity: .5;
  277. }
  278. .item-text {
  279. /* flex: 1; */
  280. color: #333333;
  281. }
  282. .item-text-overflow {
  283. width: 280px;
  284. /* fix nvue */
  285. overflow: hidden;
  286. /* #ifndef APP-NVUE */
  287. width: 20em;
  288. white-space: nowrap;
  289. text-overflow: ellipsis;
  290. -o-text-overflow: ellipsis;
  291. /* #endif */
  292. }
  293. .check {
  294. margin-right: 5px;
  295. border: 2px solid $uni-primary;
  296. border-left: 0;
  297. border-top: 0;
  298. height: 12px;
  299. width: 6px;
  300. transform-origin: center;
  301. /* #ifndef APP-NVUE */
  302. transition: all 0.3s;
  303. /* #endif */
  304. transform: rotate(45deg);
  305. }
  306. </style>