Vue表格隐藏行折叠效果如何实现

其他教程   发布日期:2025年01月27日   浏览次数:204

这篇“Vue表格隐藏行折叠效果如何实现”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“Vue表格隐藏行折叠效果如何实现”文章吧。

实现步骤

  1. 在Vue组件的模板中,定义表格的基本结构。使用v-for指令从数据源中遍历渲染表格的行数据。其中,需要添加一个绑定click事件的行,用于触发行折叠效果。代码示例如下:

  1. <template>
  2. <div>
  3. <table>
  4. <thead>
  5. <tr>
  6. <th>姓名</th>
  7. <th>年龄</th>
  8. <th>身高</th>
  9. </tr>
  10. </thead>
  11. <tbody>
  12. <tr v-for="(item,index) in tableData" :key="index">
  13. <td>{{item.name}}</td>
  14. <td>{{item.age}}</td>
  15. <td>{{item.height}}</td>
  16. <td class="fold" @click="foldRow(index)"></td>
  17. </tr>
  18. </tbody>
  19. </table>
  20. </div>
  21. </template>
  1. 在组件的data属性中定义变量,用于判断表格中的行是否需要折叠。并且在初始化组件时,将所有行的状态设置为未折叠。代码示例如下:

  1. <script>
  2. export default {
  3. data() {
  4. return {
  5. tableData: [
  6. { name: '小明', age: '20', height: '180' },
  7. { name: '小红', age: '19', height: '170' },
  8. { name: '小刚', age: '22', height: '185' },
  9. ],
  10. foldArr: [],
  11. };
  12. },
  13. created() {
  14. this.foldArr = new Array(this.tableData.length).fill(false);
  15. },
  16. methods: {
  17. foldRow(index) {
  18. this.foldArr.splice(index, 1, !this.foldArr[index]);
  19. },
  20. },
  21. };
  22. </script>
  1. 定义一个折叠行的组件。组件的模板中包含需要折叠的内容。当某一行需要折叠时,将隐藏内容渲染进来。组件代码示例如下:

  1. <template>
  2. <div v-show="foldArr[index]">
  3. <p>{{item.intro}}</p>
  4. </div>
  5. </template>
  6. <script>
  7. export default {
  8. props: ['item', 'index', 'foldArr'],
  9. };
  10. </script>
  1. 在表格的body中,添加一个包含折叠行组件的tr。通过v-if指令判断当前行是否需要折叠,如果折叠,则渲染折叠行组件。代码示例如下:

  1. <template>
  2. <div>
  3. <table>
  4. <thead>
  5. <tr>
  6. <th>姓名</th>
  7. <th>年龄</th>
  8. <th>身高</th>
  9. </tr>
  10. </thead>
  11. <tbody>
  12. <tr v-for="(item,index) in tableData" :key="index">
  13. <td>{{item.name}}</td>
  14. <td>{{item.age}}</td>
  15. <td>{{item.height}}</td>
  16. <td class="fold" @click="foldRow(index)"></td>
  17. </tr>
  18. <tr v-for="(item,index) in tableData" :key="index">
  19. <td colspan="4" v-if="foldArr[index]">
  20. <fold-row :item="item" :index="index" :foldArr="foldArr"></fold-row>
  21. </td>
  22. </tr>
  23. </tbody>
  24. </table>
  25. </div>
  26. </template>
  27. <script>
  28. import FoldRow from '@/components/FoldRow.vue';
  29. export default {
  30. components: {
  31. FoldRow,
  32. },
  33. data() {
  34. return {
  35. tableData: [
  36. { name: '小明', age: '20', height: '180', intro: '我是小明,今年20岁,身高180cm' },
  37. { name: '小红', age: '19', height: '170', intro: '我是小红,今年19岁,身高170cm' },
  38. { name: '小刚', age: '22', height: '185', intro: '我是小刚,今年22岁,身高185cm' },
  39. ],
  40. foldArr: [],
  41. };
  42. },
  43. created() {
  44. this.foldArr = new Array(this.tableData.length).fill(false);
  45. },
  46. methods: {
  47. foldRow(index) {
  48. this.foldArr.splice(index, 1, !this.foldArr[index]);
  49. },
  50. },
  51. };
  52. </script>
  1. 对于样式的处理,可以使用CSS进行控制。通过设置.fold的width和height为0,使其无占用空间。通过设置.fold:before和.fold:after伪元素的样式,来实现折叠图标的切换。代码示例如下:

  1. <style lang="scss">
  2. .fold {
  3. position: relative;
  4. width: 0;
  5. height: 0;
  6. &:before,
  7. &:after {
  8. position: absolute;
  9. left: 50%;
  10. transform: translateX(-50%);
  11. content: '';
  12. width: 6px;
  13. height: 6px;
  14. border-radius: 50%;
  15. background-color: #999;
  16. transition: all 0.2s ease;
  17. }
  18. &:before {
  19. top: 0;
  20. }
  21. &:after {
  22. bottom: 0;
  23. }
  24. }
  25. .fold.folded:before {
  26. transform: translateY(2px) translateX(-50%) rotate(45deg);
  27. }
  28. .fold.folded:after {
  29. transform: translateY(-2px) translateX(-50%) rotate(-45deg);
  30. }
  31. </style>

以上就是Vue表格隐藏行折叠效果如何实现的详细内容,更多关于Vue表格隐藏行折叠效果如何实现的资料请关注九品源码其它相关文章!