当前位置:首页 > VUE

vue表格动态实现

2026-03-08 18:21:48VUE

Vue 表格动态实现方法

使用 v-for 动态渲染表格

通过 v-for 指令可以动态渲染表格行和列数据。定义一个数组作为数据源,在模板中使用 v-for 循环生成表格内容。

<template>
  <table>
    <thead>
      <tr>
        <th v-for="(header, index) in headers" :key="index">{{ header }}</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="(row, rowIndex) in rows" :key="rowIndex">
        <td v-for="(cell, cellIndex) in row" :key="cellIndex">{{ cell }}</td>
      </tr>
    </tbody>
  </table>
</template>

<script>
export default {
  data() {
    return {
      headers: ['姓名', '年龄', '职业'],
      rows: [
        ['张三', 25, '工程师'],
        ['李四', 30, '设计师'],
        ['王五', 28, '产品经理']
      ]
    }
  }
}
</script>

动态列配置

可以通过配置对象实现更灵活的表格结构,支持自定义列宽、对齐方式等属性。

vue表格动态实现

<template>
  <table>
    <thead>
      <tr>
        <th v-for="col in columns" :key="col.prop" :style="{ width: col.width }">
          {{ col.label }}
        </th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="(item, index) in tableData" :key="index">
        <td v-for="col in columns" :key="col.prop" :style="{ textAlign: col.align }">
          {{ item[col.prop] }}
        </td>
      </tr>
    </tbody>
  </table>
</template>

<script>
export default {
  data() {
    return {
      columns: [
        { prop: 'name', label: '姓名', width: '150px', align: 'left' },
        { prop: 'age', label: '年龄', width: '100px', align: 'center' },
        { prop: 'job', label: '职业', width: '200px', align: 'left' }
      ],
      tableData: [
        { name: '张三', age: 25, job: '工程师' },
        { name: '李四', age: 30, job: '设计师' },
        { name: '王五', age: 28, job: '产品经理' }
      ]
    }
  }
}
</script>

动态表格组件

封装可复用的动态表格组件,支持传入配置和数据。

vue表格动态实现

<!-- DynamicTable.vue -->
<template>
  <table>
    <thead>
      <tr>
        <th v-for="col in columns" :key="col.prop">
          {{ col.label }}
        </th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="(item, index) in data" :key="index">
        <td v-for="col in columns" :key="col.prop">
          {{ item[col.prop] }}
        </td>
      </tr>
    </tbody>
  </table>
</template>

<script>
export default {
  props: {
    columns: {
      type: Array,
      required: true
    },
    data: {
      type: Array,
      required: true
    }
  }
}
</script>

<!-- 使用组件 -->
<template>
  <dynamic-table :columns="columns" :data="tableData" />
</template>

动态排序功能

添加排序功能,通过点击表头实现数据排序。

<template>
  <table>
    <thead>
      <tr>
        <th 
          v-for="col in columns" 
          :key="col.prop"
          @click="sortTable(col.prop)"
        >
          {{ col.label }}
          <span v-if="sortKey === col.prop">
            {{ sortOrder > 0 ? '↑' : '↓' }}
          </span>
        </th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="(item, index) in sortedData" :key="index">
        <td v-for="col in columns" :key="col.prop">
          {{ item[col.prop] }}
        </td>
      </tr>
    </tbody>
  </table>
</template>

<script>
export default {
  data() {
    return {
      sortKey: '',
      sortOrder: 1,
      columns: [
        { prop: 'name', label: '姓名' },
        { prop: 'age', label: '年龄' },
        { prop: 'job', label: '职业' }
      ],
      tableData: [
        { name: '张三', age: 25, job: '工程师' },
        { name: '李四', age: 30, job: '设计师' },
        { name: '王五', age: 28, job: '产品经理' }
      ]
    }
  },
  computed: {
    sortedData() {
      if (!this.sortKey) return this.tableData
      return [...this.tableData].sort((a, b) => {
        const valueA = a[this.sortKey]
        const valueB = b[this.sortKey]
        return this.sortOrder * (valueA > valueB ? 1 : -1)
      })
    }
  },
  methods: {
    sortTable(key) {
      if (this.sortKey === key) {
        this.sortOrder *= -1
      } else {
        this.sortKey = key
        this.sortOrder = 1
      }
    }
  }
}
</script>

动态分页功能

结合分页组件实现表格数据分页显示。

<template>
  <div>
    <table>
      <!-- 表格内容同上 -->
    </table>
    <div class="pagination">
      <button 
        v-for="page in totalPages" 
        :key="page"
        @click="currentPage = page"
      >
        {{ page }}
      </button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentPage: 1,
      pageSize: 2,
      // 其他数据同上
    }
  },
  computed: {
    paginatedData() {
      const start = (this.currentPage - 1) * this.pageSize
      const end = start + this.pageSize
      return this.sortedData.slice(start, end)
    },
    totalPages() {
      return Math.ceil(this.tableData.length / this.pageSize)
    }
  }
}
</script>

标签: 表格动态
分享给朋友:

相关文章

vue日历表格实现

vue日历表格实现

实现Vue日历表格的基本步骤 使用Vue实现日历表格需要处理日期数据生成、渲染和交互逻辑。以下是核心实现方法: 安装依赖(可选) 若需复杂功能可引入date-fns或dayjs等日期库: npm…

css表格制作

css表格制作

CSS表格制作方法 使用CSS创建表格可以通过多种方式实现,以下是常见的几种方法: 使用HTML原生表格标签结合CSS样式 <table class="styled-table">…

vue实现分组表格

vue实现分组表格

Vue 实现分组表格的方法 使用 v-for 嵌套循环实现分组 通过嵌套的 v-for 循环可以轻松实现分组表格。外层循环遍历分组数据,内层循环遍历每组中的具体项。 <template>…

vue实现动态表单

vue实现动态表单

Vue 动态表单实现方法 使用 v-for 动态渲染表单字段 通过遍历数组动态生成表单项,数据驱动视图变化。表单字段配置可存储在数组中,修改数组即可实现表单动态更新。 <template>…

vue实现表格合并

vue实现表格合并

实现表格合并的方法 在Vue中实现表格合并通常涉及对表格数据的处理和单元格属性的动态绑定。以下是几种常见的方法: 使用rowspan和colspan属性 通过计算需要合并的行或列数,动态绑定rows…

vue实现条纹表格

vue实现条纹表格

实现条纹表格的基本方法 在Vue中实现条纹表格可以通过CSS的:nth-child选择器或结合Vue的动态类绑定来完成。以下是两种常见实现方式: 使用纯CSS实现 <template>…