u-cascader.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. <template>
  2. <up-popup :show="popupShow" mode="bottom" :popup="false"
  3. :mask="true" :closeable="closeable" :safe-area-inset-bottom="true"
  4. close-icon-color="#ffffff" :z-index="uZIndex"
  5. :maskCloseAble="maskCloseAble" @close="close">
  6. <view class="up-p-t-30 up-p-l-20 up-m-b-10" v-if="headerDirection =='column'">
  7. <up-steps v-if="popupShow" dot direction="column" v-model:current="tabsIndex">
  8. <up-steps-item v-for="(item, index) in genTabsList"
  9. @click="tabsIndex = index" :title="item.name"></up-steps-item>
  10. </up-steps>
  11. </view>
  12. <view class="up-p-t-20 up-m-b-10" v-else>
  13. <up-tabs v-if="popupShow" :list="genTabsList"
  14. :scrollable="true" v-model:current="tabsIndex" @change="tabsChange" ref="tabs"></up-tabs>
  15. </view>
  16. <view class="area-box">
  17. <view class="u-flex" :class="{ 'change':isChange }"
  18. :style="{transform: optionsCols == 2 && isChange ? 'translateX(-33.3333333%)' : ''}">
  19. <template v-for="(levelData, levelIndex) in levelList" :key="levelIndex">
  20. <view v-if="optionsCols == 2 || levelIndex == tabsIndex" class="area-item"
  21. :style="{ width: optionsCols == 2 ? '33.33333%' : '750rpx'}">
  22. <view class="u-padding-10 u-bg-gray" style="height: 100%;">
  23. <scroll-view :scroll-y="true" style="height: 100%">
  24. <up-cell-group v-if="levelIndex === 0 || selectedValueIndexs[levelIndex - 1] !== undefined">
  25. <up-cell v-for="(item,index) in levelData"
  26. :title="item[labelKey]" :arrow="false"
  27. :index="index" :key="index"
  28. @click="levelChange(levelIndex, index)">
  29. <template v-slot:right-icon>
  30. <up-icon v-if="selectedValueIndexs[levelIndex] === index"
  31. size="17" name="checkbox-mark"></up-icon>
  32. </template>
  33. </up-cell>
  34. </up-cell-group>
  35. </scroll-view>
  36. </view>
  37. </view>
  38. </template>
  39. </view>
  40. </view>
  41. <!-- 添加按钮区域 -->
  42. <view class="u-cascader-action up-flex up-flex-between">
  43. <view class="u-padding-20 up-flex-fill">
  44. <up-button @click="handleCancel" type="default">{{ t("up.common.cancel") }}</up-button>
  45. </view>
  46. <view class="u-padding-20 up-flex-fill">
  47. <up-button @click="handleConfirm" type="primary">{{ t("up.common.confirm") }}</up-button>
  48. </view>
  49. </view>
  50. </up-popup>
  51. </template>
  52. <script>
  53. /**
  54. * u-cascader 通用无限级联选择器
  55. * @property {String Number} z-index 弹出时的z-index值(默认1075)
  56. * @property {Boolean} mask-close-able 是否允许通过点击遮罩关闭Picker(默认true)
  57. * @property {Array} data 级联数据
  58. * @property {Array} default-value 默认选中的值
  59. * @property {String} valueKey 指定选项的值为选项对象中的哪个属性值
  60. * @property {String} labelKey 指定选项标签为选项对象中的哪个属性值
  61. * @property {String} childrenKey 指定选项的子选项为选项对象中的哪个属性值
  62. * @property {Boolean} autoClose 是否在选择最后一级时自动关闭并触发confirm(默认false)
  63. * @property {Boolean} closeable 是否显示关闭图标(默认true)
  64. */
  65. import { t } from '../../libs/i18n'
  66. export default {
  67. name: 'up-cascader',
  68. props: {
  69. // 通过双向绑定控制组件的弹出与收起
  70. show: {
  71. type: Boolean,
  72. default: false
  73. },
  74. // 级联数据
  75. data: {
  76. type: Array,
  77. default() {
  78. return [];
  79. }
  80. },
  81. // 默认选中的值
  82. modelValue: {
  83. type: Array,
  84. default() {
  85. return [];
  86. }
  87. },
  88. // 指定选项的值为选项对象中的哪个属性值
  89. valueKey: {
  90. type: String,
  91. default: 'value'
  92. },
  93. // 指定选项标签为选项对象中的哪个属性值
  94. labelKey: {
  95. type: String,
  96. default: 'label'
  97. },
  98. // 指定选项的子选项为选项对象中的哪个属性值
  99. childrenKey: {
  100. type: String,
  101. default: 'children'
  102. },
  103. // 是否允许通过点击遮罩关闭Picker
  104. maskCloseAble: {
  105. type: Boolean,
  106. default: true
  107. },
  108. // 弹出的z-index值
  109. zIndex: {
  110. type: [String, Number],
  111. default: 0
  112. },
  113. // 是否在选择最后一级时自动关闭并触发confirm
  114. autoClose: {
  115. type: Boolean,
  116. default: false
  117. },
  118. // 选中项目的展示方向direction垂直方向适合文字长度过长
  119. headerDirection: {
  120. type: String,
  121. default: 'row'
  122. },
  123. // 选项区域列数,支持1列和2列,默认为2列
  124. optionsCols: {
  125. type: [Number],
  126. default: 2
  127. },
  128. // 是否显示关闭图标
  129. closeable: {
  130. type: Boolean,
  131. default: true
  132. }
  133. },
  134. data() {
  135. return {
  136. // 存储每一级的数据
  137. levelList: [],
  138. // 存储每一级选中的索引
  139. selectedValueIndexs: [],
  140. tabsIndex: 0,
  141. popupShow: false,
  142. // 新增confirmValues用于存储确认的值
  143. confirmValues: []
  144. }
  145. },
  146. watch: {
  147. data: {
  148. handler() {
  149. this.initLevelList();
  150. this.setDefaultValue();
  151. },
  152. immediate: true
  153. },
  154. show() {
  155. this.popupShow = this.show;
  156. },
  157. modelValue: {
  158. handler() {
  159. // 初始化选中值
  160. this.setDefaultValue();
  161. },
  162. immediate: true
  163. }
  164. },
  165. computed: {
  166. isChange() {
  167. return this.tabsIndex > 1;
  168. },
  169. genTabsList() {
  170. let tabsList = [{
  171. name: "请选择"
  172. }];
  173. // 根据选中的值动态生成tabs
  174. for (let i = 0; i < this.selectedValueIndexs.length; i++) {
  175. if (this.selectedValueIndexs[i] !== undefined && this.levelList[i]) {
  176. const selectedItem = this.levelList[i][this.selectedValueIndexs[i]];
  177. if (selectedItem) {
  178. tabsList[i] = {
  179. name: selectedItem[this.labelKey]
  180. };
  181. // 如果还有下一级,则添加"请选择"
  182. if (i === this.selectedValueIndexs.length - 1 &&
  183. selectedItem[this.childrenKey] &&
  184. selectedItem[this.childrenKey].length > 0) {
  185. tabsList.push({
  186. name: "请选择"
  187. });
  188. }
  189. }
  190. }
  191. }
  192. return tabsList;
  193. },
  194. uZIndex() {
  195. // 如果用户有传递z-index值,优先使用
  196. return this.zIndex ? this.zIndex : this.$u.zIndex.popup;
  197. }
  198. },
  199. // 新增confirm事件
  200. emits: ['update:modelValue', 'update:show', 'change', 'confirm', 'cancel'],
  201. methods: {
  202. t,
  203. initLevelList() {
  204. // 初始化第一级数据
  205. if (this.data && this.data.length > 0) {
  206. this.levelList = [this.data];
  207. this.selectedValueIndexs = [];
  208. }
  209. },
  210. setDefaultValue() {
  211. // 检查data是否为空
  212. if (!this.data || this.data.length == 0) return;
  213. // 检查modelValue是否为空
  214. if (!this.modelValue || this.modelValue.length == 0) return;
  215. // 根据默认值设置选中项
  216. // 根据modelValue获取indexs给selectedValueIndexs
  217. this.selectedValueIndexs = [];
  218. this.levelList = []; // 设置层级数据为空
  219. let currentLevelData = this.data;
  220. for (let i = 0; i < this.modelValue.length; i++) {
  221. const value = this.modelValue[i];
  222. const index = currentLevelData.findIndex(item => item[this.valueKey] === value);
  223. this.levelList[i] = currentLevelData; // 设置每一层级的数据
  224. if (index !== -1) {
  225. this.selectedValueIndexs.push(index);
  226. // 更新下一级的数据
  227. if (currentLevelData[index][this.childrenKey]) {
  228. currentLevelData = currentLevelData[index][this.childrenKey];
  229. } else {
  230. // 如果没有子级数据,则停止处理
  231. break;
  232. }
  233. } else {
  234. // 如果找不到匹配项,则停止处理
  235. break;
  236. }
  237. }
  238. },
  239. close() {
  240. this.$emit('cancel');
  241. this.$emit('update:show', false);
  242. },
  243. tabsChange(item) {
  244. },
  245. levelChange(levelIndex, index) {
  246. // 设置当前级的选中值
  247. this.$set(this.selectedValueIndexs, levelIndex, index);
  248. // 清除后续级别的选中值
  249. this.selectedValueIndexs.splice(levelIndex + 1);
  250. this.tabsIndex = Math.min(this.tabsIndex, levelIndex);
  251. // 清除后续级别的列表
  252. this.levelList.splice(levelIndex + 1);
  253. // 获取当前选中项
  254. const currentItem = this.levelList[levelIndex][index];
  255. // 如果有子级数据,则初始化下一级
  256. if (currentItem && currentItem[this.childrenKey] && currentItem[this.childrenKey].length > 0) {
  257. // 确保levelList数组足够长
  258. if (this.levelList.length <= levelIndex + 1) {
  259. this.levelList.push(currentItem[this.childrenKey]);
  260. } else {
  261. this.$set(this.levelList, levelIndex + 1, currentItem[this.childrenKey]);
  262. }
  263. // 切换到下一级tab
  264. this.tabsIndex = levelIndex + 1;
  265. } else {
  266. // 没有子级数据,说明是最后一级
  267. if (this.autoClose) {
  268. // 如果启用自动关闭,则触发change事件并关闭
  269. this.emitChange();
  270. } else {
  271. // 否则只触发change事件,不关闭
  272. this.emitChange(false);
  273. }
  274. }
  275. },
  276. // 修改emitChange方法,增加closePopup参数
  277. emitChange(closePopup = true) {
  278. // 构造选中结果
  279. const result = [];
  280. for (let i = 0; i < this.selectedValueIndexs.length; i++) {
  281. if (this.selectedValueIndexs[i] !== undefined && this.levelList[i]) {
  282. result.push(this.levelList[i][this.selectedValueIndexs[i]][this.valueKey]);
  283. }
  284. }
  285. // 更新confirmValues
  286. this.confirmValues = [...result];
  287. // 触发change事件,返回value数组
  288. this.$emit('change', this.confirmValues);
  289. // 根据参数决定是否关闭弹窗
  290. if (closePopup) {
  291. this.close();
  292. }
  293. },
  294. handleCancel() {
  295. this.close();
  296. },
  297. handleConfirm() {
  298. // 确认时触发confirm事件
  299. this.$emit('update:modelValue', this.confirmValues);
  300. this.$emit('confirm', this.confirmValues);
  301. this.close();
  302. }
  303. }
  304. }
  305. </script>
  306. <style lang="scss">
  307. .area-box {
  308. width: 100%;
  309. overflow: hidden;
  310. height: 800rpx;
  311. >view {
  312. width: 150%;
  313. transition: transform 0.3s ease-in-out 0s;
  314. transform: translateX(0);
  315. &.change {
  316. // transform: translateX(-33.3333333%);
  317. }
  318. }
  319. .area-item {
  320. // width: 750rpx;
  321. height: 800rpx;
  322. }
  323. }
  324. // 添加按钮区域样式
  325. .u-cascader-action {
  326. border-top: 1px solid #eee;
  327. }
  328. </style>