当前位置:首页 > VUE

vue 实现页面 表格

2026-01-17 16:07:02VUE

Vue 实现页面表格的方法

Vue 提供了多种方式实现表格功能,可以根据需求选择不同的方案。以下是常见的实现方法:

使用原生 HTML 表格

通过 Vue 的模板语法直接渲染表格数据:

<template>
  <table>
    <thead>
      <tr>
        <th v-for="header in headers" :key="header">{{ header }}</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="row in rows" :key="row.id">
        <td v-for="(value, key) in row" :key="key">{{ value }}</td>
      </tr>
    </tbody>
  </table>
</template>

<script>
export default {
  data() {
    return {
      headers: ['ID', 'Name', 'Age'],
      rows: [
        { id: 1, name: 'John', age: 25 },
        { id: 2, name: 'Jane', age: 30 }
      ]
    }
  }
}
</script>

使用第三方表格组件库

Element UI 的表格组件示例:

<template>
  <el-table :data="tableData" style="width: 100%">
    <el-table-column prop="date" label="Date" width="180"></el-table-column>
    <el-table-column prop="name" label="Name" width="180"></el-table-column>
    <el-table-column prop="address" label="Address"></el-table-column>
  </el-table>
</template>

<script>
export default {
  data() {
    return {
      tableData: [{
        date: '2016-05-02',
        name: 'John',
        address: 'New York'
      }, {
        date: '2016-05-04',
        name: 'Jane',
        address: 'London'
      }]
    }
  }
}
</script>

实现表格分页功能

结合分页组件实现数据分页:

<template>
  <div>
    <table>
      <!-- 表格内容 -->
    </table>
    <div class="pagination">
      <button @click="prevPage" :disabled="currentPage === 1">Previous</button>
      <span>Page {{ currentPage }} of {{ totalPages }}</span>
      <button @click="nextPage" :disabled="currentPage === totalPages">Next</button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentPage: 1,
      pageSize: 10,
      totalItems: 100
    }
  },
  computed: {
    totalPages() {
      return Math.ceil(this.totalItems / this.pageSize)
    }
  },
  methods: {
    prevPage() {
      if (this.currentPage > 1) this.currentPage--
    },
    nextPage() {
      if (this.currentPage < this.totalPages) this.currentPage++
    }
  }
}
</script>

表格排序功能实现

为表格添加排序功能:

<template>
  <table>
    <thead>
      <tr>
        <th @click="sortBy('name')">Name</th>
        <th @click="sortBy('age')">Age</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="row in sortedRows" :key="row.id">
        <td>{{ row.name }}</td>
        <td>{{ row.age }}</td>
      </tr>
    </tbody>
  </table>
</template>

<script>
export default {
  data() {
    return {
      rows: [
        { id: 1, name: 'John', age: 25 },
        { id: 2, name: 'Jane', age: 30 }
      ],
      sortKey: '',
      sortDirection: 1
    }
  },
  computed: {
    sortedRows() {
      if (!this.sortKey) return this.rows
      return [...this.rows].sort((a, b) => {
        return (a[this.sortKey] > b[this.sortKey] ? 1 : -1) * this.sortDirection
      })
    }
  },
  methods: {
    sortBy(key) {
      if (this.sortKey === key) {
        this.sortDirection *= -1
      } else {
        this.sortKey = key
        this.sortDirection = 1
      }
    }
  }
}
</script>

表格筛选功能

添加搜索框实现表格筛选:

<template>
  <div>
    <input v-model="searchText" placeholder="Search...">
    <table>
      <tbody>
        <tr v-for="row in filteredRows" :key="row.id">
          <td>{{ row.name }}</td>
          <td>{{ row.age }}</td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchText: '',
      rows: [
        { id: 1, name: 'John', age: 25 },
        { id: 2, name: 'Jane', age: 30 }
      ]
    }
  },
  computed: {
    filteredRows() {
      return this.rows.filter(row => 
        row.name.toLowerCase().includes(this.searchText.toLowerCase()) ||
        row.age.toString().includes(this.searchText)
      )
    }
  }
}
</script>

响应式表格设计

使用 CSS 实现响应式表格:

<template>
  <div class="table-responsive">
    <table>
      <!-- 表格内容 -->
    </table>
  </div>
</template>

<style>
.table-responsive {
  overflow-x: auto;
}

@media (max-width: 600px) {
  table {
    display: block;
  }
  th, td {
    display: block;
    width: 100%;
  }
}
</style>

以上方法可以根据实际项目需求组合使用,构建功能完善的表格组件。

vue 实现页面 表格

标签: 表格页面
分享给朋友:

相关文章

vue页面实现

vue页面实现

Vue 页面实现方法 创建 Vue 组件 使用 Vue 单文件组件(SFC)方式创建页面,文件扩展名为 .vue。一个基本的 Vue 组件包含三个部分:模板(template)、脚本(script)和…

vue实现转页面

vue实现转页面

Vue 实现页面跳转的方法 在 Vue 中实现页面跳转通常可以通过以下几种方式完成,具体取决于项目结构和需求。 使用 router-link 组件 router-link 是 Vue Router…

vue页面实现日历

vue页面实现日历

Vue 页面实现日历的方法 使用第三方组件库 推荐使用成熟的日历组件库,如 v-calendar 或 fullcalendar-vue,快速实现功能丰富的日历。 安装 v-calendar: np…

vue实现页面分页

vue实现页面分页

Vue实现页面分页的方法 使用Element UI的分页组件 Element UI提供了现成的分页组件el-pagination,可以快速实现分页功能。需要先安装Element UI库。 <…

css表格制作

css表格制作

CSS 表格制作基础 使用 CSS 可以创建美观且响应式的表格。以下是一些基本方法: table 元素用于定义表格,tr 定义行,td 定义单元格,th 定义表头单元格。 <table>…

使用vue实现表格

使用vue实现表格

使用 Vue 实现表格 在 Vue 中实现表格可以通过多种方式完成,以下是一个基于 Vue 3 的示例,展示如何动态渲染表格数据并支持基础功能(如排序、筛选)。 基础表格实现 通过 v-for 循环…