当前位置:首页 > VUE

纯vue实现表格

2026-02-19 05:37:01VUE

纯 Vue 实现表格的方法

基础表格结构

使用 Vue 的模板语法直接渲染表格数据。通过 v-for 指令动态生成表格行和列。

<template>
  <table>
    <thead>
      <tr>
        <th v-for="header in headers" :key="header">{{ header }}</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="(row, index) in rows" :key="index">
        <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>

动态数据绑定

通过 v-model 实现表格数据的双向绑定,适用于可编辑表格场景。

<template>
  <table>
    <tbody>
      <tr v-for="(row, index) in rows" :key="index">
        <td v-for="(cell, cellIndex) in row" :key="cellIndex">
          <input v-model="rows[index][cellIndex]" type="text">
        </td>
      </tr>
    </tbody>
  </table>
</template>

表格排序功能

通过计算属性实现客户端排序,点击表头时切换排序方式。

<template>
  <table>
    <thead>
      <tr>
        <th 
          v-for="header in headers" 
          :key="header.key"
          @click="sortBy(header.key)"
        >
          {{ header.text }}
          <span v-if="sortKey === header.key">
            {{ sortOrder > 0 ? '↑' : '↓' }}
          </span>
        </th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="row in sortedRows" :key="row.id">
        <td v-for="header in headers" :key="header.key">{{ row[header.key] }}</td>
      </tr>
    </tbody>
  </table>
</template>

<script>
export default {
  data() {
    return {
      sortKey: '',
      sortOrder: 1,
      headers: [
        { text: '姓名', key: 'name' },
        { text: '年龄', key: 'age' }
      ],
      rows: [
        { id: 1, name: '张三', age: 25 },
        { id: 2, name: '李四', age: 30 }
      ]
    };
  },
  computed: {
    sortedRows() {
      const key = this.sortKey;
      if (!key) return this.rows;

      return [...this.rows].sort((a, b) => {
        return (a[key] > b[key] ? 1 : -1) * this.sortOrder;
      });
    }
  },
  methods: {
    sortBy(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: 5,
      allData: [] // 所有数据
    };
  },
  computed: {
    paginatedData() {
      const start = (this.currentPage - 1) * this.pageSize;
      return this.allData.slice(start, start + this.pageSize);
    },
    totalPages() {
      return Math.ceil(this.allData.length / this.pageSize);
    }
  }
};
</script>

表格筛选功能

通过输入框实现表格数据的实时筛选。

纯vue实现表格

<template>
  <div>
    <input v-model="searchQuery" placeholder="搜索...">
    <table>
      <tbody>
        <tr v-for="row in filteredRows" :key="row.id">
          <td v-for="header in headers" :key="header.key">{{ row[header.key] }}</td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchQuery: '',
      rows: [] // 原始数据
    };
  },
  computed: {
    filteredRows() {
      const query = this.searchQuery.toLowerCase();
      return this.rows.filter(row => {
        return Object.values(row).some(
          val => String(val).toLowerCase().includes(query)
        );
      });
    }
  }
};
</script>

注意事项

  • 对于大数据量表格(超过1000行),建议使用虚拟滚动技术优化性能
  • 复杂表格功能(如合并单元格)可能需要结合CSS或第三方库实现
  • 样式控制可以通过Scoped CSS或CSS Modules实现组件隔离

以上实现方式均基于Vue核心功能,无需额外依赖库,适合需要轻量级表格解决方案的场景。

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

相关文章

vue 实现权限

vue 实现权限

Vue 实现权限控制的方法 在 Vue 项目中实现权限控制通常涉及前端路由、组件和按钮级别的权限管理。以下是几种常见的实现方式: 路由权限控制 通过路由守卫实现权限验证,过滤用户无权访问的路由:…

vue 实现pdf

vue 实现pdf

在Vue中实现PDF功能 在Vue项目中实现PDF功能通常涉及PDF生成、预览或下载。以下是几种常见实现方式: 使用vue-pdf库预览PDF 安装依赖: npm install vue-pdf…

vue实现机制

vue实现机制

Vue 实现机制的核心原理 Vue.js 的核心实现机制主要基于响应式系统、虚拟 DOM 和组件化设计。以下是关键实现细节: 响应式系统 Vue 通过 Object.defineProperty(V…

vue实现gridlayout

vue实现gridlayout

Vue 实现 Grid Layout 使用 CSS Grid 布局 CSS Grid 是一种强大的布局系统,可以直接在 Vue 组件的样式中使用。通过定义网格容器和网格项,可以快速实现复杂的布局结构。…

vue实现字幕

vue实现字幕

Vue 实现字幕功能 在 Vue 中实现字幕功能可以通过动态绑定数据和 CSS 动画来实现。以下是具体实现方法: 1. 基础字幕组件 创建一个字幕组件,通过 v-model 或 props 接收字…

vue实现多线程

vue实现多线程

Vue.js 本身是单线程框架,但可以通过以下方法实现类似多线程的效果或利用浏览器多线程能力: 使用 Web Worker Web Worker 是浏览器提供的多线程 API,适合处理 CPU 密集…