当前位置:首页 > 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
分享给朋友:

相关文章

vue实现批量

vue实现批量

Vue 实现批量操作的方法 在 Vue 中实现批量操作通常涉及选择多个项目并执行统一处理,例如批量删除、批量更新等。以下是几种常见实现方式: 表格多选批量操作 使用 el-table 配合复选框实…

vue实现编辑

vue实现编辑

Vue 实现编辑功能 在 Vue 中实现编辑功能通常涉及表单绑定、状态管理和事件处理。以下是一个常见的实现方法: 双向数据绑定 使用 v-model 指令实现表单元素与数据的双向绑定: <…

vue网页实现

vue网页实现

Vue 网页实现指南 环境准备 安装 Node.js 和 npm/yarn,确保环境配置正确。使用 Vue CLI 或 Vite 创建项目框架,快速搭建开发环境。 npm install -g @v…

lua 实现vue

lua 实现vue

在 Lua 中实现类似 Vue 的响应式框架需要模拟 Vue 的核心功能,包括数据绑定、模板渲染和依赖追踪。以下是实现的关键思路和代码示例: 响应式数据绑定 使用 Lua 的 __index 和 _…

vue实现vr

vue实现vr

Vue 实现 VR 的方法 Vue 可以与 WebVR 或 WebXR API 结合使用,通过 Three.js、A-Frame 等库实现 VR 效果。以下是几种常见方法: 使用 A-Frame…

vue递归实现

vue递归实现

vue递归实现方法 在Vue中实现递归组件通常用于树形结构、嵌套菜单等场景。以下是具体实现方式: 使用组件name属性递归 定义一个组件时,通过name属性让组件可以在自身模板中调用自己:…