当前位置:首页 > VUE

vue 实现table组件

2026-02-18 07:17:01VUE

Vue 实现 Table 组件

基础表格实现

使用 Vue 的 v-for 指令可以轻松渲染表格数据。以下是一个基础表格的实现示例:

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

<script>
export default {
  props: {
    headers: Array,
    data: Array
  }
}
</script>

添加排序功能

通过添加排序方法,可以实现表格列的升序和降序排列:

vue 实现table组件

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

<script>
export default {
  props: {
    headers: Array,
    data: Array
  },
  data() {
    return {
      sortKey: '',
      sortOrder: 1
    }
  },
  computed: {
    sortedData() {
      if (!this.sortKey) return this.data
      return [...this.data].sort((a, b) => {
        const valueA = a[this.sortKey]
        const valueB = b[this.sortKey]
        return this.sortOrder * (valueA > valueB ? 1 : -1)
      })
    }
  },
  methods: {
    sortBy(key) {
      if (this.sortKey === key) {
        this.sortOrder *= -1
      } else {
        this.sortKey = key
        this.sortOrder = 1
      }
    }
  }
}
</script>

添加分页功能

结合分页组件可以实现大数据量的分页显示:

<template>
  <div>
    <table>
      <!-- 表格内容同上 -->
    </table>
    <div class="pagination">
      <button @click="prevPage" :disabled="currentPage === 1">上一页</button>
      <span>{{ currentPage }} / {{ totalPages }}</span>
      <button @click="nextPage" :disabled="currentPage === totalPages">下一页</button>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    headers: Array,
    data: Array,
    itemsPerPage: {
      type: Number,
      default: 10
    }
  },
  data() {
    return {
      currentPage: 1
    }
  },
  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.sortedData.slice(start, end)
    }
  },
  methods: {
    prevPage() {
      if (this.currentPage > 1) this.currentPage--
    },
    nextPage() {
      if (this.currentPage < this.totalPages) this.currentPage++
    }
  }
}
</script>

添加筛选功能

实现基于关键词的表格数据筛选:

vue 实现table组件

<template>
  <div>
    <input v-model="searchQuery" placeholder="搜索..." />
    <table>
      <!-- 表格内容同上 -->
    </table>
  </div>
</template>

<script>
export default {
  props: {
    headers: Array,
    data: Array
  },
  data() {
    return {
      searchQuery: ''
    }
  },
  computed: {
    filteredData() {
      if (!this.searchQuery) return this.data
      const query = this.searchQuery.toLowerCase()
      return this.data.filter(row => {
        return Object.values(row).some(value => 
          String(value).toLowerCase().includes(query)
        )
      })
    }
  }
}
</script>

添加行选择功能

实现多行选择和全选功能:

<template>
  <table>
    <thead>
      <tr>
        <th>
          <input type="checkbox" v-model="selectAll" @change="toggleAll" />
        </th>
        <!-- 其他表头 -->
      </tr>
    </thead>
    <tbody>
      <tr v-for="(row, index) in data" :key="index">
        <td>
          <input 
            type="checkbox" 
            v-model="selectedRows" 
            :value="row.id" 
            @change="updateSelectAll"
          />
        </td>
        <!-- 其他单元格 -->
      </tr>
    </tbody>
  </table>
</template>

<script>
export default {
  props: {
    data: Array
  },
  data() {
    return {
      selectedRows: [],
      selectAll: false
    }
  },
  methods: {
    toggleAll() {
      this.selectedRows = this.selectAll 
        ? this.data.map(row => row.id) 
        : []
    },
    updateSelectAll() {
      this.selectAll = this.selectedRows.length === this.data.length
    }
  }
}
</script>

响应式设计

添加响应式样式,使表格在不同设备上表现良好:

<style scoped>
table {
  width: 100%;
  border-collapse: collapse;
}

th, td {
  padding: 8px;
  text-align: left;
  border-bottom: 1px solid #ddd;
}

th {
  background-color: #f2f2f2;
  cursor: pointer;
}

@media screen and (max-width: 600px) {
  table {
    display: block;
    overflow-x: auto;
  }
}
</style>

以上实现展示了 Vue 表格组件的基本功能,可以根据实际需求进行组合和扩展。对于更复杂的需求,可以考虑使用现成的表格组件库如 Element UI 的 ElTable 或 Vuetify 的 v-data-table。

标签: 组件vue
分享给朋友:

相关文章

js实现vue

js实现vue

Vue.js 的基本实现方法 在 JavaScript 中实现 Vue.js 的功能,可以通过直接引入 Vue 库或使用现代构建工具(如 Vite 或 Webpack)。以下是几种常见的实现方式:…

vue实现摘要

vue实现摘要

Vue 实现摘要的方法 在 Vue 中实现文本摘要功能通常涉及截取文本的前部分内容并添加省略号。可以通过计算属性、过滤器或自定义指令来实现。 计算属性实现 在 Vue 组件中定义一个计算属性,用于截…

实现vue模版

实现vue模版

Vue 模板的基本结构 Vue 模板基于 HTML 语法,通过 Vue 的指令和插值实现动态渲染。以下是一个基础模板示例: <template> <div id="…

vue element实现

vue element实现

Vue Element UI 实现 Vue Element UI 是基于 Vue.js 的组件库,提供丰富的 UI 组件,适合快速开发企业级中后台产品。以下是一些关键实现方法和步骤: 安装 Vue…

vue实现过程

vue实现过程

Vue 的实现过程 Vue 的实现过程可以分为多个核心模块,包括响应式系统、虚拟 DOM、模板编译等。以下是 Vue 的主要实现过程分析。 响应式系统 Vue 的响应式系统基于 Object.def…

实现 vue 框架

实现 vue 框架

Vue 框架的基本实现 Vue 是一个渐进式 JavaScript 框架,用于构建用户界面。以下是实现 Vue 框架的基本步骤和核心概念。 核心概念 响应式数据绑定 Vue 的核心特性之一是响应式数…