当前位置:首页 > VUE

vue实现的表格

2026-02-17 11:45:14VUE

Vue 实现的表格

Vue 中实现表格可以通过多种方式,包括使用原生 HTML 表格、第三方组件库(如 Element UI、Ant Design Vue)或自定义表格组件。以下是几种常见方法:

使用原生 HTML 表格

通过 Vue 的 v-for 指令动态渲染表格数据,适合简单表格需求。

<template>
  <table>
    <thead>
      <tr>
        <th v-for="header in headers" :key="header">{{ header }}</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="row in data" :key="row.id">
        <td v-for="header in headers" :key="header">{{ row[header] }}</td>
      </tr>
    </tbody>
  </table>
</template>

<script>
export default {
  data() {
    return {
      headers: ['Name', 'Age', 'Email'],
      data: [
        { id: 1, Name: 'Alice', Age: 25, Email: 'alice@example.com' },
        { id: 2, Name: 'Bob', Age: 30, Email: 'bob@example.com' }
      ]
    };
  }
};
</script>

使用 Element UI 的表格组件

Element UI 提供了功能丰富的表格组件,支持排序、分页、筛选等高级功能。

<template>
  <el-table :data="data" style="width: 100%">
    <el-table-column prop="Name" label="Name" width="180"></el-table-column>
    <el-table-column prop="Age" label="Age" width="180"></el-table-column>
    <el-table-column prop="Email" label="Email"></el-table-column>
  </el-table>
</template>

<script>
export default {
  data() {
    return {
      data: [
        { Name: 'Alice', Age: 25, Email: 'alice@example.com' },
        { Name: 'Bob', Age: 30, Email: 'bob@example.com' }
      ]
    };
  }
};
</script>

自定义可编辑表格

通过 Vue 的双向绑定实现表格单元格的编辑功能。

<template>
  <table>
    <thead>
      <tr>
        <th v-for="header in headers" :key="header">{{ header }}</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="(row, index) in data" :key="row.id">
        <td v-for="header in headers" :key="header">
          <input v-model="row[header]" v-if="editable[index]" />
          <span v-else>{{ row[header] }}</span>
        </td>
        <td>
          <button @click="toggleEdit(index)">
            {{ editable[index] ? 'Save' : 'Edit' }}
          </button>
        </td>
      </tr>
    </tbody>
  </table>
</template>

<script>
export default {
  data() {
    return {
      headers: ['Name', 'Age', 'Email'],
      data: [
        { id: 1, Name: 'Alice', Age: 25, Email: 'alice@example.com' },
        { id: 2, Name: 'Bob', Age: 30, Email: 'bob@example.com' }
      ],
      editable: []
    };
  },
  methods: {
    toggleEdit(index) {
      this.$set(this.editable, index, !this.editable[index]);
    }
  }
};
</script>

使用 Ant Design Vue 的表格

Ant Design Vue 也提供了强大的表格组件,支持自定义渲染和复杂交互。

<template>
  <a-table :columns="columns" :data-source="data" row-key="id">
    <template #action="{ record }">
      <a-button @click="handleEdit(record)">Edit</a-button>
    </template>
  </a-table>
</template>

<script>
export default {
  data() {
    return {
      columns: [
        { title: 'Name', dataIndex: 'Name', key: 'Name' },
        { title: 'Age', dataIndex: 'Age', key: 'Age' },
        { title: 'Email', dataIndex: 'Email', key: 'Email' },
        { title: 'Action', key: 'action', slots: { customRender: 'action' } }
      ],
      data: [
        { id: 1, Name: 'Alice', Age: 25, Email: 'alice@example.com' },
        { id: 2, Name: 'Bob', Age: 30, Email: 'bob@example.com' }
      ]
    };
  },
  methods: {
    handleEdit(record) {
      console.log('Edit:', record);
    }
  }
};
</script>

动态加载数据的表格

通过 API 异步加载表格数据,适合处理大量数据。

vue实现的表格

<template>
  <table v-if="!loading">
    <thead>
      <tr>
        <th v-for="header in headers" :key="header">{{ header }}</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="row in data" :key="row.id">
        <td v-for="header in headers" :key="header">{{ row[header] }}</td>
      </tr>
    </tbody>
  </table>
  <div v-else>Loading...</div>
</template>

<script>
export default {
  data() {
    return {
      headers: ['Name', 'Age', 'Email'],
      data: [],
      loading: true
    };
  },
  async created() {
    try {
      const response = await fetch('https://api.example.com/users');
      this.data = await response.json();
    } catch (error) {
      console.error('Error fetching data:', error);
    } finally {
      this.loading = false;
    }
  }
};
</script>

注意事项

  • 对于复杂表格(如树形表格、虚拟滚动),建议使用成熟的组件库(如 Element UI 或 Ant Design Vue)。
  • 自定义表格时,注意性能优化,尤其是处理大量数据时。
  • 动态数据加载时,添加加载状态和错误处理以提升用户体验。

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

相关文章

vue实现tag

vue实现tag

Vue 实现标签(Tag)功能 在 Vue 中实现标签(Tag)功能可以通过多种方式完成,以下是一些常见的方法和实现步骤: 使用动态组件和 v-for 通过 v-for 指令动态渲染标签列表,结合样…

vue实现tap

vue实现tap

Vue 实现 Tap 事件 在移动端开发中,Tap 事件比 Click 事件更常用,因为 Click 事件有 300ms 延迟。以下是几种在 Vue 中实现 Tap 事件的方法。 使用第三方库 安装…

vue实现翻页

vue实现翻页

Vue实现翻页功能 在Vue中实现翻页功能通常需要结合分页组件和数据请求逻辑。以下是常见的实现方法: 使用Element UI分页组件 安装Element UI后,可以直接使用其分页组件:…

vue 实现聊天

vue 实现聊天

使用 Vue 实现聊天功能 创建 Vue 项目并安装依赖 确保已安装 Vue CLI,通过以下命令创建新项目: vue create chat-app 进入项目目录后,安装必要的依赖(如 Socke…

黑马vue实现

黑马vue实现

以下是关于使用 Vue.js 实现黑马相关功能或项目的关键方法和实践指南: 环境配置与项目初始化 安装最新版 Vue CLI 或使用 Vite 创建项目: npm create vue@lates…

vue ui实现创建vue项目

vue ui实现创建vue项目

使用 Vue UI 创建 Vue 项目 Vue UI 是 Vue CLI 提供的图形化界面工具,可以通过可视化操作创建和管理 Vue 项目。 安装 Vue CLI 确保已安装 Node.js(建议版…