当前位置:首页 > 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 实现 Table 的基本方法 使用 Vue 实现表格功能可以通过多种方式完成,包括原生 HTML 表格、第三方组件库或自定义组件。以下是几种常见的方法: 原生 HTML 表格 在 Vue 模…

vue+实现table切换

vue+实现table切换

Vue 实现 Table 切换的方法 在 Vue 中实现表格切换功能,可以通过动态渲染不同的表格数据或切换不同的表格组件来完成。以下是几种常见的实现方式: 动态绑定表格数据 通过切换数据源实现表格…

vue table 内实现联动

vue table 内实现联动

实现 Vue Table 联动功能 联动功能通常指表格内多行或多列数据之间存在动态关联关系,例如选择某一行后,其他行数据自动更新或触发特定操作。以下是几种常见实现方式: 数据绑定与计算属性 利用…

js实现table

js实现table

使用原生JavaScript创建表格 创建HTML表格需要结合DOM操作,以下是基础实现方法: <!DOCTYPE html> <html> <body> &l…

vue实现table

vue实现table

Vue 实现 Table 的基本方法 使用原生 HTML 表格 通过 Vue 的 v-for 指令动态渲染表格数据,适合简单场景。 <template> <table>…