tui-cascade-selection.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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. defaultItemList(val){
  254. this.setDefaultData(val)
  255. }
  256. },
  257. created() {
  258. this.setDefaultData(this.defaultItemList)
  259. },
  260. data() {
  261. return {
  262. currentTab: 0,
  263. //tab栏scrollview滚动的位置
  264. scrollViewId: 'id__1',
  265. selectedArr: []
  266. };
  267. },
  268. methods: {
  269. setDefaultData(val){
  270. let defaultItemList = val || [];
  271. if (defaultItemList.length > 0) {
  272. defaultItemList.map(item => {
  273. item.scrollViewId = `id_${item.index}`;
  274. });
  275. this.selectedArr = defaultItemList;
  276. this.currentTab = defaultItemList.length - 1;
  277. this.$nextTick(() => {
  278. this.checkCor();
  279. });
  280. } else {
  281. this.initData(this.itemList, -1);
  282. }
  283. },
  284. initData(data, layer) {
  285. if (!data || data.length === 0) return;
  286. if (this.request) {
  287. //第一级数据
  288. this.subLevelData(data, layer);
  289. } else {
  290. let selectedValue = this.selectedValue || {};
  291. if (selectedValue.type) {
  292. this.setDefaultData(selectedValue);
  293. } else {
  294. this.subLevelData(this.getItemList(layer, -1), layer);
  295. }
  296. }
  297. },
  298. removeChildren(data) {
  299. let list = data.map(item => {
  300. delete item['children'];
  301. return item;
  302. });
  303. return list;
  304. },
  305. getItemList(layer, index) {
  306. let list = [];
  307. let arr = JSON.parse(JSON.stringify(this.itemList));
  308. if (layer == -1) {
  309. list = this.removeChildren(arr);
  310. } else {
  311. let value = this.selectedArr[0].index;
  312. value = value == -1 ? index : value;
  313. list = arr[value].children || [];
  314. if (layer > 0) {
  315. for (let i = 1; i < layer + 1; i++) {
  316. let val = layer === i ? index : this.selectedArr[i].index;
  317. list = list[val].children || [];
  318. if (list.length === 0) break;
  319. }
  320. }
  321. list = this.removeChildren(list);
  322. }
  323. return list;
  324. },
  325. //滚动切换
  326. switchTab: function(e) {
  327. this.currentTab = e.detail.current;
  328. this.checkCor();
  329. },
  330. //点击标题切换当
  331. swichNav: function(e) {
  332. let cur = e.currentTarget.dataset.current;
  333. if (this.currentTab != cur) {
  334. this.currentTab = cur;
  335. }
  336. },
  337. checkCor: function() {
  338. let item = this.selectedArr[this.currentTab];
  339. item.scrollViewId = 'id__1';
  340. this.$nextTick(() => {
  341. setTimeout(() => {
  342. let val = item.index < 2 ? 0 : Number(item.index - 2);
  343. item.scrollViewId = `id_${val}`;
  344. }, 2);
  345. });
  346. if (this.currentTab > 1) {
  347. this.scrollViewId = `id_${this.currentTab - 1}`;
  348. } else {
  349. this.scrollViewId = `id_0`;
  350. }
  351. },
  352. change(index, subIndex, subItem) {
  353. let item = this.selectedArr[index];
  354. if (item.index == subIndex) return;
  355. item.index = subIndex;
  356. item.text = subItem.text;
  357. item.value = subItem.value;
  358. item.subText = subItem.subText || '';
  359. item.src = subItem.src || '';
  360. this.$emit('change', {
  361. layer: index,
  362. subIndex: subIndex, //layer=> Array index
  363. ...subItem
  364. });
  365. if (!this.request) {
  366. let data = this.getItemList(index, subIndex);
  367. this.subLevelData(data, index);
  368. }
  369. },
  370. //新增子级数据时处理
  371. subLevelData(data, layer) {
  372. if (!data || data.length === 0) {
  373. if (layer == -1) return;
  374. //完成选择
  375. let result = JSON.parse(JSON.stringify(this.selectedArr));
  376. let lastItem = result[result.length - 1] || {};
  377. let text = '';
  378. result.map(item => {
  379. text += item.text;
  380. delete item['list'];
  381. //delete item['index'];
  382. delete item['scrollViewId'];
  383. return item;
  384. });
  385. this.$emit('complete', {
  386. result: result,
  387. value: lastItem.value,
  388. text: text,
  389. subText: lastItem.subText,
  390. src: lastItem.src
  391. });
  392. } else {
  393. //重置数据( >layer层级)
  394. let item = [
  395. {
  396. text: this.text,
  397. subText: '',
  398. value: '',
  399. src: '',
  400. index: -1,
  401. scrollViewId: 'id__1',
  402. list: data
  403. }
  404. ];
  405. if (layer == -1) {
  406. this.selectedArr = item;
  407. } else {
  408. let retainArr = this.selectedArr.slice(0, layer + 1);
  409. this.selectedArr = retainArr.concat(item);
  410. }
  411. this.$nextTick(() => {
  412. this.currentTab = this.selectedArr.length - 1;
  413. });
  414. }
  415. }
  416. }
  417. };
  418. </script>
  419. <style scoped>
  420. .tui-cascade-selection {
  421. width: 100%;
  422. box-sizing: border-box;
  423. }
  424. .tui-selection-header {
  425. width: 100%;
  426. display: flex;
  427. align-items: center;
  428. position: relative;
  429. box-sizing: border-box;
  430. }
  431. .tui-bottom-line {
  432. position: relative;
  433. }
  434. .tui-bottom-line::after {
  435. width: 100%;
  436. content: '';
  437. position: absolute;
  438. border-bottom: 1rpx solid #eaeef1;
  439. -webkit-transform: scaleY(0.5) translateZ(0);
  440. transform: scaleY(0.5) translateZ(0);
  441. transform-origin: 0 100%;
  442. bottom: 0;
  443. right: 0;
  444. left: 0;
  445. }
  446. .tui-btm-none::after {
  447. border-bottom: 0 !important;
  448. }
  449. .tui-header-item {
  450. max-width: 240rpx;
  451. padding: 15rpx 30rpx;
  452. box-sizing: border-box;
  453. flex-shrink: 0;
  454. overflow: hidden;
  455. white-space: nowrap;
  456. text-overflow: ellipsis;
  457. position: relative;
  458. }
  459. .tui-font-bold {
  460. font-weight: bold;
  461. }
  462. .tui-active-line {
  463. width: 60rpx;
  464. height: 6rpx;
  465. border-radius: 4rpx;
  466. position: absolute;
  467. bottom: 0;
  468. right: 0;
  469. left: 50%;
  470. transform: translateX(-50%);
  471. }
  472. .tui-selection-cell {
  473. width: 100%;
  474. box-sizing: border-box;
  475. display: flex;
  476. align-items: center;
  477. }
  478. .tui-icon-success {
  479. margin-right: 12rpx;
  480. }
  481. .tui-cell-img {
  482. margin-right: 12rpx;
  483. flex-shrink: 0;
  484. }
  485. .tui-cell-title {
  486. word-break: break-all;
  487. }
  488. .tui-flex-shrink {
  489. flex-shrink: 0;
  490. }
  491. .tui-font-bold {
  492. font-weight: bold;
  493. }
  494. .tui-cell-sub_title {
  495. margin-left: 20rpx;
  496. word-break: break-all;
  497. }
  498. .tui-first-item {
  499. width: 100%;
  500. }
  501. </style>