当前位置:   article > 正文

使用vue的多条件筛选以及当前行多余内容隐藏_vue多条件联动筛选

vue多条件联动筛选

效果如下:

这是参考了其他博主的代码进行的更改。

实现对三种条件筛选,默认是第一项‘全部’,且实现点击后面的更多出现当前行隐藏的内容

 代码实现:

  1. <div class="row" v-for="(i, k) in typeList" :key="k">
  2. <div class="name">{{ i.title }}:</div>
  3. <div class="content" :class="{ hidden: !i.isActive }">
  4. <div
  5. v-for="(item, index) in i.contentList"
  6. :key="index"
  7. @click="handleClick(k, item, index)"
  8. :class="{ actvCss: item.isShow }"
  9. >
  10. {{ item.text }}
  11. </div>
  12. </div>
  13. <div class="collapse-right" @click="click(i, k)">
  14. <span v-if="!i.isActive">更多</span>
  15. <span v-if="i.isActive">收起</span>
  16. </div>
  17. </div>
  18. <!-- 商品展示 -->
  19. <div class="ContentBox" v-if="!comData.length">
  20. <div class="nullCss">暂无相关商品~</div>
  21. </div>
  22. <div class="DataListBox">
  23. <div class="box" v-for="(item, index) in comData" :key="index">
  24. <div>{{ item.Platform }}</div>
  25. <div>{{ item.Title }}</div>
  26. <div>{{ item.Condition }}</div>
  27. </div>
  28. </div>
  29. <!--实现点击的元素的展示-->
  30. <div v-for="(i, v) in newArr0" :key="v">
  31. {{ i.text }}
  32. </div>
  1. data(){
  2. return{
  3. activeKey: "",
  4. newArr0: [],
  5. filterForm: {
  6. PlatformVar: "全部", // 表示当前选中的平台 设置默认 不选择时为全部
  7. TypeVar: "全部", // 表示当前选中的类型
  8. ConditionVar: "全部", // 表示当前选中的成色
  9. },
  10. typeList: [
  11. {
  12. title: "平台",
  13. contentList: [
  14. {
  15. id: 1,
  16. text: "全部",
  17. },
  18. {
  19. text: "淘宝",
  20. },
  21. {
  22. text: "天猫",
  23. },
  24. {
  25. text: "京东",
  26. },
  27. {
  28. text: "闲鱼",
  29. },
  30. {
  31. text: "转转",
  32. },
  33. {
  34. text: "1111",
  35. },
  36. {
  37. text: "222222",
  38. },
  39. {
  40. text: "3333",
  41. },
  42. ], // 平台
  43. },
  44. {
  45. title: "类型",
  46. contentList: [
  47. {
  48. id: 2,
  49. text: "全部",
  50. },
  51. {
  52. text: "CPU",
  53. },
  54. {
  55. text: "显卡",
  56. },
  57. {
  58. text: "内存条",
  59. },
  60. {
  61. text: "硬盘",
  62. },
  63. ], // 类型
  64. },
  65. {
  66. title: "成色",
  67. contentList: [
  68. {
  69. id: 3,
  70. text: "全部",
  71. },
  72. {
  73. text: "全新",
  74. },
  75. {
  76. text: "打折",
  77. },
  78. {
  79. text: "二手",
  80. },
  81. {
  82. text: "停售",
  83. },
  84. ], // 成色
  85. },
  86. ],
  87. DataList: [
  88. // 模拟接口返回的数据结构
  89. {
  90. id: 1,
  91. Title: "i9 13900k",
  92. Platform: "淘宝",
  93. Type: "CPU",
  94. Condition: "全新",
  95. },
  96. {
  97. id: 2,
  98. Title: "i9 9900ks",
  99. Platform: "闲鱼",
  100. Type: "CPU",
  101. Condition: "停售",
  102. },
  103. {
  104. id: 3,
  105. Title: "i9 11900k",
  106. Platform: "天猫",
  107. Type: "CPU",
  108. Condition: "全新",
  109. },
  110. {
  111. id: 4,
  112. Title: "i5 13600k",
  113. Platform: "转转",
  114. Type: "CPU",
  115. Condition: "停售",
  116. },
  117. {
  118. id: 5,
  119. Title: "i5 10400f",
  120. Platform: "京东",
  121. Type: "CPU",
  122. Condition: "全新",
  123. },
  124. {
  125. id: 6,
  126. Title: "i3 12100f",
  127. Platform: "闲鱼",
  128. Type: "CPU",
  129. Condition: "打折",
  130. },
  131. ],
  132. }
  133. },
  134. //当内容发生变化时,会重新计算(筛选)
  135. computed:{
  136. comData() {
  137. return this.DataList.filter((i) => {
  138. console.log(i);
  139. if (
  140. (this.filterForm.PlatformVar === "全部" ||
  141. this.filterForm.PlatformVar === i.Platform) &&
  142. (this.filterForm.TypeVar === "全部" ||
  143. this.filterForm.TypeVar === i.Type) &&
  144. (this.filterForm.ConditionVar === "全部" ||
  145. this.filterForm.ConditionVar === i.Condition)
  146. ) {
  147. return i;
  148. }
  149. });
  150. },
  151. },
  152. //数据的初始化
  153. created() {
  154. this.typeList.forEach((i) => {
  155. //为对象数组的每一项设置 isActive 为 false
  156. this.$set(i, "isActive", false);
  157. //对当前行中的每个元素进行设置
  158. i.contentList.forEach((item, index) => {
  159. //设置一个项的为true,其余为false,这样默认第一项有样式
  160. if (index === 0) {
  161. this.$set(item, "isShow", true);
  162. } else {
  163. this.$set(item, "isShow", false);
  164. }
  165. });
  166. });
  167. },
  168. methods:{
  169. handleClick(parentIndex, val, childIndex) {
  170. // 添加isShow===true ,显示点击出现的颜色
  171. this.typeList[parentIndex].contentList.map((item) => {
  172. item.isShow = false;
  173. });
  174. this.typeList[parentIndex].contentList[childIndex].isShow = true;
  175. //选中的数据
  176. let newArr1 = [];
  177. this.typeList.map((i) => {
  178. i.contentList.map((item) => {
  179. if (item.isShow == true) {
  180. newArr1.push(item);
  181. }
  182. });
  183. });
  184. this.newArr0 = newArr1;
  185. this.filterForm.PlatformVar = this.newArr0[0].text;
  186. this.filterForm.TypeVar = this.newArr0[1].text;
  187. this.filterForm.ConditionVar = this.newArr0[2].text;
  188. },
  189. click(item, index) {
  190. this.activeKey = index;
  191. //判断点击的 ‘activeKey’ 是否等于点击当前行的索引,
  192. if (this.activeKey === index) {
  193. item.isActive = !item.isActive;//根据索引值是否相等来对 isActive的值进行取反,实现‘更多’‘收起’的效果
  194. }
  195. },
  196. }
  1. .row {
  2. display: flex;
  3. margin-bottom: 10px;
  4. font-size: 20px;
  5. cursor: pointer;
  6. border: 1px solid seagreen;
  7. .name {
  8. width: 200px;
  9. }
  10. .content {
  11. display: flex;
  12. flex-wrap: wrap;
  13. width: 800px;
  14. div {
  15. margin-right: 50px;
  16. }
  17. }
  18. .hidden {
  19. height: 26px;
  20. overflow: hidden;
  21. }
  22. }
  23. .actvCss {
  24. background-color: aquamarine;
  25. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/76800
推荐阅读