当前位置:首页 > 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 提供的高级编辑功能。

用vue实现表格

<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实现triger

vue实现triger

在Vue中实现触发器(trigger)功能通常涉及自定义事件或DOM事件触发。以下是几种常见场景的实现方法: 自定义事件触发 通过$emit方法触发父组件中监听的自定义事件: // 子组件 thi…

实现vue cli

实现vue cli

安装 Vue CLI 确保系统中已安装 Node.js(版本建议 12.x 或更高)。通过 npm 全局安装 Vue CLI: npm install -g @vue/cli 安装完成后验证版本:…

vue实现uuid

vue实现uuid

使用 uuid 库生成 UUID 在 Vue 项目中,可以使用 uuid 库来生成 UUID。安装依赖: npm install uuid 在 Vue 组件中使用: import { v4 as…

vue 实现循环

vue 实现循环

Vue 实现循环的方法 在 Vue 中,可以使用 v-for 指令来实现循环渲染列表数据。以下是几种常见的循环实现方式: 基本数组循环 <template> <ul>…

vue实现页面

vue实现页面

Vue 实现页面的基本方法 创建 Vue 项目 使用 Vue CLI 快速初始化项目,运行以下命令安装并创建项目: npm install -g @vue/cli vue create my-pro…

vue实现结算页面

vue实现结算页面

Vue 实现结算页面的核心步骤 数据绑定与表单验证 使用 Vue 的 v-model 绑定表单数据,例如收货地址、支付方式等。通过 Vue 的 computed 属性计算总价,结合 v-if 或 v-…