当前位置:首页 > VUE

vue 实现table组件

2026-01-17 14:43:09VUE

Vue 实现 Table 组件

基础表格结构

使用 Vue 的模板语法构建表格的基本框架,通过 v-for 动态渲染数据。

<template>
  <table>
    <thead>
      <tr>
        <th v-for="column in columns" :key="column.key">{{ column.title }}</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="(row, index) in data" :key="index">
        <td v-for="column in columns" :key="column.key">{{ row[column.key] }}</td>
      </tr>
    </tbody>
  </table>
</template>

动态数据绑定

通过 props 接收外部传入的数据和列配置,确保组件的复用性。

vue 实现table组件

<script>
export default {
  props: {
    data: {
      type: Array,
      default: () => []
    },
    columns: {
      type: Array,
      default: () => []
    }
  }
};
</script>

支持自定义列模板

使用插槽(slot)允许用户自定义单元格内容,增强灵活性。

<template>
  <table>
    <thead>...</thead>
    <tbody>
      <tr v-for="(row, index) in data" :key="index">
        <td v-for="column in columns" :key="column.key">
          <slot :name="`column-${column.key}`" :row="row">
            {{ row[column.key] }}
          </slot>
        </td>
      </tr>
    </tbody>
  </table>
</template>

添加排序功能

通过事件触发排序逻辑,更新数据顺序。

vue 实现table组件

<script>
export default {
  methods: {
    handleSort(column) {
      this.data.sort((a, b) => {
        if (a[column.key] > b[column.key]) return 1;
        if (a[column.key] < b[column.key]) return -1;
        return 0;
      });
    }
  }
};
</script>

样式与交互优化

添加悬停效果和斑马纹样式,提升用户体验。

<style scoped>
table {
  width: 100%;
  border-collapse: collapse;
}
th, td {
  padding: 8px;
  border: 1px solid #ddd;
}
tr:hover {
  background-color: #f5f5f5;
}
tr:nth-child(even) {
  background-color: #f9f9f9;
}
</style>

完整组件示例

整合上述功能,提供一个可直接复用的 Table 组件。

<template>
  <table>
    <thead>
      <tr>
        <th 
          v-for="column in columns" 
          :key="column.key"
          @click="handleSort(column)"
        >
          {{ column.title }}
        </th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="(row, index) in data" :key="index">
        <td v-for="column in columns" :key="column.key">
          <slot :name="`column-${column.key}`" :row="row">
            {{ row[column.key] }}
          </slot>
        </td>
      </tr>
    </tbody>
  </table>
</template>

<script>
export default {
  props: {
    data: Array,
    columns: Array
  },
  methods: {
    handleSort(column) {
      this.$emit('sort', column);
    }
  }
};
</script>

<style scoped>
/* 添加样式 */
</style>

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

相关文章

实现 vue ssr

实现 vue ssr

Vue SSR 实现方法 Vue SSR(Server-Side Rendering)通过服务器端渲染 Vue 应用,提升首屏加载速度和 SEO 友好性。以下是核心实现方法: 基础配置 安装必要依赖…

vue交互实现

vue交互实现

Vue 交互实现方法 Vue.js 提供了多种方式实现用户交互,包括事件处理、表单绑定、动态渲染等。以下是常见的交互实现方法: 事件处理 通过 v-on 或 @ 指令绑定事件,触发方法或直接执行表…

vue实现过滤

vue实现过滤

Vue实现过滤的方法 在Vue中实现数据过滤可以通过多种方式完成,以下是几种常见的方法: 使用计算属性过滤 计算属性是Vue中处理数据过滤的理想选择,它们基于响应式依赖进行缓存,只有在相关依赖发生改…

实现简易vue

实现简易vue

实现简易 Vue 的核心思路 Vue 的核心功能包括数据响应式、模板编译和虚拟 DOM。以下是一个简易实现方案,涵盖核心功能模块。 数据响应式系统 通过 Object.definePropert…

vue实现换行

vue实现换行

Vue 中实现换行的几种方法 在 Vue 中实现换行可以通过多种方式,具体取决于使用场景和需求。以下是常见的几种方法: 使用 HTML 的 <br> 标签 在模板中直接使用 <br…

vue实现表白

vue实现表白

Vue 实现表白页面 使用 Vue 可以快速创建一个动态、交互式的表白页面。以下是一个简单的实现方案: 基础结构 创建一个 Vue 项目或单文件组件,包含以下核心部分: <templat…