当前位置:首页 > 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>

表格筛选功能

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

<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 的表单验证库(如 VeeValidate)或自定义验证规则,确保用户输入的账号和密码符合要求。例如,检查邮箱格式、密码长度等。 发送登录…

vue实现生成二维码

vue实现生成二维码

使用qrcode.vue库生成二维码 安装qrcode.vue库: npm install qrcode.vue --save 在Vue组件中使用: <template> &l…

vue  select实现

vue select实现

Vue Select 实现方法 在 Vue 中实现下拉选择功能可以使用原生 <select> 标签或第三方库如 vue-select。以下是两种方法的详细说明: 原生 HTML Sele…

vue功能实现

vue功能实现

Vue 功能实现指南 Vue.js 是一个渐进式 JavaScript 框架,广泛用于构建用户界面。以下是 Vue 功能的常见实现方法。 数据绑定 Vue 的核心功能之一是数据绑定,通过 v-mod…

vue实现类别

vue实现类别

Vue 实现类别的几种方法 在 Vue 中实现类别功能可以通过多种方式完成,以下是一些常见的方法: 使用 v-bind:class 动态绑定类名 通过对象语法或数组语法动态绑定类名,根据条件添加或…

vue 实现流程

vue 实现流程

Vue 实现流程 Vue.js 是一个渐进式 JavaScript 框架,用于构建用户界面。以下是 Vue 实现的基本流程: 安装 Vue.js 通过 CDN 引入或使用 npm/yarn 安装:…