当前位置:   article > 正文

【Element】表格 el-table

el-table

目录

ElementUI

表格分页(每页20条)

表格分页(全部数据)

表格排序(全部数据)

表格排序(默认)

两个el-table冲突

加载数据前显示“ 暂无数据 ”

表格项为路由

表头样式

树形表格

点击表格单行

人数统计表(多级表头、合并行或列)


ElementUI

表格分页(每页20条)

  1. <el-table :data="tableData" border style="width: 100%" @selection-change="handleSelectionChange" tooltip-effect="dark">
  2. <!-- 复选框 -->
  3. <el-table-column type="selection"> </el-table-column>
  4. <!-- 序号 -->
  5. <el-table-column type="index" label="序号" width="70"></el-table-column>
  6. <!-- 名称很长显示悬浮窗 -->
  7. <el-table-column prop="name" label="名称" show-overflow-tooltip></el-table-column>
  8. <!-- 自定义参数类型 -->
  9. <el-table-column label="类型">
  10. <template slot-scope="scope">
  11. <div v-if="scope.row.type === 1">小程序</div>
  12. <div v-else-if="scope.row.type === 2">后台</div>
  13. <div v-else>全部</div>
  14. </template>
  15. </el-table-column>
  16. <!-- 操作 -->
  17. <el-table-column label="操作" width="220">
  18. <template slot-scope="scope">
  19. <el-button size="small" @click="edit(scope.row,scope.$index)" type="primary">编辑</el-button>
  20. <el-button size="small" @click="remove(scope.row,scope.$index)" type="danger">删除</el-button>
  21. </template>
  22. </el-table-column>
  23. </el-table>
  24. <!-- 分页 -->
  25. <div class="pagination">
  26. <el-pagination
  27. background
  28. layout="total,prev,pager,next,sizes,jumper"
  29. :total="total"
  30. :page-sizes="[10, 20, 30, 50]"
  31. :page-size="pageSize"
  32. :current-page.sync="pageNum"
  33. @size-change="handleSizeChange"
  34. @current-change="handleCurrentChange"
  35. >
  36. </el-pagination>
  37. </div>

表格单元格内容过多,使用文字提示。超出一行省略号。悬浮展示所有内容。

show-overflow-tooltip

tooltip-effect="dark"

 操作的时候获取当前行和当前行的下标

scope.row

scope.$index

  1. data() {
  2. return {
  3. // 复选框所选中信息
  4. selection: [],
  5. selectStr: "",
  6. // 表格数据
  7. tableData: [],
  8. // 总条数
  9. total: 0,
  10. // 页面条数
  11. pageSize: 20,
  12. // 页码
  13. pageNum: 1,
  14. };
  15. },
  1. created() {
  2. this.getList();
  3. },
  4. // 获取列表数据
  5. async getList(pageNum, pageSize) {
  6. const res = await getIntegralRecord({
  7. pageNum,
  8. pageSize,
  9. });
  10. this.tableData = res.data.list;
  11. this.total = res.data.total;
  12. },
  13. // 数量回调
  14. handleSizeChange(e) {
  15. this.pageSize = e;
  16. this.getList();
  17. },
  18. // 页码回调
  19. handleCurrentChange(e) {
  20. this.pageNum = e;
  21. this.getList();
  22. },
  23. // 复选框回调
  24. handleSelectionChange(val) {
  25. this.selection = val;
  26. this.selectStr = val.map(function (e) {return e.id}).join(",");
  27. },

表格分页(全部数据)

每页5条

每页10条

  1. <el-table
  2. v-loading="loading"
  3. :data="list.slice((pageNum-1)*pageSize,pageNum*pageSize)"
  4. style="width: 100%;"
  5. >
  6. <el-table-column label="序号" type="index" align="center">
  7. <template slot-scope="scope">
  8. <span>{{(pageNum - 1) * pageSize + scope.$index + 1}}</span>
  9. </template>
  10. </el-table-column>
  11. <el-table-column label="编号" align="center" prop="id" />
  12. </el-table>
  13. <el-pagination
  14. :current-page="pageNum"
  15. :page-size="pageSize"
  16. :page-sizes="[5, 10, 20, 30, 50]"
  17. :pager-count="5"
  18. :total="total"
  19. layout="total, sizes, prev, pager, next, jumper"
  20. @size-change="handleSizeChange"
  21. @current-change="handleCurrentChange"
  22. />
  1. data() {
  2. return {
  3. loading: true,
  4. total: 0,
  5. pageNum: 1,
  6. pageSize: 10,
  7. list: []
  8. };
  9. },
  1. created() {
  2. this.getList();
  3. },
  4. methods: {
  5. handleSizeChange(val) {
  6. this.pageSize = val
  7. if (this.pageNum * val > this.total) {
  8. this.pageNum = 1
  9. }
  10. this.getList()
  11. },
  12. handleCurrentChange(val) {
  13. this.pageNum = val
  14. this.getList()
  15. },
  16. getList() {
  17. this.loading = true;
  18. list({}).then(response => {
  19. this.list = [
  20. {id: 1},
  21. {id: 2},
  22. {id: 3},
  23. {id: 4},
  24. {id: 5},
  25. {id: 6},
  26. {id: 7},
  27. {id: 8},
  28. {id: 9},
  29. {id: 10},
  30. {id: 11},
  31. ];
  32. this.total = 11;
  33. this.loading = false;
  34. });
  35. },
  36. }

表格排序(全部数据)

单项排序

@sort-change="sortChange"

sortable='custom'

  1. <el-table
  2. v-loading="loading"
  3. :data="list.slice((pageNum-1)*pageSize,pageNum*pageSize)"
  4. style="width: 100%;"
  5. @sort-change="sortChange"
  6. >
  7. <el-table-column label="序号" type="index" align="center">
  8. <template slot-scope="scope">
  9. <span>{{(pageNum - 1) * pageSize + scope.$index + 1}}</span>
  10. </template>
  11. </el-table-column>
  12. <el-table-column label="编号" align="center" prop="id" sortable='custom' />
  13. </el-table>
  1. data() {
  2. return {
  3. list: [],
  4. };
  5. },
  1. created() {
  2. this.getList();
  3. },
  4. methods: {
  5. sortChange(column){
  6. this.pageNum = 1
  7. if(column.order === 'ascending'){ // 降序
  8. this.list.sort(this.ascSortFun)
  9. }else if(column.order === 'descending'){ // 升序
  10. this.list.sort(this.desSortFun)
  11. }else{
  12. this.getList()
  13. }
  14. },
  15. //升序
  16. ascSortFun(a, b) {
  17. if (a.id > b.id) return 1;
  18. if (a.id == b.id) return 0;
  19. if (a.id < b.id) return -1;
  20. },
  21. //降序
  22. desSortFun(a,b){
  23. if (a.id > b.id) return -1;
  24. if (a.id == b.id) return 0;
  25. if (a.id < b.id) return 1;
  26. },
  27. getList() {
  28. this.loading = true;
  29. list({}).then(response => {
  30. this.list = [
  31. {id: 1},
  32. {id: 2},
  33. {id: 3},
  34. {id: 4},
  35. {id: 5},
  36. {id: 6},
  37. {id: 7},
  38. {id: 8},
  39. {id: 9},
  40. {id: 10},
  41. {id: 11},
  42. ];
  43. this.total = 11;
  44. this.loading = false;
  45. });
  46. },
  47. }

多项排序

  1. <el-table
  2. v-loading="loading"
  3. :data="list.slice((pageNum-1)*pageSize,pageNum*pageSize)"
  4. style="width: 100%;"
  5. @sort-change="sortChange"
  6. >
  7. <el-table-column label="序号" type="index" align="center">
  8. <template slot-scope="scope">
  9. <span>{{(pageNum - 1) * pageSize + scope.$index + 1}}</span>
  10. </template>
  11. </el-table-column>
  12. <el-table-column label="编号" align="center" prop="id" sortable='custom' />
  13. <el-table-column label="时间" align="center" prop="time" sortable='custom' />
  14. </el-table>
  1. data() {
  2. return {
  3. prop: "",
  4. list: [],
  5. };
  6. },
  1. created() {
  2. this.getList();
  3. },
  4. methods: {
  5. sortChange(column){
  6. this.pageNum = 1
  7. this.prop = column.prop
  8. if(column.order === 'ascending'){ // 降序
  9. this.list.sort(this.ascSortFun)
  10. }else if(column.order === 'descending'){ // 升序
  11. this.list.sort(this.desSortFun)
  12. }else{
  13. this.getList()
  14. }
  15. },
  16. //升序
  17. ascSortFun(a, b) {
  18. if (a[this.prop] > b[this.prop]) return 1;
  19. if (a[this.prop] == b[this.prop]) return 0;
  20. if (a[this.prop] < b[this.prop]) return -1;
  21. },
  22. //降序
  23. desSortFun(a,b){
  24. if (a[this.prop] > b[this.prop]) return -1;
  25. if (a[this.prop] == b[this.prop]) return 0;
  26. if (a[this.prop] < b[this.prop]) return 1;
  27. },
  28. getList() {
  29. this.loading = true;
  30. list({}).then(response => {
  31. this.list = [
  32. {id: 1,time: '11-02'},
  33. {id: 2,time: '11-03'},
  34. {id: 3,time: '11-05'},
  35. {id: 4,time: '11-04'},
  36. {id: 5,time: '11-02'},
  37. {id: 6,time: '11-04'},
  38. {id: 7,time: '11-03'},
  39. {id: 8,time: '11-04'},
  40. {id: 9,time: '11-11'},
  41. {id: 10,time: '11-03'},
  42. {id: 11,time: '11-01'},
  43. ];
  44. this.total = 11;
  45. this.loading = false;
  46. });
  47. },
  48. }

表格排序(默认)

  1. <el-table
  2. ref="table"
  3. v-loading="loading"
  4. :data="list.slice((pageNum-1)*pageSize,pageNum*pageSize)"
  5. style="width: 100%;"
  6. :default-sort="defaultSort"
  7. @sort-change="sortChange"
  8. >
  9. <el-table-column label="序号" type="index" align="center">
  10. <template slot-scope="scope">
  11. <span>{{(pageNum - 1) * pageSize + scope.$index + 1}}</span>
  12. </template>
  13. </el-table-column>
  14. <el-table-column label="编号" align="center" prop="id" sortable='custom' />
  15. <el-table-column label="时间" align="center" prop="time" sortable='custom' />
  16. </el-table>
  1. data() {
  2. return {
  3. defaultSort: {prop: 'time', order: 'descending'},
  4. list: [],
  5. };
  6. },

 重置成默认值

  1. reset() {
  2. this.$refs.table.sort(this.defaultSort.prop, this.defaultSort.order)
  3. },

两个el-table冲突

设置两个不同的key就解决问题啦

  1. <el-table :data="tableData" key="1"></el-table>
  2. <el-table :data="tableData2" key="2"></el-table>

加载数据前显示“ 暂无数据 ”

  1. <el-table :data="tableData" style="width: 100%">
  2. <template slot="empty">
  3. <p>{{empty}}</p>
  4. </template>
  5. </el-table>
  1. data: {
  2. return: {
  3. tableData: [],
  4. text: ''
  5. }
  6. }
  1. created() {
  2. getTableData({}).then(res => {
  3. if(res.code == 200) {
  4. this.tableData = res.data
  5. this.text = this.tableData.length > 0 ? '暂无数据' : ''
  6. }
  7. });
  8. },

表格项为路由

  1. <el-table-column label="字典类型" align="center" :show-overflow-tooltip="true">
  2. <template slot-scope="scope">
  3. <router-link :to="'/system/dict-data/index/' + scope.row.dictId" class="link-type">
  4. <span>{{ scope.row.dictType }}</span>
  5. </router-link>
  6. </template>
  7. </el-table-column>

表头样式

class和style的两种写法

  1. <el-table
  2. :data="tableData"
  3. header-cell-class-name="tableStyle"
  4. :header-cell-class-name="headerStyle"
  5. :header-cell-style='tableHeaderStyle'
  6. :header-cell-style="{
  7. 'background-color': '#1989fa',
  8. 'color': '#fff',
  9. 'font-weight': '400'
  10. }"
  11. ></el-table>
  1. headerStyle ({row, column, rowIndex, columnIndex}) {
  2. return 'tableStyle'
  3. },
  4. tableHeaderStyle ({row, column, rowIndex, columnIndex}) {
  5. return 'background-color:#1989fa;color:#fff;font-weight:400;'
  6. }

树形表格

  1. <el-button @click="toggleExpandAll">展开/折叠</el-button>
  2. <el-table
  3. v-if="refreshTable"
  4. :data="list"
  5. row-key="id"
  6. :default-expand-all="isExpandAll"
  7. :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
  8. >
  9. <el-table-column prop="name" label="名称"></el-table-column>
  10. </el-table>
  1. data() {
  2. return {
  3. refreshTable: true,
  4. isExpandAll: true,
  5. list:[
  6. {
  7. id: 1,
  8. name: '1',
  9. children: [
  10. {
  11. id: 11,
  12. name: '11',
  13. children: [
  14. {
  15. id: 111,
  16. name: '111',
  17. }
  18. ]
  19. },
  20. {
  21. id: 12,
  22. name: '12',
  23. },
  24. ]
  25. },
  26. {
  27. id: 2,
  28. name: '2',
  29. children: [
  30. {
  31. id: 21,
  32. name: '21'
  33. },
  34. {
  35. id: 22,
  36. name: '22'
  37. },
  38. ]
  39. }
  40. ]
  41. };
  42. },

展开折叠操作

  1. toggleExpandAll() {
  2. this.refreshTable = false;
  3. this.isExpandAll = !this.isExpandAll;
  4. this.$nextTick(() => {
  5. this.refreshTable = true;
  6. });
  7. },

点击表格单行

row-key:行数据的Key

  1. <el-table
  2. ref="table"
  3. v-loading="loading"
  4. :data="tableData"
  5. :row-key="getRowKey"
  6. @row-click="clickRow"
  7. @selection-change="handleSelectionChange"
  8. >
  9. </el-table>
  1. // 单击选中行数据
  2. clickRow(row) {
  3. this.$refs.table.toggleRowSelection(row);
  4. },
  5. // 多选框选中数据
  6. handleSelectionChange(selection) {
  7. this.ids = selection.map((item) => item.id);
  8. },
  9. // 保存选中的数据编号
  10. getRowKey(row) {
  11. return row.id;
  12. },

选中多页数据

默认情况下,

全选第一页

切换到第二页,选中的值被清空了。 

  1. <el-table v-loading="loading" :data="managerList" @selection-change="handleSelectionChange">
  2. <el-table-column type="selection" width="55" align="center" fixed="left"/>
  3. <el-table-column label="名称" align="center" prop="deviceName">
  4. <template slot-scope="scope">名称</template>
  5. </el-table-column>
  6. </el-table>
  1. handleSelectionChange(selection) {
  2. console.log(selection)
  3. },

提出解决方法,让第二页的数据可以选中。

  1. <el-table v-loading="loading" :data="managerList" @selection-change="handleSelectionChange" :row-key="getRowKeys">
  2. <el-table-column type="selection" width="55" align="center" fixed="left" :reserve-selection="true"/>
  3. <el-table-column label="名称" align="center" prop="deviceName">
  4. <template slot-scope="scope">名称</template>
  5. </el-table-column>
  6. </el-table>
  1. getRowKeys(row) {
  2. return row.id
  3. },
  4. handleSelectionChange(selection) {
  5. console.log(selection)
  6. },

人数统计表(多级表头、合并行或列)

  1. <el-table
  2. :data="school_table"
  3. style="width: 100%;margin-top: 20px;"
  4. :span-method="spanMethod"
  5. >
  6. <el-table-column
  7. align="center"
  8. prop="name"
  9. label="名称"
  10. width="150">
  11. </el-table-column>
  12. <el-table-column align="center" :label="item.name" v-for="(item,index) in class_list" :key="index">
  13. <el-table-column
  14. align="center"
  15. label="男"
  16. width="120">
  17. <template slot-scope="scope">
  18. <div v-for="(v,k) in scope.row.data" :key="k" v-show="v.classId == item.id">
  19. <el-input-number style="width: 100%;" v-model="v.man" :controls="false"></el-input-number>
  20. </div>
  21. </template>
  22. </el-table-column>
  23. <el-table-column
  24. align="center"
  25. prop="name"
  26. label="女"
  27. width="120">
  28. <template slot-scope="scope">
  29. <div v-for="(v,k) in scope.row.data" :key="k" v-show="v.classId == item.id">
  30. <el-input-number style="width: 100%;" v-model="v.woman" :controls="false"></el-input-number>
  31. </div>
  32. </template>
  33. </el-table-column>
  34. </el-table-column>
  35. <el-table-column
  36. align="center"
  37. prop="total"
  38. label="合计">
  39. </el-table-column>
  40. </el-table>
  1. school_table: [
  2. {
  3. id: 1,
  4. name: '1小学',
  5. total: 1,
  6. data:[
  7. {classId:1, man:1, woman:2},
  8. {classId:2, man:3, woman:4},
  9. ],
  10. },
  11. {
  12. id: 2,
  13. name: '2小学',
  14. data:[
  15. {classId:1, man:7, woman:8},
  16. {classId:2, man:9, woman:10},
  17. ],
  18. }
  19. ],
  20. school_list: [
  21. {
  22. id: 1,
  23. name:'1小学'
  24. },
  25. {
  26. id: 2,
  27. name:'2小学'
  28. },
  29. ],
  30. class_list:[
  31. {
  32. id: 1,
  33. name:'一年级'
  34. },
  35. {
  36. id: 2,
  37. name:'二年级'
  38. }
  39. ]

文字提示超出屏幕(show-overflow-tootip )

解决方法

  1. <style>
  2. .el-tooltip__popper {
  3. width: 250px;
  4. }
  5. </style>

弹出框(el-popover)

  1. <el-table ref="dataTable" v-loading="loading" :data="managerList" @selection-change="handleSelectionChange">
  2. <el-table-column type="selection" width="55" align="center"/>
  3. <el-table-column label="名称" align="center" prop="createTime">
  4. <template slot-scope="scope">
  5. <el-popover
  6. placement="top-end"
  7. width="100"
  8. trigger="hover"
  9. >
  10. 哈哈哈哈啊哈哈哈哈啊哈哈哈哈啊哈哈哈
  11. <span slot="reference">名称</span>
  12. </el-popover>
  13. </template>
  14. </el-table-column>
  15. </el-table>

复选框偏移,有小黑点

当宽度小于30的时候会出现向左偏移问题

  1. <el-table-column
  2. type="selection"
  3. width="25">
  4. </el-table-column>

复选框禁用

  1. <el-table v-loading="loading" :data="managerList" @selection-change="handleSelectionChange">
  2. <el-table-column type="selection" width="55" align="center" :selectable="selectMethod"/>
  3. <el-table-column label="名称" align="center" prop="createTime"></el-table-column>
  4. </el-table>
  1. selectMethod(row) {
  2. console.log(row)
  3. return row.createTime == '2023-08-17 15:33:27' // 创建时间为 2023-08-17 15:33:27 可以被选
  4. },

 可以选中的规则

:selectable="selectMethod"

复选框默认选中

  1. <el-table ref="dataTable" :data="managerList" @selection-change="handleSelectionChange">
  2. <el-table-column type="selection" width="55" align="center"/>
  3. <el-table-column label="名称" align="center" prop="createTime"></el-table-column>
  4. </el-table>
  1. getList() {
  2. this.loading = true
  3. getTableList().then(response => {
  4. this.list = response.rows
  5. this.list.forEach(element => {
  6. this.$nextTick(() => {
  7. if (element.createTime == '2023-08-17 15:33:27') {
  8. this.$refs.dataTable.toggleRowSelection(element, true)
  9. }
  10. })
  11. });
  12. this.total = response.total
  13. this.loading = false
  14. });
  15. },

设置选中项 

this.$refs.dataTable.toggleRowSelection(element, true)

动态添加列 

新增之后

  1. <el-button type="primary" @click="addCol"> 新增列</el-button>
  2. <el-table :data="list">
  3. <el-table-column prop="paramName" :label="item" v-for="(item,index) in date" :key="index">
  4. <template slot-scope="scope">{{ scope.row[item] }}</template>
  5. </el-table-column>
  6. </el-table>
  1. list: [
  2. {
  3. '年': '2023',
  4. '月': '01',
  5. '日': '01',
  6. '时': '01',
  7. '分': '01',
  8. '秒': '01'
  9. },
  10. {
  11. '年': '2023',
  12. '月': '02',
  13. '日': '02',
  14. '时': '02',
  15. '分': '02',
  16. '秒': '02'
  17. }
  18. ],
  19. date: ['年', '月', '日']
  1. addCol() {
  2. this.date.push('时')
  3. },

动态添加列导致表格闪烁

方法一:官方提供的方法

新增列后,重新计算单元格高度和宽度渲染到页面,从而发生闪烁。

  1. beforeUpdate(){
  2. this.$nextTick(() => { //在数据加载完,重新渲染表格
  3. this.$refs['table'].doLayout();
  4. })
  5. }

方法二:设置独一无二的key

  1. <el-button type="primary" @click="addCol"> 新增列</el-button>
  2. <el-table :data="list" :key="date">
  3. <el-table-column prop="paramName" :label="item" v-for="(item,index) in date" :key="index">
  4. <template slot-scope="scope">{{ scope.row[item] }}</template>
  5. </el-table-column>
  6. </el-table>
  1. list: [
  2. {
  3. '年': '2023',
  4. '月': '01',
  5. '日': '01',
  6. '时': '01',
  7. '分': '01',
  8. '秒': '01'
  9. },
  10. {
  11. '年': '2023',
  12. '月': '02',
  13. '日': '02',
  14. '时': '02',
  15. '分': '02',
  16. '秒': '02'
  17. }
  18. ],
  19. date: ['年', '月', '日']

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/426360
推荐阅读
相关标签
  

闽ICP备14008679号