当前位置:首页 > 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中实现表格复选框功能,可以通过以下方法完成: 方法一:使用el-table(Element UI) 安装Element UI后,在表格列中直接添加type="sele…

css表格制作表格

css表格制作表格

CSS 表格制作方法 使用 CSS 创建表格可以通过多种方式实现,包括原生 HTML 表格元素结合 CSS 样式,或者使用纯 CSS 布局技术(如 Flexbox 或 Grid)模拟表格效果。以下是常…

vue 实现表格单选

vue 实现表格单选

实现表格单选的基本思路 在Vue中实现表格单选功能,通常需要结合表格组件(如el-table)和数据绑定。核心逻辑是通过点击事件更新当前选中行的唯一标识(如id),并通过:class或:style绑定…

vue实现表格追加

vue实现表格追加

实现表格数据追加的方法 在Vue中实现表格数据追加可以通过多种方式完成,主要涉及数据绑定和数组操作。以下是几种常见的方法: 数据绑定与v-for指令 通过v-for指令渲染表格数据,利用数组的co…

css怎么制作表格

css怎么制作表格

使用HTML和CSS制作表格 在HTML中,表格由<table>标签定义,结合CSS可以灵活控制样式。 基本HTML结构: <table> <tr>…

vue表格实现导出

vue表格实现导出

使用 vue-json-excel 插件 安装插件: npm install vue-json-excel --save 在组件中引入并注册: import JsonExcel from 'vue…