uni-data-pickerview.vue 6.9 KB

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