tui-cascade-selection.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. <template>
  2. <view class="tui-cascade-selection">
  3. <scroll-view
  4. scroll-x
  5. scroll-with-animation
  6. :scroll-into-view="scrollViewId"
  7. :style="{ backgroundColor: headerBgColor }"
  8. class="tui-bottom-line"
  9. :class="{ 'tui-btm-none': !headerLine }"
  10. >
  11. <view class="tui-selection-header" :style="{ height: tabsHeight, backgroundColor: backgroundColor }">
  12. <view
  13. class="tui-header-item"
  14. :class="{ 'tui-font-bold': index === currentTab && bold }"
  15. :style="{ color: index === currentTab ? activeColor : color, fontSize: size + 'rpx' }"
  16. :id="`id_${index}`"
  17. @tap.stop="swichNav"
  18. :data-current="index"
  19. v-for="(item, index) in selectedArr"
  20. :key="index"
  21. >
  22. {{ item.text }}
  23. <view class="tui-active-line" :style="{ backgroundColor: lineColor }" v-if="index === currentTab && showLine"></view>
  24. </view>
  25. </view>
  26. </scroll-view>
  27. <swiper class="tui-selection-list" :current="currentTab" duration="300" @change="switchTab" :style="{ height: height, backgroundColor: backgroundColor }">
  28. <swiper-item v-for="(item, index) in selectedArr" :key="index">
  29. <scroll-view scroll-y :scroll-into-view="item.scrollViewId" class="tui-selection-item" :style="{ height: height }">
  30. <view class="tui-first-item" :style="{ height: firstItemTop }"></view>
  31. <view
  32. class="tui-selection-cell"
  33. :style="{ padding: padding, backgroundColor: backgroundColor }"
  34. :id="`id_${subIndex}`"
  35. v-for="(subItem, subIndex) in item.list"
  36. :key="subIndex"
  37. @tap="change(index, subIndex, subItem)"
  38. >
  39. <icon type="success_no_circle" v-if="item.index === subIndex" :color="checkMarkColor" :size="checkMarkSize" class="tui-icon-success"></icon>
  40. <image :src="subItem.src" v-if="subItem.src" class="tui-cell-img" :style="{ width: imgWidth, height: imgHeight, borderRadius: radius }"></image>
  41. <view
  42. class="tui-cell-title"
  43. :class="{ 'tui-font-bold': item.index === subIndex && textBold, 'tui-flex-shrink': nowrap }"
  44. :style="{ color: item.index === subIndex ? textActiveColor : textColor, fontSize: textSize + 'rpx' }"
  45. >
  46. {{ subItem.text }}
  47. </view>
  48. <view class="tui-cell-sub_title" :style="{ color: subTextColor, fontSize: subTextSize + 'rpx' }" v-if="subItem.subText">{{ subItem.subText }}</view>
  49. </view>
  50. </scroll-view>
  51. </swiper-item>
  52. </swiper>
  53. </view>
  54. </template>
  55. <script>
  56. export default {
  57. name: 'tuiCascadeSelection',
  58. props: {
  59. /**
  60. * 如果下一级是请求返回,则为第一级数据,否则所有数据
  61. * 数据格式
  62. [{
  63. src: "",
  64. text: "",
  65. subText: "",
  66. value: 0,
  67. children:[{
  68. text: "",
  69. subText: "",
  70. value: 0,
  71. children:[]
  72. }]
  73. }]
  74. * */
  75. itemList: {
  76. type: Array,
  77. default: () => {
  78. return [];
  79. }
  80. },
  81. /*
  82. 初始化默认选中数据
  83. [{
  84. text: "",//选中text
  85. subText: '',//选中subText
  86. value: '',//选中value
  87. src: '', //选中src,没有则传空或不传
  88. index: 0, //选中数据在当前layer索引
  89. list: [{src: "", text: "", subText: "", value: 101}] //所有layer数据集合
  90. }];
  91. */
  92. defaultItemList: {
  93. type: Array,
  94. value: []
  95. },
  96. //是否显示header底部细线
  97. headerLine: {
  98. type: Boolean,
  99. default: true
  100. },
  101. //header背景颜色
  102. headerBgColor: {
  103. type: String,
  104. default: '#FFFFFF'
  105. },
  106. //顶部标签栏高度
  107. tabsHeight: {
  108. type: String,
  109. default: '88rpx'
  110. },
  111. //默认显示文字
  112. text: {
  113. type: String,
  114. default: '请选择'
  115. },
  116. //tabs 文字大小
  117. size: {
  118. type: Number,
  119. default: 28
  120. },
  121. //tabs 文字颜色
  122. color: {
  123. type: String,
  124. default: '#555'
  125. },
  126. //选中颜色
  127. activeColor: {
  128. type: String,
  129. default: '#5677fc'
  130. },
  131. //选中后文字加粗
  132. bold: {
  133. type: Boolean,
  134. default: true
  135. },
  136. //选中后是否显示底部线条
  137. showLine: {
  138. type: Boolean,
  139. default: true
  140. },
  141. //线条颜色
  142. lineColor: {
  143. type: String,
  144. default: '#5677fc'
  145. },
  146. //icon 大小
  147. checkMarkSize: {
  148. type: Number,
  149. default: 15
  150. },
  151. //icon 颜色
  152. checkMarkColor: {
  153. type: String,
  154. default: '#5677fc'
  155. },
  156. //item 图片宽度
  157. imgWidth: {
  158. type: String,
  159. default: '40rpx'
  160. },
  161. //item 图片高度
  162. imgHeight: {
  163. type: String,
  164. default: '40rpx'
  165. },
  166. //图片圆角
  167. radius: {
  168. type: String,
  169. default: '50%'
  170. },
  171. //item text颜色
  172. textColor: {
  173. type: String,
  174. default: '#333'
  175. },
  176. textActiveColor: {
  177. type: String,
  178. default: '#333'
  179. },
  180. //选中后字体是否加粗
  181. textBold: {
  182. type: Boolean,
  183. default: true
  184. },
  185. //item text字体大小
  186. textSize: {
  187. type: Number,
  188. default: 28
  189. },
  190. //text 是否不换行
  191. nowrap: {
  192. type: Boolean,
  193. default: false
  194. },
  195. //item subText颜色
  196. subTextColor: {
  197. type: String,
  198. default: '#999'
  199. },
  200. //item subText字体大小
  201. subTextSize: {
  202. type: Number,
  203. default: 24
  204. },
  205. // item padding
  206. padding: {
  207. type: String,
  208. default: '20rpx 30rpx'
  209. },
  210. //占位高度,第一条数据距离顶部距离
  211. firstItemTop: {
  212. type: String,
  213. default: '20rpx'
  214. },
  215. //swiper 高度
  216. height: {
  217. type: String,
  218. default: '300px'
  219. },
  220. //item swiper 内容部分背景颜色
  221. backgroundColor: {
  222. type: String,
  223. default: '#FFFFFF'
  224. },
  225. //子集数据是否请求返回(默认false,一次性返回所有数据)
  226. request: {
  227. type: Boolean,
  228. default: false
  229. },
  230. //子级数据(当有改变时,默认当前选中项新增子级数据,request=true时生效)
  231. receiveData: {
  232. type: Array,
  233. default: () => {
  234. return [];
  235. }
  236. },
  237. //改变值则重置数据
  238. reset: {
  239. type: [Number, String],
  240. default: 0
  241. }
  242. },
  243. watch: {
  244. itemList(val) {
  245. this.initData(val, -1);
  246. },
  247. receiveData(val) {
  248. this.subLevelData(val, this.currentTab);
  249. },
  250. reset() {
  251. this.initData(this.itemList, -1);
  252. }
  253. },
  254. created() {
  255. let defaultItemList = this.defaultItemList || [];
  256. if (defaultItemList.length > 0) {
  257. defaultItemList.map(item => {
  258. item.scrollViewId = `id_${item.index}`;
  259. });
  260. this.selectedArr = defaultItemList;
  261. this.currentTab = defaultItemList.length - 1;
  262. this.$nextTick(() => {
  263. this.checkCor();
  264. });
  265. } else {
  266. this.initData(this.itemList, -1);
  267. }
  268. },
  269. data() {
  270. return {
  271. currentTab: 0,
  272. //tab栏scrollview滚动的位置
  273. scrollViewId: 'id__1',
  274. selectedArr: []
  275. };
  276. },
  277. methods: {
  278. initData(data, layer) {
  279. if (!data || data.length === 0) return;
  280. if (this.request) {
  281. //第一级数据
  282. this.subLevelData(data, layer);
  283. } else {
  284. let selectedValue = this.selectedValue || {};
  285. if (selectedValue.type) {
  286. this.setDefaultData(selectedValue);
  287. } else {
  288. this.subLevelData(this.getItemList(layer, -1), layer);
  289. }
  290. }
  291. },
  292. removeChildren(data) {
  293. let list = data.map(item => {
  294. delete item['children'];
  295. return item;
  296. });
  297. return list;
  298. },
  299. getItemList(layer, index) {
  300. let list = [];
  301. let arr = JSON.parse(JSON.stringify(this.itemList));
  302. if (layer == -1) {
  303. list = this.removeChildren(arr);
  304. } else {
  305. let value = this.selectedArr[0].index;
  306. value = value == -1 ? index : value;
  307. list = arr[value].children || [];
  308. if (layer > 0) {
  309. for (let i = 1; i < layer + 1; i++) {
  310. let val = layer === i ? index : this.selectedArr[i].index;
  311. list = list[val].children || [];
  312. if (list.length === 0) break;
  313. }
  314. }
  315. list = this.removeChildren(list);
  316. }
  317. return list;
  318. },
  319. //滚动切换
  320. switchTab: function(e) {
  321. this.currentTab = e.detail.current;
  322. this.checkCor();
  323. },
  324. //点击标题切换当
  325. swichNav: function(e) {
  326. let cur = e.currentTarget.dataset.current;
  327. if (this.currentTab != cur) {
  328. this.currentTab = cur;
  329. }
  330. },
  331. checkCor: function() {
  332. let item = this.selectedArr[this.currentTab];
  333. item.scrollViewId = 'id__1';
  334. this.$nextTick(() => {
  335. setTimeout(() => {
  336. let val = item.index < 2 ? 0 : Number(item.index - 2);
  337. item.scrollViewId = `id_${val}`;
  338. }, 2);
  339. });
  340. if (this.currentTab > 1) {
  341. this.scrollViewId = `id_${this.currentTab - 1}`;
  342. } else {
  343. this.scrollViewId = `id_0`;
  344. }
  345. },
  346. change(index, subIndex, subItem) {
  347. let item = this.selectedArr[index];
  348. if (item.index == subIndex) return;
  349. item.index = subIndex;
  350. item.text = subItem.text;
  351. item.value = subItem.value;
  352. item.subText = subItem.subText || '';
  353. item.src = subItem.src || '';
  354. this.$emit('change', {
  355. layer: index,
  356. subIndex: subIndex, //layer=> Array index
  357. ...subItem
  358. });
  359. if (!this.request) {
  360. let data = this.getItemList(index, subIndex);
  361. this.subLevelData(data, index);
  362. }
  363. },
  364. //新增子级数据时处理
  365. subLevelData(data, layer) {
  366. if (!data || data.length === 0) {
  367. if (layer == -1) return;
  368. //完成选择
  369. let result = JSON.parse(JSON.stringify(this.selectedArr));
  370. let lastItem = result[result.length - 1] || {};
  371. let text = '';
  372. result.map(item => {
  373. text += item.text;
  374. delete item['list'];
  375. //delete item['index'];
  376. delete item['scrollViewId'];
  377. return item;
  378. });
  379. this.$emit('complete', {
  380. result: result,
  381. value: lastItem.value,
  382. text: text,
  383. subText: lastItem.subText,
  384. src: lastItem.src
  385. });
  386. } else {
  387. //重置数据( >layer层级)
  388. let item = [
  389. {
  390. text: this.text,
  391. subText: '',
  392. value: '',
  393. src: '',
  394. index: -1,
  395. scrollViewId: 'id__1',
  396. list: data
  397. }
  398. ];
  399. if (layer == -1) {
  400. this.selectedArr = item;
  401. } else {
  402. let retainArr = this.selectedArr.slice(0, layer + 1);
  403. this.selectedArr = retainArr.concat(item);
  404. }
  405. this.$nextTick(() => {
  406. this.currentTab = this.selectedArr.length - 1;
  407. });
  408. }
  409. }
  410. }
  411. };
  412. </script>
  413. <style scoped>
  414. .tui-cascade-selection {
  415. width: 100%;
  416. box-sizing: border-box;
  417. }
  418. .tui-selection-header {
  419. width: 100%;
  420. display: flex;
  421. align-items: center;
  422. position: relative;
  423. box-sizing: border-box;
  424. }
  425. .tui-bottom-line {
  426. position: relative;
  427. }
  428. .tui-bottom-line::after {
  429. width: 100%;
  430. content: '';
  431. position: absolute;
  432. border-bottom: 1rpx solid #eaeef1;
  433. -webkit-transform: scaleY(0.5) translateZ(0);
  434. transform: scaleY(0.5) translateZ(0);
  435. transform-origin: 0 100%;
  436. bottom: 0;
  437. right: 0;
  438. left: 0;
  439. }
  440. .tui-btm-none::after {
  441. border-bottom: 0 !important;
  442. }
  443. .tui-header-item {
  444. max-width: 240rpx;
  445. padding: 15rpx 30rpx;
  446. box-sizing: border-box;
  447. flex-shrink: 0;
  448. overflow: hidden;
  449. white-space: nowrap;
  450. text-overflow: ellipsis;
  451. position: relative;
  452. }
  453. .tui-font-bold {
  454. font-weight: bold;
  455. }
  456. .tui-active-line {
  457. width: 60rpx;
  458. height: 6rpx;
  459. border-radius: 4rpx;
  460. position: absolute;
  461. bottom: 0;
  462. right: 0;
  463. left: 50%;
  464. transform: translateX(-50%);
  465. }
  466. .tui-selection-cell {
  467. width: 100%;
  468. box-sizing: border-box;
  469. display: flex;
  470. align-items: center;
  471. }
  472. .tui-icon-success {
  473. margin-right: 12rpx;
  474. }
  475. .tui-cell-img {
  476. margin-right: 12rpx;
  477. flex-shrink: 0;
  478. }
  479. .tui-cell-title {
  480. word-break: break-all;
  481. }
  482. .tui-flex-shrink {
  483. flex-shrink: 0;
  484. }
  485. .tui-font-bold {
  486. font-weight: bold;
  487. }
  488. .tui-cell-sub_title {
  489. margin-left: 20rpx;
  490. word-break: break-all;
  491. }
  492. .tui-first-item {
  493. width: 100%;
  494. }
  495. </style>