当前位置:首页 > VUE

vue实现表格界面

2026-01-15 22:05:19VUE

Vue 实现表格界面的方法

使用 Element UI 的表格组件

Element UI 提供了功能丰富的表格组件,适合快速开发表格界面。安装 Element UI 后,可以直接使用 el-table 组件。

<template>
  <el-table :data="tableData" style="width: 100%">
    <el-table-column prop="date" label="日期" width="180"></el-table-column>
    <el-table-column prop="name" label="姓名" width="180"></el-table-column>
    <el-table-column prop="address" label="地址"></el-table-column>
  </el-table>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        { date: '2023-01-01', name: '张三', address: '北京市' },
        { date: '2023-01-02', name: '李四', address: '上海市' }
      ]
    }
  }
}
</script>

使用原生 HTML 表格

如果需要更轻量级的实现,可以直接使用 HTML 的 table 标签,结合 Vue 的数据绑定功能。

<template>
  <table>
    <thead>
      <tr>
        <th>日期</th>
        <th>姓名</th>
        <th>地址</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="item in tableData" :key="item.date">
        <td>{{ item.date }}</td>
        <td>{{ item.name }}</td>
        <td>{{ item.address }}</td>
      </tr>
    </tbody>
  </table>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        { date: '2023-01-01', name: '张三', address: '北京市' },
        { date: '2023-01-02', name: '李四', address: '上海市' }
      ]
    }
  }
}
</script>

实现分页功能

表格数据较多时,分页功能是必不可少的。可以结合 el-pagination 组件实现分页。

<template>
  <el-table :data="currentTableData" style="width: 100%">
    <el-table-column prop="date" label="日期" width="180"></el-table-column>
    <el-table-column prop="name" label="姓名" width="180"></el-table-column>
    <el-table-column prop="address" label="地址"></el-table-column>
  </el-table>
  <el-pagination
    @current-change="handleCurrentChange"
    :current-page="currentPage"
    :page-size="pageSize"
    :total="tableData.length"
    layout="prev, pager, next"
  ></el-pagination>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        { date: '2023-01-01', name: '张三', address: '北京市' },
        { date: '2023-01-02', name: '李四', address: '上海市' }
      ],
      currentPage: 1,
      pageSize: 10
    }
  },
  computed: {
    currentTableData() {
      return this.tableData.slice(
        (this.currentPage - 1) * this.pageSize,
        this.currentPage * this.pageSize
      )
    }
  },
  methods: {
    handleCurrentChange(val) {
      this.currentPage = val
    }
  }
}
</script>

实现表格排序

表格排序可以通过 el-tablesortable 属性实现,或者自定义排序逻辑。

<template>
  <el-table :data="tableData" style="width: 100%" @sort-change="handleSortChange">
    <el-table-column prop="date" label="日期" width="180" sortable></el-table-column>
    <el-table-column prop="name" label="姓名" width="180" sortable></el-table-column>
    <el-table-column prop="address" label="地址" sortable></el-table-column>
  </el-table>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        { date: '2023-01-01', name: '张三', address: '北京市' },
        { date: '2023-01-02', name: '李四', address: '上海市' }
      ]
    }
  },
  methods: {
    handleSortChange({ column, prop, order }) {
      this.tableData.sort((a, b) => {
        if (order === 'ascending') {
          return a[prop] > b[prop] ? 1 : -1
        } else {
          return a[prop] < b[prop] ? 1 : -1
        }
      })
    }
  }
}
</script>

实现表格筛选

表格筛选可以通过 el-table-columnfilters 属性实现,或者自定义筛选逻辑。

vue实现表格界面

<template>
  <el-table :data="tableData" style="width: 100%">
    <el-table-column prop="date" label="日期" width="180"></el-table-column>
    <el-table-column prop="name" label="姓名" width="180"></el-table-column>
    <el-table-column
      prop="address"
      label="地址"
      :filters="[{ text: '北京市', value: '北京市' }, { text: '上海市', value: '上海市' }]"
      :filter-method="filterHandler"
    ></el-table-column>
  </el-table>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        { date: '2023-01-01', name: '张三', address: '北京市' },
        { date: '2023-01-02', name: '李四', address: '上海市' }
      ]
    }
  },
  methods: {
    filterHandler(value, row) {
      return row.address === value
    }
  }
}
</script>

以上方法可以根据实际需求选择使用,或者组合使用以实现更复杂的表格功能。

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

相关文章

vue 实现表格单选

vue 实现表格单选

实现表格单选的基本思路 在Vue中实现表格单选功能,通常需要结合表格组件(如el-table)和数据绑定。核心逻辑是通过点击事件更新当前选中行的唯一标识(如id),并通过:class或:style绑定…

uniapp 界面模板

uniapp 界面模板

Uniapp 界面模板推荐 Uniapp 提供了丰富的界面模板和组件库,开发者可以直接使用或基于这些模板进行二次开发。以下是一些常用的界面模板和资源: 官方模板与示例 Uniapp 官方提供了多个示…

vue实现分组表格

vue实现分组表格

Vue 实现分组表格的方法 使用 v-for 嵌套循环实现分组 通过嵌套的 v-for 循环可以轻松实现分组表格。外层循环遍历分组数据,内层循环遍历每组中的具体项。 <template>…

vue表格实现导出

vue表格实现导出

使用 vue-json-excel 插件 安装插件: npm install vue-json-excel --save 在组件中引入并注册: import JsonExcel from 'vue…

vue实现表格透明

vue实现表格透明

实现表格透明的方法 在Vue中实现表格透明效果,可以通过CSS样式调整表格的背景透明度。以下是几种常见的方法: 设置表格背景透明度 通过CSS的background-color属性配合rgba()…

vue实现表格滚动

vue实现表格滚动

Vue 实现表格滚动的方法 使用 CSS 固定表头并滚动内容 通过 CSS 设置表格容器的固定高度和 overflow-y: auto,实现内容区域的滚动。表头部分使用 position: stick…