当前位置:首页 > 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 的 v-model 绑定表单数据,例如收货地址、支付方式等。通过 Vue 的 computed 属性计算总价,结合 v-if 或 v-…

vue实现页面定位

vue实现页面定位

Vue 实现页面定位的方法 在 Vue 中实现页面定位通常可以通过以下几种方式完成,包括使用原生 JavaScript 的 scrollIntoView 方法、Vue Router 的滚动行为配置,以…

vue页面实现日历

vue页面实现日历

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

css制作表格

css制作表格

使用HTML和CSS创建表格 HTML中的<table>元素用于创建表格,结合CSS可以调整样式。以下是一个基础示例: <table> <tr> &…

vue实现页面缓存

vue实现页面缓存

使用 <keep-alive> 组件实现缓存 Vue 内置的 <keep-alive> 组件可以缓存动态组件或路由组件。通过包裹需要缓存的组件,可以保留其状态避免重复渲染。…

css怎么制作表格

css怎么制作表格

使用HTML和CSS创建表格 HTML提供<table>标签用于创建表格结构,结合CSS可以自定义样式。以下是基本实现方法: HTML结构 <table> <…