tui-cascade-selection.vue 13 KB

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