当前位置:首页 > 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 接收外部传入的数据,实现表格的动态渲染。

<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>

添加排序功能

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

<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 自定义,或使用第三方库的默认样式。

vue2.0 实现table

标签: table
分享给朋友:

相关文章

elementui table

elementui table

ElementUI Table 基本用法 ElementUI 的 Table 组件用于展示结构化数据,支持排序、筛选、分页等功能。以下是一个基础示例: <template> &…

实现vue table

实现vue table

实现 Vue 表格的基础方法 使用 Vue 实现表格功能可以通过原生 HTML 表格结合 Vue 的数据绑定,或者使用第三方组件库(如 Element UI、Vuetify)快速构建。以下是几种常见方…

h5 实现table

h5 实现table

实现 H5 表格的基本方法 使用 HTML5 的 <table> 标签可以快速创建表格结构。以下是一个基础示例: <table border="1"> <thea…

vue怎么实现table切换

vue怎么实现table切换

实现 Vue 表格切换的方法 在 Vue 中实现表格切换通常涉及动态渲染不同的表格数据或切换不同的表格组件。以下是几种常见的方法: 使用 v-if 或 v-show 切换表格 通过条件渲染指令 v…

vue实现table行计算

vue实现table行计算

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

Vue中table实现CheckBox

Vue中table实现CheckBox

Vue中实现表格复选框功能 在Vue中为表格添加复选框功能可以通过多种方式实现,以下是几种常见方法: 使用v-model绑定选中状态 为表格的每一行数据添加一个选中状态字段,通过v-model绑定复…