当前位置:首页 > VUE

vue实现表格输出

2026-02-17 18:11:13VUE

Vue 实现表格输出的方法

使用 v-for 指令渲染表格数据

在 Vue 中,可以通过 v-for 指令动态渲染表格数据。定义一个数组作为数据源,然后在模板中使用 v-for 循环输出表格行。

<template>
  <table>
    <thead>
      <tr>
        <th>姓名</th>
        <th>年龄</th>
        <th>职业</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="(item, index) in tableData" :key="index">
        <td>{{ item.name }}</td>
        <td>{{ item.age }}</td>
        <td>{{ item.job }}</td>
      </tr>
    </tbody>
  </table>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        { name: '张三', age: 25, job: '工程师' },
        { name: '李四', age: 30, job: '设计师' },
        { name: '王五', age: 28, job: '产品经理' }
      ]
    }
  }
}
</script>

动态绑定表格样式

可以通过 v-bind 动态绑定表格的样式或类名,实现条件渲染。

<template>
  <table>
    <tr v-for="(item, index) in tableData" :key="index" :class="{ 'highlight': item.age > 28 }">
      <td>{{ item.name }}</td>
      <td>{{ item.age }}</td>
      <td>{{ item.job }}</td>
    </tr>
  </table>
</template>

<style>
.highlight {
  background-color: #f0f0f0;
}
</style>

使用计算属性处理表格数据

通过计算属性对表格数据进行处理,例如排序或过滤。

<template>
  <table>
    <tr v-for="(item, index) in sortedData" :key="index">
      <td>{{ item.name }}</td>
      <td>{{ item.age }}</td>
      <td>{{ item.job }}</td>
    </tr>
  </table>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        { name: '张三', age: 25, job: '工程师' },
        { name: '李四', age: 30, job: '设计师' },
        { name: '王五', age: 28, job: '产品经理' }
      ]
    }
  },
  computed: {
    sortedData() {
      return this.tableData.sort((a, b) => a.age - b.age);
    }
  }
}
</script>

使用第三方表格组件

对于复杂需求,可以集成第三方表格组件如 Element UIVuetify

<template>
  <el-table :data="tableData" style="width: 100%">
    <el-table-column prop="name" label="姓名"></el-table-column>
    <el-table-column prop="age" label="年龄"></el-table-column>
    <el-table-column prop="job" label="职业"></el-table-column>
  </el-table>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        { name: '张三', age: 25, job: '工程师' },
        { name: '李四', age: 30, job: '设计师' },
        { name: '王五', age: 28, job: '产品经理' }
      ]
    }
  }
}
</script>

分页和筛选功能

通过添加分页和筛选逻辑增强表格功能。

vue实现表格输出

<template>
  <div>
    <input v-model="searchQuery" placeholder="搜索姓名" />
    <table>
      <tr v-for="(item, index) in filteredData" :key="index">
        <td>{{ item.name }}</td>
        <td>{{ item.age }}</td>
        <td>{{ item.job }}</td>
      </tr>
    </table>
    <div>
      <button @click="prevPage" :disabled="currentPage === 1">上一页</button>
      <span>第 {{ currentPage }} 页</span>
      <button @click="nextPage" :disabled="currentPage >= totalPages">下一页</button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        { name: '张三', age: 25, job: '工程师' },
        { name: '李四', age: 30, job: '设计师' },
        { name: '王五', age: 28, job: '产品经理' }
      ],
      searchQuery: '',
      currentPage: 1,
      itemsPerPage: 2
    }
  },
  computed: {
    filteredData() {
      return this.tableData
        .filter(item => item.name.includes(this.searchQuery))
        .slice((this.currentPage - 1) * this.itemsPerPage, this.currentPage * this.itemsPerPage);
    },
    totalPages() {
      return Math.ceil(this.tableData.length / this.itemsPerPage);
    }
  },
  methods: {
    prevPage() {
      if (this.currentPage > 1) this.currentPage--;
    },
    nextPage() {
      if (this.currentPage < this.totalPages) this.currentPage++;
    }
  }
}
</script>

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

相关文章

vue实现toggle

vue实现toggle

Vue 实现 Toggle 功能 在 Vue 中实现 toggle(切换)功能可以通过多种方式完成,以下是几种常见的方法: 使用 v-model 和 v-on 通过 v-model 绑定数据,结合…

vue实现 单选

vue实现 单选

实现 Vue 单选功能 在 Vue 中实现单选功能可以通过多种方式完成,以下是几种常见的方法: 使用 v-model 绑定单选按钮 通过 v-model 可以轻松实现单选功能,将单选按钮的值绑定到同…

vue实现视频

vue实现视频

Vue 实现视频播放的方法 在 Vue 项目中实现视频播放可以通过多种方式完成,以下是几种常见的方法: 使用 HTML5 的 <video> 标签 在 Vue 组件中直接使用 HTML…

vue 实现平面

vue 实现平面

Vue 实现平面的方法 在 Vue 中实现平面效果通常涉及 CSS 样式、UI 框架或自定义组件的使用。以下是几种常见方法: 使用 CSS 样式 通过 Vue 的样式绑定或 scoped CSS 为…

vue watch实现

vue watch实现

在 Vue 中实现 watch 监听数据变化 Vue 的 watch 选项用于监听数据变化并执行相应的回调函数。以下是几种常见的实现方式: 基础用法 export default { data(…

vue for实现

vue for实现

Vue 中的 v-for 指令 v-for 是 Vue.js 中用于渲染列表数据的核心指令,通过遍历数组或对象生成动态内容。 基本语法(数组) <ul> <li v-for="…