Element中的表格组件Table和分页组件Pagination

简述:在 Element UI 中,Table组件是一个功能强大的数据展示工具,用于呈现结构化的数据列表。它提供了丰富的特性,使得数据展示不仅美观而且高效。而Pagination组件是一个用于实现数据分页显示的强大工具。它允许用户在大量数据中导航,通过控制每页显示的数据数量和跳转到不同的页面,从而提高数据展示的效率和用户体验。简单记录


一. 超文本标记语言和元素属性

1. 超文本标记语言,HTML

//表格
  <el-table
          @selection-change="handleSelectionChange"
          ref="singleTable"
          :data="tableData"
          :cell-style="{ 'text-align': 'center' }"
          highlight-current-row
          style="width: 100%"
          :header-cell-style="tableHeader"
          :cell-style="tableContent"
          :header-row-style="{ background: '#091e50', color: 'white' }"
          :row-style="{ background: '#091e50', color: 'white' }"
        >

          // 表格内容
          // 全选显示  
          <el-table-column type="selection" width="55"> </el-table-column>
          <el-table-column
            header-align="center"
            min-width="20%"
            label="开始时间"
            property="startTime"
          >
          </el-table-column>

  </el-table>      


//已选项数量提示和分页
  <div class="btm_choseBox">

        <div class="selected_items">
          <div class="tb_checks">
            <el-checkbox
              :indeterminate="isIndeterminate"
              v-model="checkAll"
              @change="handleCheckAllChange"
            >
              已选 {{ multipleSelection.length }} 项
            </el-checkbox>
          </div>
          <el-button @click="deletemultipleSelection"> 删除选中项 </el-button>
        </div>

        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-sizes="[10, 20, 30, 40, 50]"
          layout="total, sizes, prev, pager, next, jumper"
          :current-page="currentPage"
          :page-size="params.pageSize"
          :total="total"
        >
        </el-pagination>

  </div>

2. 元素属性介绍

<!-- 表格组件 -->
  @selection-change="handleSelectionChange"  // 选中项变化时的处理方法
  ref="singleTable"                          // 表格的引用名称
  :data="tableData"                          // 表格数据
  :cell-style="{ 'text-align': 'center' }"   // 单元格样式,文本居中
  highlight-current-row                      // 高亮当前行
  style="width: 100%"                        // 表格宽度
  :header-cell-style="tableHeader"           // 表头单元格样式
  :cell-style="tableContent"                 // 表格内容样式
  // 表头行样式,背景色和文字颜色
  :header-row-style="{ background: '#091e50', color: 'white' }"  
  // 表格行样式,背景色和文字颜色  
  :row-style="{ background: '#091e50', color: 'white' }"  


<!-- 全选显示列 -->
type="selection"


<!-- 表格内容列 -->
header-align="center"      // 表头文本居中
min-width="20%"            // 最小列宽度
label="开始时间"            // 表头标签
property="startTime"       // 对应数据属性


<!-- 已选项数量提示和分页 -->
:indeterminate="isIndeterminate"  // 部分选中状态
v-model="checkAll"                // 双向绑定全选状态
@change="handleCheckAllChange"    // 全选状态变化时的处理方法


<!-- 分页组件 -->
@size-change="handleSizeChange"                     // 页面大小变化时的处理方法
@current-change="handleCurrentChange"               // 当前页变化时的处理方法
:page-sizes="[10, 20, 30, 40, 50]"                  // 可选择的每页显示数量
layout="total, sizes, prev, pager, next, jumper"    // 分页组件布局
:current-page="currentPage"  // 当前页
:page-size="params.pageSize"                        // 每页显示数量
:total="total"                                      // 总数据量


二. 参数属性

// 表格参数
      tableData: [],
      tableLoading: false,


// 已选参数
      multipleSelection: [],
      checkAll: false, // 全选框状态
      isIndeterminate: false, // 全选框的中间状态

     
// 分页参数
      params: { pageNum: 1, pageSize: 10 },
      currentPage: 1,
      total: 0,


三. 函数事件

// 表头样式
tableHeader({ row, column, rowIndex, columnIndex }) {
      if (rowIndex === 0) {
        //第一行
        return `font-weight: bolder;
                background-color:#091e50;
                color:white;
                font-size:20px;`;
      }
},},
// 表格内容样式
tableContent({ row, column, rowIndex, columnIndex }) {
      // if (columnIndex === 1) {
      //   return "color:blue;text-align:center;background-color:#72a4dd;";
      // }
      return `
              color: gray;
              text-align: center; 
              font-size:14px;
              font-weight:600;
              font-family:"宋体";
              `;
},},


// 表格的全选事件
handleSelectionChange(val) {
      this.multipleSelection = val;
      this.checkAll = val.length === this.tableData.length;
      this.isIndeterminate =
        val.length > 0 && val.length < this.tableData.length;
},},


// 删除选中项的方法
deletemultipleSelection() {
  // 获取选中的项的ID数组
  const ArrUuid = this.multipleSelection.map((cur) => {
    return cur.id; // 返回每个选中项的ID
  });
  
  // 显示确认对话框,确认是否永久删除选中项
  this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
    confirmButtonText: "确定", // 确认按钮的文本
    cancelButtonText: "取消", // 取消按钮的文本
    type: "warning", // 提示类型,警告
  })
  .then(() => {
    // 用户确认删除后的操作

    // 开启加载状态
    this.tableLoading = true;
    
    // 调用删除操作的接口方法,传入选中项的ID数组
    delbypassControl(ArrUuid)
      .then((res) => {
        // 接口请求成功后的处理
        console.log(res);
        if (res.code === 200) {
          // 删除成功后显示提示信息
          this.$message({
            message: "删除成功", // 提示信息内容
            type: "success", // 提示类型,成功
            center: true, // 提示信息文字居中
            offset: 400, // 提示信息的垂直位置偏移量
          });
          // 调用刷新数据的方法
          this.callbypassControl();
        }
      })
      .catch(() => {
        // 接口请求失败后的处理
        this.tableLoading = false; // 关闭加载状态
      })
      .finally(() => {
        // 接口请求完成后的处理
        this.tableLoading = false; // 关闭加载状态
      });

  })
  .catch(() => {
    // 用户取消删除操作后的处理

    // 显示取消删除的提示信息
    this.$message({
      type: "info", // 提示类型,信息
      message: "已取消删除", // 提示信息内容
      offset: 400, // 提示信息的垂直位置偏移量
    });

  });
},},


// 分页函数
handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.params.pageSize = val;
      // this.callbypassControl();
},},
handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.params.pageNum = val;
      // this.callbypassControl();
},},


 四. 元素样式

// 父级
position: relative;
padding-bottom: 50px;

// 元素
.btm_choseBox {
  width: 100%;
  height: 50px;
  position: absolute;
  left: 20px;
  bottom: 15px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-sizing: border-box;
}
::v-deep .selected_items {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  .el-checkbox__label {
    margin: 0px 20px 0px 10px;
  }
  label {
    color: white;
  }
  button {
    background-color: rgba(192, 192, 192, 0.479);
    border: none;
    color: white;
  }
}
.el-pagination {
  height: 100%;
  box-sizing: border-box;
  padding-right: 20px;
  text-align: right;
  display: flex;
  justify-content: flex-end;
  align-items: center;
}


五. Pagination分页补充,分页样式(分页全部元素的样式)

// 输入框中的文字颜色
::v-deep input {
  color: white !important;
}
// loading背景色
::v-deep .el-loading-mask {
  background-color: rgba(0, 0, 0, 0.479);
}


// el-pagination分页的背景和文字颜色
::v-deep .el-pagination {
  // 两边文字
  .el-pagination__total {
    color: white !important;
  }
  .el-pagination__jump {
    color: white !important;
  }
  // 两个输入框
  .el-input__inner {
    background-color: #092546;
    border: 1px solid rgba(192, 192, 192, 0.479);
    color: white !important;
  }
  // 左右按键和里面的图标
  button {
    background-color: #092546;
    .el-icon::before {
      color: white;
    }
  }
  // 分页数字
  ul {
    color: white;
    .el-icon::before {
      color: white !important;
    }
  }
  // 分页数字的背景色
  ul > li {
    background-color: #092546;
  }
}
::v-deep .el-loading-mask {
  background-color: rgba(0, 0, 0, 0.479);
}


六. 表格组件Table和分页组件Pagination,更多配置,请看

Element官网icon-default.png?t=N7T8https://element.eleme.cn/#/zh-CN/component/table

  • 13
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
非常感谢您的提问!以下是封装 Element UI Table 组件并带有分页功能的代码: ``` <template> <div> <el-table :data="tableData" :columns="columns" :height="height" :stripe="stripe" :border="border" :show-header="showHeader" :highlight-current-row="highlightCurrentRow" :row-class-name="rowClassName"> <el-table-column v-for="(column, index) in columns" :key="index" :prop="column.prop" :label="column.label" :width="column.width" :min-width="column.minWidth" :fixed="column.fixed" :render-header="column.renderHeader" :sortable="column.sortable" :sort-method="column.sortMethod" :resizable="column.resizable" :formatter="column.formatter" :show-overflow-tooltip="column.showOverflowTooltip" :align="column.align" :header-align="column.headerAlign" :class-name="column.className" :label-class-name="column.labelClassName" :selectable="column.selectable" :reserve-selection="column.reserveSelection" :filters="column.filters" :filter-placement="column.filterPlacement" :filter-multiple="column.filterMultiple" :filter-method="column.filterMethod" :filtered-value="column.filteredValue"> </el-table-column> </el-table> <el-pagination :current-page.sync="currentPage" :page-size="pageSize" :total="total" :page-sizes="pageSizes" :layout="layout" :prev-text="prevText" :next-text="nextText" :disabled="disabled" :hide-on-single-page="hideOnSinglePage" @size-change="handleSizeChange" @current-change="handleCurrentChange"></el-pagination> </div> </template> <script> export default { name: 'MyTable', props: { tableData: { type: Array, required: true }, columns: { type: Array, required: true }, height: { type: [Number, String], default: '' }, stripe: { type: Boolean, default: true }, border: { type: Boolean, default: true }, showHeader: { type: Boolean, default: true }, highlightCurrentRow: { type: Boolean, default: false }, rowClassName: { type: Function, default: () => '' }, currentPage: { type: Number, default: 1 }, pageSize: { type: Number, default: 10 }, total: { type: Number, default: 0 }, pageSizes: { type: Array, default: () => [10, 20, 30, 40, 50, 100] }, layout: { type: String, default: 'total, sizes, prev, pager, next, jumper' }, prevText: { type: String, default: '上一页' }, nextText: { type: String, default: '下一页' }, disabled: { type: Boolean, default: false }, hideOnSinglePage: { type: Boolean, default: false } }, methods: { handleSizeChange(val) { this.$emit('size-change', val) }, handleCurrentChange(val) { this.$emit('current-change', val) } } } </script> ``` 希望这个代码对您有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北城笑笑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值