当前位置:   article > 正文

前端实现三级分类搜索_商城点击分类搜索前端的实现

商城点击分类搜索前端的实现

 后端返回来的是一个树形图,搜索时匹配第三级的title并返回与他相关的上一级数

  1. let keyword="其他"
  2. let data =[
  3. {
  4. "id": "1664902947899924482",
  5. "parentId": "0",
  6. "children": [{
  7. "id": "1669527816008568834",
  8. "parentId": "1664902947899924482",
  9. "hasChildren": false,
  10. "title": "手机",
  11. "key": "2",
  12. }],
  13. "hasChildren": true,
  14. "title": "手机售后",
  15. "key": "1",""
  16. },
  17. {
  18. "id": "1673252728467623938",
  19. "parentId": "0",
  20. "hasChildren": false,
  21. "title": "测试",
  22. "key": "1",
  23. },
  24. {
  25. "id": "1664214513480867841",
  26. "parentId": "0",
  27. "children": [{
  28. "id": "1664216675430670337",
  29. "parentId": "1664214513480867841",
  30. "children": [{
  31. "id": "1665018889980043266",
  32. "parentId": "1664216675430670337",
  33. "hasChildren": false,
  34. "title": "小米",
  35. "key": "3",
  36. }, {
  37. "id": "1665018856224284674",
  38. "parentId": "1664216675430670337",
  39. "hasChildren": false,
  40. "title": "美的",
  41. "key": "3",
  42. }],
  43. "hasChildren": true,
  44. "title": "空调",
  45. "key": "2",
  46. }, {
  47. "id": "1664216695471054849",
  48. "parentId": "1664214513480867841",
  49. "children": [{
  50. "id": "1665018966920355842",
  51. "parentId": "1664216695471054849",
  52. "hasChildren": false,
  53. "title": "西门子",
  54. "key": "3",
  55. }, {
  56. "id": "1665018922062274561",
  57. "parentId": "1664216695471054849",
  58. "hasChildren": false,
  59. "title": "美的",
  60. "key": "3",
  61. }],
  62. "hasChildren": true,
  63. "title": "冰箱",
  64. "key": "2",
  65. }, {
  66. "id": "1664216721056309250",
  67. "parentId": "1664214513480867841",
  68. "children": [{
  69. "id": "1665019063980744706",
  70. "parentId": "1664216721056309250",
  71. "hasChildren": false,
  72. "title": "海尔",
  73. "key": "3",
  74. }, {
  75. "id": "1665019008213278722",
  76. "parentId": "1664216721056309250",
  77. "hasChildren": false,
  78. "title": "美的",
  79. "key": "3",
  80. }],
  81. "hasChildren": true,
  82. "title": "洗衣机",
  83. "key": "2",
  84. }, {
  85. "id": "1664216742065577985",
  86. "parentId": "1664214513480867841",
  87. "children": [{
  88. "id": "1665019154003091457",
  89. "parentId": "1664216742065577985",
  90. "hasChildren": false,
  91. "title": "长虹",
  92. "key": "3",
  93. }, {
  94. "id": "1665019122663251969",
  95. "parentId": "1664216742065577985",
  96. "hasChildren": false,
  97. "title": "TCL",
  98. "key": "3",
  99. }],
  100. "hasChildren": true,
  101. "title": "电视",
  102. "key": "2",
  103. }, {
  104. "id": "1664216765738229761",
  105. "parentId": "1664214513480867841",
  106. "children": [{
  107. "id": "1665019300166197250",
  108. "parentId": "1664216765738229761",
  109. "hasChildren": false,
  110. "title": "其他",
  111. "key": "3",
  112. }, {
  113. "id": "1665019261947699201",
  114. "parentId": "1664216765738229761",
  115. "hasChildren": false,
  116. "title": "万家乐",
  117. "key": "3",
  118. }],
  119. "hasChildren": true,
  120. "title": "热水器",
  121. "key": "2",
  122. }],
  123. "hasChildren": true,
  124. "title": "常用电器",
  125. "key": "1",
  126. }]
  127. <script>
  128. // 使用 map() 方法遍历返回来的数据中的每个对象
  129. const filteredData = this.tabList1.map(item => {
  130. let arr = []
  131. // 如果有子级
  132. if (item.hasChildren) {
  133. // 遍历每个对象的 children 数组
  134. const children = item.children.map(childItem => {
  135. // 如果有子级
  136. if (childItem.hasChildren) {
  137. // 到第三级、筛选出符合搜索条件的
  138. const grandchildren = childItem.children.filter(grandchildItem => {
  139. return grandchildItem.title.includes(this.keyword);
  140. });
  141. // 将过滤后的 grandchildren 赋值回 childItem 的 children 属性中
  142. if (grandchildren.length > 0) {
  143. console.log('grandchildren', grandchildren)
  144. childItem.children = grandchildren;
  145. return childItem;
  146. }
  147. }
  148. }).filter(childItem => (childItem !== undefined && childItem.length !==
  149. 0)); // 过滤childItem有undefined和[]的情况
  150. if (children.length > 0) {
  151. console.log('children', children)
  152. // 将过滤后的 children 赋值回 item 的 children 属性中
  153. item.children = children;
  154. return item;
  155. }
  156. }
  157. }).filter(item => (item !== undefined));
  158. console.log('filteredData ', filteredData)
  159. </script>

记分类管理页面相关代码 

  1. <template>
  2. <view @click="closeSwiper">
  3. <!-- 搜索栏 -->
  4. <view class="topfixed ">
  5. <view class="flex" style="background:#fff;padding:0 10px;height: 44px;">
  6. <!-- <view> -->
  7. <zb-tooltip color="white" content="Right Top" placement="bottom-center" ref="tooltip">
  8. <view slot="content" style="color: black;z-index: 999;">
  9. <view>
  10. <view
  11. :style="{'font-size': '14px','padding':'0 10px','color':titleType==0?'#2295FF':'#000'}"
  12. @click="clickTitle(0)">产品类</view>
  13. <view style="width: 100%;height: 1px;background: #eee;margin: 10px 0;"></view>
  14. <view
  15. :style="{'font-size': '14px','padding':'0 10px','color':titleType!=0?'#2295FF':'#000'}"
  16. @click="clickTitle(1)">服务类</view>
  17. </view>
  18. </view>
  19. <view class="tiptext flex">{{titleType==0?'产品类':'服务类'}}
  20. <view style="margin: 0 0px 0 10px;">
  21. <u-icon name="arrow-down" color='#2295ff' size="15px"></u-icon>
  22. </view>
  23. <text style="margin: 0 0px 0 10px;">| </text>
  24. </view>
  25. </zb-tooltip>
  26. <view style="width:82%;">
  27. <u-tabs :list="tabList" ref="uTabs" lineColor="#fff"
  28. :activeStyle="{'color':'#2295ff','font-weight':'bold'}" :current="tabsCurrent"
  29. @click="tabTitle">
  30. <view slot="right" style="padding-left: 4px;" @tap="clickRight">
  31. <u-icon name="arrow-right" size="21"></u-icon>
  32. </view>
  33. </u-tabs>
  34. </view>
  35. </view>
  36. </view>
  37. <view style="height:100rpx"></view>
  38. <u-empty v-if="(dataList.length==0||tabList.length==0) && nonePerson" text="暂无数据~" :icon="emptyImg"
  39. width="220rpx" height="220rpx" marginTop="20vh" />
  40. <view class="u-demo-block__content" v-else>
  41. <view style="display:flex">
  42. <!-- 左边分类名称 -->
  43. <view style="width:35vw">
  44. <view class="u-page leftitem">
  45. <u-list @scrolltolower="scrolltolower">
  46. <u-list-item v-for="(item, index) in dataList" :key="index">
  47. <u-cell size="large" :border="false"
  48. :customStyle="{'color':index==current?'#2295ff':'#101010','background':index==current?'#fff':'#f8f8f8'}"
  49. @click="clickItem(item,index)">
  50. <view slot="title"
  51. :style="{'color':index==current?'#2295ff':'#101010','font-weight':index==current?'bold':'none','padding':'13px','border-left':index==current?'3px solid #2295ff':'3px solid #fff','text-align':'center'}">
  52. {{item.title}}
  53. </view>
  54. </u-cell>
  55. </u-list-item>
  56. </u-list>
  57. </view>
  58. </view>
  59. <!-- 中间分割线 -->
  60. <!-- 右边设备名称 -->
  61. <view style="width:65vw; background: #fff;height:90vh">
  62. <view class="flex" style="padding:6px 10px;border-bottom: 1px solid #f0f0f0;">
  63. <u-search placeholder="请输入名称" v-model="keyword" :showAction="false" @change="searchChange"
  64. @clear="searchClear" :clearabled="true">
  65. </u-search>
  66. <view style="margin-left: 10px;" @click="addType">
  67. <u-icon name="plus-circle-fill" color="#2295ff" size="30px"></u-icon>
  68. </view>
  69. </view>
  70. <view class="demo-layout">
  71. <u-empty v-if="indexList.length==0" text="暂无数据~" :icon="emptyImg" width="220rpx" height="220rpx"
  72. marginTop="20vh" />
  73. <view class="u-page" v-else>
  74. <u-swipe-action ref="swipeActions">
  75. <u-list @scrolltolower="scrolltolower2">
  76. <u-list-item v-for="(item, index) in indexList" :key="index">
  77. <u-swipe-action-item :options="options" @click="clickGroup($event,item)">
  78. <view class="swipe-action u-border-bottom">
  79. <view class="swipe-action__content">
  80. <view class="swipe-action__content__text flexbetween">
  81. <view class="flex">
  82. <view style="margin-right:5px;margin-top: -5px;">
  83. <uni-icons type="smallcircle-filled" size="10"
  84. color="#2295FF">
  85. </uni-icons>
  86. </view>
  87. <view class="font14 oneline">
  88. {{item.title}}
  89. {{item.englishName}}
  90. </view>
  91. </view>
  92. </view>
  93. </view>
  94. </view>
  95. </u-swipe-action-item>
  96. </u-list-item>
  97. </u-list>
  98. </u-swipe-action>
  99. </view>
  100. </view>
  101. </view>
  102. </view>
  103. </view>
  104. </view>
  105. </template>
  106. <script>
  107. var util = require('@/utils/util.js');
  108. var api = require('@/config/api.js');
  109. export default {
  110. data() {
  111. return {
  112. emptyImg: '',
  113. tabList: [],
  114. dataList: [],
  115. indexList: [],
  116. popupShow: false,
  117. options: [{
  118. text: '修改',
  119. style: {
  120. backgroundColor: '#3c9cff',
  121. fontSize: '14px'
  122. }
  123. }, {
  124. text: '删除',
  125. style: {
  126. backgroundColor: '#f56c6c',
  127. fontSize: '14px'
  128. }
  129. }],
  130. current: 0,
  131. show: false,
  132. title: '',
  133. content: '',
  134. modelType: '', //确认框类型
  135. item: {},
  136. value: 0,
  137. titleType: 0, //0 产品类 1 服务类
  138. keyword: '',
  139. tabsCurrent: 0,
  140. typeId: '',
  141. titleTypeId: '',
  142. nonePerson: false,
  143. pageTotal: 0,
  144. pageTotal2: 0,
  145. pageCurrent: 1,
  146. pageCurrent2: 1,
  147. isSecond: true,
  148. isSecond2: true,
  149. nameValue: '',
  150. titleName: '',
  151. formatter: value => value.replace(/[^\a-zA-Z]/g, ''),
  152. btnLoading: false,
  153. tabList1: [], //用来筛选数据
  154. emtyDataList: [], //搜索为空时显示所有数据
  155. }
  156. },
  157. onLoad() {
  158. // this.loadmore()
  159. this.getTabList()
  160. },
  161. methods: {
  162. // 点击切换产品类、服务类
  163. clickTitle(str) {
  164. if (this.titleType == str) {
  165. return
  166. }
  167. this.tabsCurrent = 0;
  168. this.current = 0;
  169. this.pageCurrent = 1;
  170. this.pageCurrent2 = 1;
  171. this.isSecond = true;
  172. this.titleType = str;
  173. this.dataList = [];
  174. this.indexList = []
  175. this.getTabList()
  176. },
  177. // 获取头部数据
  178. getTabList() {
  179. uni.showLoading({
  180. mask: true
  181. })
  182. let params = {
  183. keyword: this.keyword,
  184. levelType: 3,
  185. companyId: 1,
  186. partitionType: this.titleType,
  187. companyId: 1,
  188. }
  189. util.request(api.prodTypePageClild, params, 'get').then((res) => {
  190. uni.hideLoading()
  191. if (res.code === 200 || res.code === '200') {
  192. this.tabList = [];
  193. this.tabList1 = [];
  194. if (res.data.length > 0) {
  195. res.data.forEach(el => {
  196. var obj = {
  197. name: el.title,
  198. id: el.id
  199. }
  200. this.tabList.push(obj)
  201. this.tabList1.push(el)
  202. this.emtyDataList.push(el)
  203. })
  204. this.titleTypeId = this.tabList[this.tabsCurrent].id
  205. this.getLeftList(this.titleTypeId, this.tabList1)
  206. } else {
  207. this.nonePerson = true
  208. }
  209. } else {
  210. uni.showToast({
  211. title: res.msg,
  212. icon: 'none'
  213. })
  214. }
  215. }).catch(() => {
  216. uni.hideLoading()
  217. })
  218. },
  219. // 点击头部导航
  220. tabTitle(e) {
  221. if (this.tabsCurrent == e.index) {
  222. return
  223. }
  224. this.tabsCurrent = e.index;
  225. this.titleTypeId = e.id
  226. uni.showLoading({
  227. mask: true
  228. })
  229. this.current = 0;
  230. this.dataList = [];
  231. this.indexList = [];
  232. // this.pageCurrent = 1
  233. // this.pageCurrent2 = 1
  234. this.isSecond = true
  235. // this.getLeftList(this.titleTypeId)
  236. console.log(e)
  237. for (let tab of this.tabList1) {
  238. if (tab.id === e.id) {
  239. if (tab.hasChildren) {
  240. const tabData = this.findLeft(tab.id, this.tabList1)
  241. console.log('tabData', tabData)
  242. // /this.dataList = this.dataList.concat(tabData.children);
  243. this.dataList = tabData.children
  244. console.log('this.dataList', this.dataList)
  245. uni.hideLoading()
  246. // 右边数据初始化
  247. this.getrightList(this.dataList[0].id, this.dataList)
  248. } else {
  249. this.indexList = []
  250. uni.hideLoading()
  251. }
  252. }
  253. }
  254. },
  255. findLeft(id, tabList1) {
  256. console.log('tabList1', id, tabList1)
  257. for (const tabData of tabList1) {
  258. if (tabData.id === id) {
  259. console.log('tabData', tabData)
  260. return tabData
  261. }
  262. }
  263. return null
  264. },
  265. scrolltolower() {
  266. if (this.pageTotal == this.pageCurrent) {
  267. return
  268. }
  269. this.isSecond = false;
  270. this.pageCurrent++
  271. uni.showLoading({
  272. mask: true
  273. })
  274. this.getLeftList()
  275. },
  276. getLeftList(id, tabList) {
  277. console.log('tabList', tabList)
  278. this.dataList = []
  279. if (tabList.length > 0) {
  280. if (tabList[0].hasChildren) {
  281. this.dataList = this.dataList.concat(tabList[0].children);
  282. if (this.isSecond && this.dataList.length > 0) {
  283. this.typeId = this.dataList[this.current].id
  284. this.getrightList(this.typeId, this.dataList)
  285. }
  286. } else {
  287. this.dataList = []
  288. }
  289. }
  290. },
  291. scrolltolower2() {
  292. if (this.pageTotal2 == this.pageCurrent2) {
  293. return
  294. }
  295. this.isSecond = false;
  296. this.pageCurrent2++
  297. uni.showLoading({
  298. mask: true
  299. })
  300. this.getrightList(this.typeId)
  301. },
  302. getrightList(id, dataList) {
  303. console.log('dataList', dataList)
  304. if (dataList.length > 0) {
  305. if (dataList[0].hasChildren) {
  306. this.indexList = this.indexList.concat(dataList[0].children);
  307. } else {
  308. this.indexList = []
  309. }
  310. }
  311. },
  312. // 关闭左滑
  313. closeSwiper() {
  314. if (this.$refs.tooltip) {
  315. this.$refs.tooltip.close()
  316. }
  317. if (this.$refs.swipeActions && this.$refs.swipeActions.children.length > 0) {
  318. this.$refs.swipeActions.children.forEach(el => {
  319. el.closeOther()
  320. })
  321. }
  322. },
  323. // 头部标题点击右侧
  324. clickRight() {
  325. // console.log(this.$refs.uTabs)
  326. let systemInfo = uni.getSystemInfoSync();
  327. var width = systemInfo.windowWidth - 150
  328. this.$refs.uTabs.scrollLeft += width
  329. if (this.$refs.uTabs.scrollLeft > this.$refs.uTabs.scrollViewWidth) {
  330. this.$refs.uTabs.scrollLeft = 0;
  331. }
  332. },
  333. // 搜索按钮
  334. searchBtn(e) {
  335. this.keyword = e
  336. if (this.keyword == '') {}
  337. },
  338. // 搜索
  339. searchChange(e) {
  340. console.log('搜索', e)
  341. this.keyword = e
  342. this.pageCurrent2 = 1;
  343. this.dataList = []
  344. this.indexList = []
  345. if (this.keyword != '') {
  346. // 使用 map() 方法遍历返回来的数据中的每个对象
  347. const filteredData = this.tabList1.map(item => {
  348. let arr = []
  349. // 如果有子级
  350. if (item.hasChildren) {
  351. // 遍历每个对象的 children 数组
  352. const children = item.children.map(childItem => {
  353. // 如果有子级
  354. if (childItem.hasChildren) {
  355. // 到第三级、筛选出符合搜索条件的
  356. const grandchildren = childItem.children.filter(grandchildItem => {
  357. return grandchildItem.title.includes(this.keyword);
  358. });
  359. // 将过滤后的 grandchildren 赋值回 childItem 的 children 属性中
  360. if (grandchildren.length > 0) {
  361. console.log('grandchildren', grandchildren)
  362. childItem.children = grandchildren;
  363. return childItem;
  364. }
  365. }
  366. }).filter(childItem => (childItem !== undefined && childItem.length !==
  367. 0)); // 过滤childItem有undefined和[]的情况
  368. if (children.length > 0) {
  369. console.log('children', children)
  370. // 将过滤后的 children 赋值回 item 的 children 属性中
  371. item.children = children;
  372. return item;
  373. }
  374. }
  375. }).filter(item => (item !== undefined));
  376. console.log('filteredData ', filteredData)
  377. let tabList = []
  378. let tabList1 = []
  379. // 处理搜到的数据赋值
  380. if (filteredData.length > 0) {
  381. filteredData.forEach(el => {
  382. //构造
  383. var obj = {
  384. name: el.title,
  385. id: el.id
  386. }
  387. tabList.push(obj)
  388. tabList1.push(el)
  389. })
  390. this.tabList = tabList
  391. this.tabList1 = tabList1
  392. if (this.tabList1.length > 0) {
  393. this.tabsCurrent = 0
  394. if (this.tabList1[0].hasChildren) {
  395. // 左边数据初始化
  396. this.dataList = this.dataList.concat(this.tabList1[0].children);
  397. if (this.dataList.length > 0) {
  398. this.current = 0
  399. if (this.dataList[0].hasChildren) {
  400. // 右边数据初始化
  401. this.indexList = this.indexList.concat(this.dataList[0].children);
  402. } else {
  403. this.indexList = []
  404. }
  405. }
  406. } else {
  407. this.dataList = []
  408. }
  409. }
  410. } else {
  411. // 搜不到相关数据
  412. this.nonePerson = true
  413. this.tabList = []
  414. this.dataList = []
  415. this.indexList = []
  416. this.indexList1 = []
  417. }
  418. } else {
  419. this.searchClear()
  420. }
  421. },
  422. // 清空按钮
  423. searchClear() {
  424. console.log('kong---111111')
  425. this.keyword = ''
  426. this.tabsCurrent = 0;
  427. this.current = 0;
  428. this.tabList = []
  429. this.tabList1 = []
  430. this.dataList = []
  431. this.indexList = []
  432. this.getTabList()
  433. },
  434. // 点击分类名称
  435. clickItem(obj, index) {
  436. this.current = index
  437. if (this.typeId == obj.id) {
  438. return
  439. }
  440. this.typeId = obj.id;
  441. uni.showLoading({
  442. mask: true
  443. })
  444. this.pageCurrent2 = 1;
  445. this.indexList = [];
  446. // this.getrightList(this.typeId)
  447. console.log('obj---', obj)
  448. if (obj.hasChildren) {
  449. const leftData = this.findRight(this.typeId, this.dataList)
  450. console.log('leftData', leftData)
  451. this.indexList = this.indexList.concat(leftData.children);
  452. uni.hideLoading()
  453. } else {
  454. this.indexList = []
  455. uni.hideLoading()
  456. }
  457. },
  458. findRight(id, dataList) {
  459. console.log('findRight', id, dataList)
  460. for (const leftData of dataList) {
  461. if (leftData.id === id) {
  462. return leftData
  463. }
  464. }
  465. return null
  466. },
  467. // 左滑点击事件
  468. clickGroup(e, item) {
  469. this.$refs.swipeActions.closeOther()
  470. this.btnLoading = false;
  471. if (e.index == 0) {
  472. this.item = item;
  473. this.title = "修改品牌名称"
  474. this.modelType = 3;
  475. this.titleName = item.typeName;
  476. this.nameValue = item.englishName
  477. this.range.forEach((el, index) => {
  478. if (item.region == el.text) {
  479. this.value = index
  480. }
  481. })
  482. // this.show = true;
  483. this.popupShow = true;
  484. } else {
  485. this.show = true;
  486. this.title = '删除'
  487. this.modelType = 4
  488. this.content = '是否确认要删除该品牌?'
  489. this.item = item
  490. // this.delGroup(item)
  491. }
  492. },
  493. }
  494. }
  495. </script>
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/247291
推荐阅读
相关标签
  

闽ICP备14008679号