element-plus表格组件el-table 的使用

表格是在前端页面中是经常被用到的,尤其是管理系统,几乎每个页面都会存在表格,所以掌握表格组件是非常有必要的。element-plus提供el-table,el-table-column来渲染表格,

1. el-table

组件主要属性

属性名作用值类型枚举值默认值
data表格数据对象数组[]
row-key关键属性,用于加速渲染的,类似v-for的key属性function 或string
border是否有边框booleanfalse
stripe是否有斑马线booleanfalse
highlight-current-row高亮选择的行booleanfalse
tree-props子属性以属性结构映射object{ hasChildren: ‘hasChildren’, children: ‘children’ }
lazy懒加载子元素booleanfalse
load展开子元素时加载函数,可远程加载子元素,返回promisefunction

组件主要事件

事件名名作用
select选择行对象数组
sort-change排序function 或string
current-change当前选择的行boolean
stripe是否有斑马线boolean
highlight-current-row高亮选择的行boolean
page-sizes每页条目选择列表数字数组
layout组件布局 ,上下翻页,跳页等布局设置 ,多项逗号分割字符串

1. el-table-column (表格列组件)

组件主要属性

属性名作用值类型枚举值默认值
label表格列的标题string
prop属性名于el-table的data元素的属性名对应string
type类型booleanselection,index,expandfalse
width宽度,单位pxnumber
fixed是否固定列stringleft,rightfalse
sortable高亮选择的行booleanfalse
sort-method指定数据按照哪个属性进行排序,仅当sortable设置为true的时候有效。 应该如同 Array.sort 那样返回一个 Numberfunction
sort-by指定数据按照哪个属性进行排序,仅当 sortable 设置为 true 且没有设置 sort-method 的时候有效。 如果 sort-by 为数组,则先按照第 1 个属性排序,如果第 1 个相等,再按照第 2 个排序,以此类推Function,string,string[]false
sort-orders数据在排序时所使用排序策略的轮转顺序,仅当 sortable 为 true 时有效。 需传入一个数组,随着用户点击表头,该列依次按照数组中元素的顺序进行排序string[]

组件插槽

插槽名作用作用域属性
default自定义列内容row,column,$index
header自定义表头column,$indexcustom才有效

3.用法例子

<script setup lang="ts">
import { aG } from 'vitest/dist/reporters-LqC_WI4d.js';
import { onMounted, reactive, ref, watch } from 'vue'
const pageSizes = ref([10, 20, 50, 100]);

const currentPageSize = ref(10)
const currentPage = ref(1)


class Student {
  id: number
  name: string
  age: number
  address: string
  children: Student[]
  hasChildren: boolean

  constructor(id: number, name: string, age: number, address: string) {
    this.id = id
    this.name = name
    this.age = age
    this.address = address
    this.children = []
    this.hasChildren = false
  }
}


watch(currentPage, (newPage, oldPage) => {
  console.log(newPage, oldPage)
})


const tableData = ref([
  new Student(1, "zhangsanxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", 30, "beijing"),
  new Student(2, "lisi1", 30, "shanghai"),
  new Student(3, "lisi2", 30, "shanghai"),
  new Student(4, "lisi3", 30, "shanghai"),
  new Student(5, "lisi4", 30, "shanghai"),
  new Student(6, "lisi5", 30, "shanghai"),
  new Student(7, "lisi6", 30, "shanghai"),
  new Student(8, "lisi7", 30, "shanghai"),
  new Student(9, "lisi8", 30, "shanghai"),
  {
    id: 10, name: "lisi8", age: 30, address: "shanghai",
    hasChildren: true,
    children: [
      {
        id: 91, name: "wangwu1", age: 30, address: "shenzhen"
      },
      {
        id: 92, name: "wangwu12", age: 30, address: "shenzhen"
      }
    ]
  },
])


const load = (
  row: Student,
  treeNode: unknown,
  resolve: (date: Student[]) => void
) => {
  setTimeout(() => {
    resolve([
      {
        id: 91, name: "wangwu1", age: 30, address: "shenzhen", hasChildren: false, children: []
      },
      {
        id: 92, name: "wangwu12", age: 30, address: "shenzhen", hasChildren: false, children: []
      },
    ])
  }, 1000)
}

const currentRow = ref()

const rowChange = (s: Student) => {
  console.log(s)
  
    //currentRow.value = s
  }


const sortChange = (cloumn: any, prop: string, order: string) => {
  console.log(cloumn, prop, order)
}


const selectChange=(selectionsS:Student[],row:Student)=>{
  console.log(selectionsS,row)
}

const toDo=(scope:any)=>{ 
  console.log(scope.id)
}


</script>


<template>

  <div>
    <el-container>
      <el-header></el-header>
      <el-main>
        <el-table :data="tableData" lazy :load="load" row-key="id" @select="selectChange"
          :tree-props="{ hasChildren: 'hasChildren', children: 'children' }" border stripe height="250" style="width:100%"
          highlight-current-row @sort-change="sortChange" @current-change="rowChange">
          <el-table-column label="id" sortable fixed prop="id" width="100"></el-table-column>
          <el-table-column label="name" sortable="custom" prop="name" width="100" show-overflow-tooltip>
          </el-table-column>
          <el-table-column label="age" prop="age">
          </el-table-column>
          <el-table-column label="address" prop="address">
          </el-table-column>
          <el-table-column fixed="right" label="Operations" min-width="120">
      <template #default="scope">
        <el-button
          link
          type="primary"
          size="small"
          @click.prevent="toDo(scope.row)"
        >
          Remove
        </el-button>
      </template>
    </el-table-column>
        </el-table>
      </el-main>
      <el-footer></el-footer>

    </el-container>


    <el-container>
      <el-header></el-header>
      <el-main>
        <el-table :data="tableData" border stripe height="250" style="width:100%" highlight-current-row @select="selectChange"
          @sort-change="sortChange" @current-change="rowChange">
          <el-table-column type="selection" width="100"></el-table-column>
          <el-table-column label="id" sortable fixed prop="id" width="100"></el-table-column>
          <el-table-column label="name" sortable="custom" prop="name" width="100" show-overflow-tooltip>
          </el-table-column>
          <el-table-column label="age" prop="age">
          </el-table-column>
          <el-table-column label="address" prop="address">
          </el-table-column>
          <el-table-column fixed="right" label="Operations" min-width="120">
            <template #default="scope">
              <el-button type="primary" @click.prevent="toDo(scope.row)">修改</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-main>
      <el-footer></el-footer>

    </el-container>


    <el-pagination :page-sizes="pageSizes" v-model:page-size="currentPageSize" v-model:current-page="currentPage"
      layout="sizes,prev,next,jumper,pager,>" :total="200"></el-pagination>
  </div>



</template>

<style scoped></style>

在这里插入图片描述
https://element-plus.org/zh-CN/component/table.html#%E8%A1%A8%E6%A0%BC%E5%B8%83%E5%B1%80

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值