uni-data-pickerview.vue 8.1 KB

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