tui-cascade-selection.vue 14 KB

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