当前位置:首页 > VUE

vue2.0 实现table

2026-01-22 22:01:23VUE

Vue 2.0 实现 Table 组件

基础表格实现

使用 v-for 指令渲染表格数据,绑定动态数据到表格的行和列。

<template>
  <table>
    <thead>
      <tr>
        <th v-for="(header, index) in headers" :key="index">{{ header }}</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="(row, rowIndex) in tableData" :key="rowIndex">
        <td v-for="(cell, cellIndex) in row" :key="cellIndex">{{ cell }}</td>
      </tr>
    </tbody>
  </table>
</template>

<script>
export default {
  data() {
    return {
      headers: ['Name', 'Age', 'Email'],
      tableData: [
        ['John', 25, 'john@example.com'],
        ['Jane', 30, 'jane@example.com']
      ]
    };
  }
};
</script>

动态绑定数据

通过 props 接收外部传入的数据,实现表格的动态渲染。

vue2.0 实现table

<template>
  <table>
    <thead>
      <tr>
        <th v-for="(header, index) in headers" :key="index">{{ header }}</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="(row, rowIndex) in data" :key="rowIndex">
        <td v-for="(cell, cellIndex) in row" :key="cellIndex">{{ cell }}</td>
      </tr>
    </tbody>
  </table>
</template>

<script>
export default {
  props: {
    headers: {
      type: Array,
      required: true
    },
    data: {
      type: Array,
      required: true
    }
  }
};
</script>

添加排序功能

通过计算属性实现表格数据的排序功能。

vue2.0 实现table

<template>
  <table>
    <thead>
      <tr>
        <th v-for="(header, index) in headers" :key="index" @click="sortTable(header)">
          {{ header }}
          <span v-if="sortKey === header">{{ sortOrder === 1 ? '↑' : '↓' }}</span>
        </th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="(row, rowIndex) in sortedData" :key="rowIndex">
        <td v-for="(cell, cellIndex) in row" :key="cellIndex">{{ cell }}</td>
      </tr>
    </tbody>
  </table>
</template>

<script>
export default {
  data() {
    return {
      sortKey: '',
      sortOrder: 1
    };
  },
  props: {
    headers: {
      type: Array,
      required: true
    },
    data: {
      type: Array,
      required: true
    }
  },
  computed: {
    sortedData() {
      if (!this.sortKey) return this.data;
      const index = this.headers.indexOf(this.sortKey);
      return [...this.data].sort((a, b) => {
        if (a[index] < b[index]) return -1 * this.sortOrder;
        if (a[index] > b[index]) return 1 * this.sortOrder;
        return 0;
      });
    }
  },
  methods: {
    sortTable(header) {
      if (this.sortKey === header) {
        this.sortOrder *= -1;
      } else {
        this.sortKey = header;
        this.sortOrder = 1;
      }
    }
  }
};
</script>

添加分页功能

通过计算属性实现表格的分页功能。

<template>
  <div>
    <table>
      <thead>
        <tr>
          <th v-for="(header, index) in headers" :key="index">{{ header }}</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(row, rowIndex) in paginatedData" :key="rowIndex">
          <td v-for="(cell, cellIndex) in row" :key="cellIndex">{{ cell }}</td>
        </tr>
      </tbody>
    </table>
    <div>
      <button @click="prevPage" :disabled="currentPage === 1">Previous</button>
      <span>{{ currentPage }} / {{ totalPages }}</span>
      <button @click="nextPage" :disabled="currentPage === totalPages">Next</button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentPage: 1,
      itemsPerPage: 2
    };
  },
  props: {
    headers: {
      type: Array,
      required: true
    },
    data: {
      type: Array,
      required: true
    }
  },
  computed: {
    totalPages() {
      return Math.ceil(this.data.length / this.itemsPerPage);
    },
    paginatedData() {
      const start = (this.currentPage - 1) * this.itemsPerPage;
      const end = start + this.itemsPerPage;
      return this.data.slice(start, end);
    }
  },
  methods: {
    nextPage() {
      if (this.currentPage < this.totalPages) {
        this.currentPage++;
      }
    },
    prevPage() {
      if (this.currentPage > 1) {
        this.currentPage--;
      }
    }
  }
};
</script>

使用第三方库

对于更复杂的需求,可以使用第三方库如 Element UIVuetify

<template>
  <el-table :data="tableData">
    <el-table-column prop="name" label="Name"></el-table-column>
    <el-table-column prop="age" label="Age"></el-table-column>
    <el-table-column prop="email" label="Email"></el-table-column>
  </el-table>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        { name: 'John', age: 25, email: 'john@example.com' },
        { name: 'Jane', age: 30, email: 'jane@example.com' }
      ]
    };
  }
};
</script>

注意事项

  • 确保 key 的唯一性,避免渲染性能问题。
  • 对于大数据量的表格,考虑使用虚拟滚动技术优化性能。
  • 表格样式可以通过 CSS 自定义,或使用第三方库的默认样式。

标签: table
分享给朋友:

相关文章

vue实现table行计算

vue实现table行计算

Vue 实现表格行计算的方法 在 Vue 中实现表格行计算通常涉及数据绑定、计算属性和方法调用。以下是几种常见的实现方式: 使用计算属性 计算属性适合处理依赖响应式数据的动态计算,例如对表格某一行的…

react实现竖向table

react实现竖向table

实现竖向表格的布局方案 在React中实现竖向表格(即表头在左侧,数据向右展开的布局)可通过CSS布局调整或组件结构重组完成。以下是几种常见方法: 使用CSS Flexbox或Grid布局 通过旋转…

vue 实现table切换

vue 实现table切换

Vue 实现 Table 切换的方法 使用 v-if 或 v-show 切换不同表格 通过条件渲染指令 v-if 或 v-show 可以快速切换不同的表格组件。v-if 是惰性的,适合切换频率低的场景…

vue table实现折叠

vue table实现折叠

Vue Table 实现折叠功能 在 Vue 中实现表格的折叠功能,可以通过动态控制行或列的显示隐藏来实现。以下是几种常见的实现方式: 使用 v-if 或 v-show 控制行显示 通过绑定一个变量…

vue实现table自动滚动

vue实现table自动滚动

Vue 实现表格自动滚动 使用 CSS 动画实现 通过 CSS 的 @keyframes 和 animation 属性实现表格内容的滚动效果。这种方法简单且性能较好,适合静态数据。 <temp…

Vue中table实现CheckBox

Vue中table实现CheckBox

Vue中实现Table的CheckBox功能 在Vue中为表格添加CheckBox功能,可以通过以下方式实现。这里以Element UI的el-table为例,原生表格的实现逻辑类似。 基础实现 定…