当前位置:首页 > VUE

vue实现分组表格

2026-01-15 07:28:48VUE

Vue 实现分组表格的方法

使用 v-for 嵌套循环实现分组

通过嵌套的 v-for 循环可以轻松实现分组表格。外层循环遍历分组数据,内层循环遍历每组中的具体项。

<template>
  <table>
    <tbody v-for="group in groupedData" :key="group.name">
      <tr class="group-header">
        <th colspan="2">{{ group.name }}</th>
      </tr>
      <tr v-for="item in group.items" :key="item.id">
        <td>{{ item.name }}</td>
        <td>{{ item.value }}</td>
      </tr>
    </tbody>
  </table>
</template>

<script>
export default {
  data() {
    return {
      groupedData: [
        {
          name: 'Group 1',
          items: [
            { id: 1, name: 'Item 1', value: 'Value 1' },
            { id: 2, name: 'Item 2', value: 'Value 2' }
          ]
        },
        {
          name: 'Group 2',
          items: [
            { id: 3, name: 'Item 3', value: 'Value 3' }
          ]
        }
      ]
    }
  }
}
</script>

<style>
.group-header {
  background-color: #f5f5f5;
  font-weight: bold;
}
</style>

使用计算属性动态分组

当原始数据需要按特定条件分组时,可以使用计算属性动态生成分组数据。

<template>
  <table>
    <tbody v-for="group in groupedItems" :key="group.category">
      <tr class="group-header">
        <th colspan="2">{{ group.category }}</th>
      </tr>
      <tr v-for="item in group.items" :key="item.id">
        <td>{{ item.name }}</td>
        <td>{{ item.price }}</td>
      </tr>
    </tbody>
  </table>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { id: 1, name: 'Apple', price: 1.2, category: 'Fruit' },
        { id: 2, name: 'Banana', price: 0.8, category: 'Fruit' },
        { id: 3, name: 'Carrot', price: 0.5, category: 'Vegetable' }
      ]
    }
  },
  computed: {
    groupedItems() {
      const groups = {}
      this.items.forEach(item => {
        if (!groups[item.category]) {
          groups[item.category] = {
            category: item.category,
            items: []
          }
        }
        groups[item.category].items.push(item)
      })
      return Object.values(groups)
    }
  }
}
</script>

使用第三方组件库

对于更复杂的分组表格需求,可以考虑使用专门的UI组件库:

vue实现分组表格

  1. Element UI 的表格组件支持分组显示

    <el-table :data="tableData">
    <el-table-column prop="date" label="Date"></el-table-column>
    <el-table-column label="Group">
     <el-table-column prop="name" label="Name"></el-table-column>
     <el-table-column prop="amount" label="Amount"></el-table-column>
    </el-table-column>
    </el-table>
  2. Vuetify 的数据表格也支持类似功能

    vue实现分组表格

    <v-data-table
    :headers="headers"
    :items="items"
    group-by="category"
    ></v-data-table>

实现可折叠的分组表格

通过添加展开/折叠功能可以提升分组表格的交互体验。

<template>
  <table>
    <tbody v-for="group in groups" :key="group.name">
      <tr class="group-header" @click="toggleGroup(group)">
        <th colspan="2">
          {{ group.name }}
          <span>{{ group.isOpen ? '▼' : '▶' }}</span>
        </th>
      </tr>
      <tr v-for="item in group.items" v-show="group.isOpen" :key="item.id">
        <td>{{ item.name }}</td>
        <td>{{ item.value }}</td>
      </tr>
    </tbody>
  </table>
</template>

<script>
export default {
  data() {
    return {
      groups: [
        {
          name: 'Group 1',
          items: [
            { id: 1, name: 'Item 1', value: 'Value 1' },
            { id: 2, name: 'Item 2', value: 'Value 2' }
          ],
          isOpen: true
        },
        {
          name: 'Group 2',
          items: [
            { id: 3, name: 'Item 3', value: 'Value 3' }
          ],
          isOpen: false
        }
      ]
    }
  },
  methods: {
    toggleGroup(group) {
      group.isOpen = !group.isOpen
    }
  }
}
</script>

响应式分组表格设计

为了确保分组表格在不同设备上都能良好显示,可以添加响应式设计。

@media screen and (max-width: 600px) {
  table {
    display: block;
  }
  tbody {
    display: block;
    margin-bottom: 1rem;
  }
  .group-header th {
    display: block;
  }
  tr {
    display: block;
    margin-bottom: 0.5rem;
  }
  td {
    display: block;
    text-align: right;
  }
  td::before {
    content: attr(data-label);
    float: left;
    font-weight: bold;
  }
}

这些方法涵盖了从基础到高级的Vue分组表格实现方式,可以根据具体需求选择合适的方法或组合使用多种技术。

标签: 表格vue
分享给朋友:

相关文章

vue实现文档导入

vue实现文档导入

Vue 实现文档导入的方法 在 Vue 中实现文档导入功能通常涉及文件上传、解析和处理。以下是几种常见的方法: 使用原生文件输入和 FileReader 通过 HTML 的原生 <input…

vue实现用户信息表

vue实现用户信息表

Vue 实现用户信息表的方法 使用 Vue CLI 创建项目 安装 Vue CLI 并初始化项目: npm install -g @vue/cli vue create user-info-tab…

vue实现下拉刷新组件

vue实现下拉刷新组件

实现下拉刷新组件的核心思路 下拉刷新功能通常通过监听触摸事件、滚动位置和动画效果实现。Vue中可以利用自定义指令或封装组件完成,以下为两种常见实现方式。 基于自定义指令的实现 创建指令监听触摸事件…

vue请求实现进度条

vue请求实现进度条

实现请求进度条的方法 在Vue中实现请求进度条可以通过多种方式完成,常见的有使用第三方库如axios结合nprogress或自定义进度条组件。以下是几种实现方法: 使用axios和nprogres…

vue实现一个tab栏

vue实现一个tab栏

Vue 实现 Tab 栏 基础实现(选项式 API) <template> <div class="tab-container"> <div class="t…

vue实现生成二维码

vue实现生成二维码

使用qrcode.vue库生成二维码 安装qrcode.vue库: npm install qrcode.vue --save 在Vue组件中使用: <template> &l…