当前位置:首页 > VUE

用vue实现表格

2026-01-15 23:52:59VUE

Vue 表格实现方法

基础表格实现

使用 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: 28, job: '工程师' },
        { name: '李四', age: 32, job: '设计师' },
        { name: '王五', age: 25, job: '产品经理' }
      ]
    }
  }
}
</script>

动态表格组件

创建可复用的表格组件,通过 props 接收数据,提供排序、筛选等功能。

<template>
  <div>
    <input v-model="searchText" placeholder="搜索..." />
    <table>
      <thead>
        <tr>
          <th v-for="col in columns" :key="col.key" @click="sortBy(col.key)">
            {{ col.title }}
          </th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="item in filteredData" :key="item.id">
          <td v-for="col in columns" :key="`${item.id}-${col.key}`">
            {{ item[col.key] }}
          </td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

<script>
export default {
  props: {
    data: Array,
    columns: Array
  },
  data() {
    return {
      searchText: '',
      sortKey: '',
      sortOrder: 1
    }
  },
  computed: {
    filteredData() {
      let data = [...this.data]
      if (this.searchText) {
        data = data.filter(item => 
          this.columns.some(col => 
            String(item[col.key]).toLowerCase().includes(this.searchText.toLowerCase())
          )
        )
      }
      if (this.sortKey) {
        data.sort((a, b) => {
          return a[this.sortKey] > b[this.sortKey] ? this.sortOrder : -this.sortOrder
        })
      }
      return data
    }
  },
  methods: {
    sortBy(key) {
      if (this.sortKey === key) {
        this.sortOrder *= -1
      } else {
        this.sortKey = key
        this.sortOrder = 1
      }
    }
  }
}
</script>

使用第三方表格组件

对于复杂需求,可以使用成熟的第三方表格组件库,如 Element UI 的 el-table 或 Vuetify 的 v-data-table。

以 Element UI 为例:

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

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

表格性能优化

对于大数据量表格,使用虚拟滚动技术提升性能。可以借助 vue-virtual-scroller 等库实现。

<template>
  <RecycleScroller
    class="scroller"
    :items="tableData"
    :item-size="50"
    key-field="id"
    v-slot="{ item }"
  >
    <div class="row">
      <div class="cell">{{ item.name }}</div>
      <div class="cell">{{ item.age }}</div>
      <div class="cell">{{ item.job }}</div>
    </div>
  </RecycleScroller>
</template>

<script>
import { RecycleScroller } from 'vue-virtual-scroller'
import 'vue-virtual-scroller/dist/vue-virtual-scroller.css'

export default {
  components: { RecycleScroller },
  data() {
    return {
      tableData: [] // 大数据数组
    }
  }
}
</script>

<style>
.scroller {
  height: 500px;
}
.row {
  display: flex;
}
.cell {
  flex: 1;
  padding: 10px;
  border-bottom: 1px solid #eee;
}
</style>

表格编辑功能

实现可编辑表格,通过 v-model 绑定数据,或使用第三方组件如 vxe-table 提供的高级编辑功能。

<template>
  <table>
    <thead>
      <tr>
        <th>姓名</th>
        <th>年龄</th>
        <th>操作</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="(item, index) in tableData" :key="index">
        <td>
          <input v-model="item.name" />
        </td>
        <td>
          <input v-model.number="item.age" type="number" />
        </td>
        <td>
          <button @click="removeRow(index)">删除</button>
        </td>
      </tr>
    </tbody>
  </table>
  <button @click="addRow">添加行</button>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        { name: '张三', age: 28 },
        { name: '李四', age: 32 }
      ]
    }
  },
  methods: {
    addRow() {
      this.tableData.push({ name: '', age: 0 })
    },
    removeRow(index) {
      this.tableData.splice(index, 1)
    }
  }
}
</script>

用vue实现表格

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

相关文章

实现 vue ssr

实现 vue ssr

Vue SSR 实现方法 Vue SSR(Server-Side Rendering)通过服务器端渲染 Vue 应用,提升首屏加载速度和 SEO 友好性。以下是核心实现方法: 基础配置 安装必要…

vue实现跑车

vue实现跑车

Vue 实现跑车动画效果 使用 Vue 结合 CSS 动画可以实现跑车在屏幕上移动的效果。以下是一个完整的实现方案: 创建 Vue 组件结构 <template> <div c…

vue实现图库

vue实现图库

Vue 实现图库的基本方法 使用 Vue 实现图库功能可以通过多种方式完成,以下是一些常见的方法和实现步骤。 使用 Vue 和第三方库(如 Vue Gallery) 安装 vue-gallery 或…

vue实现rtc

vue实现rtc

Vue 实现 RTC 的步骤 安装依赖 在 Vue 项目中安装 WebRTC 相关依赖,如 simple-peer 或 peerjs,用于简化 WebRTC 的实现。 运行以下命令安装 simple-…

vue实现图册

vue实现图册

Vue实现图册功能 在Vue中实现图册功能通常涉及图片展示、切换、缩放等交互效果。以下是常见的实现方法和关键代码示例: 基础图册组件结构 使用Vue单文件组件构建基础结构: <templ…

vue实现hover

vue实现hover

Vue 实现 hover 效果的方法 在 Vue 中实现 hover 效果可以通过多种方式实现,包括使用 CSS、Vue 指令或事件监听。以下是几种常见的方法: 使用 CSS :hover 伪类…