Vue 的Element UI 封装 el-table和 el-agination 结合组件

Vue 的Element UI 封装 el-table和 el-pagination 结合组件

  • 在后台管理系统中,经常会用到table和pagination 分页结合使用。封装个组件让其试用于所有的表格页面。
  • 思路:
  1. 确定要封装的内容,从父组件传给子组件的内容和从子组件传给父组件的内容,
  2. 熟悉父子组件间传值方法
  • 子组件:
<template>
  <div>
    <div class="all_table">
      <el-table :data="tableData" :border="objcontent.border" :stripe="objcontent.stripe" v-loading="objcontent.loading" @selection-change="handleSelectionChange">
        <el-table-column v-if="objcontent.changeselect?objcontent.changeselect:false" type="selection" style="width: 50px;">
        </el-table-column>
        <el-table-column v-if="objcontent.number?objcontent.number:false" type="index" style="width: 50px;">
        </el-table-column>
        <el-table-column v-for="(item,index) in tableColumns" :key="index" :prop="item.prop" :align="item.align" :minWidth="item.minWidth" :label="item.label">
          <template slot-scope="scope">
            <slot :name='index' :row="scope.row">{{scope.row[item.prop]}}</slot>
          </template>
        </el-table-column>
        <el-table-column :min-width="operations.width" :fixed="operations.fixed" :label="operations.label" :align="operations.align" v-if="operations.list.length > 0">
          <template slot-scope="scope">
            <div class="operations">
              <template v-for="(item, key) in operations.list">
                <el-button :type="item.type?item.type:'text'" size="mini" :icon="item.icon" :disabled="item.disabled" @click="item.method(key,scope.row)">{{item.label}}
                </el-button>
              </template>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" style="text-align:center;margin-top:20px;" :current-page="pageIndex" :page-sizes="pageSizes" :page-size="pageSize" :layout="pager_layout" :total="total">
    </el-pagination>
  </div>
</template>
<script>
export default {
  components: {},
  data() {
    return {
      multipleSelection: [],
    };
  },
  methods: {
    // 切换每页显示的数量
    handleSizeChange(val) {
      console.log('每页显示个数', val)
      // this.$emit('update:pageSize',val)
      this.$emit('handleSizeChange', val)
    },
    // 切换页码
    handleCurrentChange(val) {
      console.log('显示第几页', val)
      // this.$emit('update:pageIndex',val)
      this.$emit('handleCurrentChange', val)
    },
    //选择多条
    handleSelectionChange(val) {
      this.multipleSelection = val
      this.$emit('handleSelectionChange', val)
    },
  },
  created() { },
  mounted() { },
  computed: {},
  watch: {
  },
  props: {
    tableData: {
      type: Array,
      required: true
    }, //table
    total: {
      type: Number,
      required: true
    }, //总条数
    pager_layout: {
      type: String,
      default: 'total,sizes,prev, pager, next, jumper',
    }, //组件布局
    pageSizes: {
      type: Array,
      default: () => [10, 20, 30, 40]
    },//每页显示个数选择器
    pageSize: {
      type: Number,
      default: 10,
      required: true
    },//每页显示个数
    pageIndex: {
      type: Number,
      default: 1,
      required: true
    },//当前页码
    tableColumns: {
      type: Array,
      default: () => [],
      required: true
    },//每列标题名字
    objcontent: {
      type: Object,
      default: {
        border: true,//是否带有纵向边框
        stripe: true, // 是否为斑马纹 table
        loading: true, // 是否添加表格loading加载动画
        changeselect: false,//是否有多选项列
        number: true,//是否有序号
      }
    },//一些表格样式
    operations: {
      type: Object,
      default() {
        return {}
      },//操作按钮
    },
  }
}
</script>
<style scoped>
</style>
  • 父组件
<!--
*创建人:
*创建时间:
*说明:设备列表
*修改人:
*修改时间:
*说明:
-->

<template>
  <div>
     <!-- :pageIndex.sync="formData.pageIndex" -->
     <!-- :pageSize.sync='formData.pageSize' -->
    <equimentcomponent 
    @handleSizeChange="handleSizeChange"
    @handleCurrentChange="handleCurrentChange"
    @handleSelectionChange="handleSelectionChange" 
     :operations="operations" 
     :objcontent="objcontent" 
     :tableData="tableData" 
     :total='total' 
     :pageIndex="formData.pageIndex"
     :pageSizes='pageSizes' 
     :pageSize='formData.pageSize'
     :pager_layout='pager_layout' 
     :tableColumns='tableColumns'>
       <div slot="0" slot-scope="scope">
         {{scope.row.stationName}}
       </div>
       <div slot="2" slot-scope="scope">
         {{scope.row.deviceTs}}
       </div>
     </equimentcomponent>
    <!-- 删除模态框 -->
    <el-dialog title="是否删除客户" :visible.sync="dialogModalDel" width="38%">
      <div style='text-align:center;height:60px;line-height:60px'>
        <div class='dialog_img_one'>
          <img class='img' src="../../../assets/img/pointcustomer/shanchu_wenhao.png" alt="">
          <span class='dialog_p'>您是否确认删除该设备信息,删除后信息将无法找回?</span>
        </div>
      </div>
      <span slot="footer">
        <el-button @click="dialogModalDel = false">取 消</el-button>
        <el-button type="primary" @click="gotoDel">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import equimentcomponent from './equimentcomponent'
export default {
  name: "equipmentlist",
  data() {
    return {
      dialogModalDel: false, //删除模态框标识
      formData: {
        deviceId: undefined,
        deviceName: "",
        stationId: undefined,
        deviceClassVal: "",
        pageSize: 10,
        pageIndex: 1
      }, //搜索表单
      tableData: [],
      tableColumns: [
        {
          prop: "stationName",
          align: "left",
          label: "设备安装位置",
          minWidth: 200,
          slot:true,
        },
        {
          prop: "deviceClassVal",
          align: "left",
          label: "设备类型",
          minWidth: 140,
          formatter: (row, column, cellValue) => {
            console.log(',,,,',row, column, cellValue)
              return `<span style="white-space: nowrap;color: red;">${row.deviceClassVal}</span>`
            }
        },
        {
          prop: "deviceTs",
          align: "left",
          label: "型号",
          minWidth: 120
        },
        {
          prop: "price",
          align: "left",
          label: "金额",
          minWidth: 80
        },
        {
          prop: "ascription",
          align: "left",
          label: "设备归属",
          minWidth: 80,
          render: (h, params) => {
            console.log(h, params)
              return h('el-tag', {
                props: {type: params.row.ascription === '公司购置' ? 'success' : params.row.ascription === '客户自有' ? 'info' : 'danger'} // 组件的props
              }, params.row.ascription === '客户自有' ? '客户自有' : '客户自有')
            }
        }
      ],
      pageSizes: [10, 20, 30, 40],
      objcontent: {
        border: true,//是否带有纵向边框
        stripe: true, // 是否为斑马纹 table
        loading: false, // 是否loading(在加载数据时记得判断是否开启)
        changeselect: true,//是否有前面的多选项
        number: true,//是否有序号
      },
      operations: {
        width: 160,
        fixed: 'right',
        label:"操作",
        align:"center",
        list: [
            {
              label: '编辑',
              type: 'text',
              icon: 'el-icon-edit',
              disabled: false,
              method: (index, row) => {
                this.editChange(index, row)
              }
            },
            {
              label: '迁移记录',
              type: 'text',
              icon: 'el-icon-edit',
              disabled: false,
              method: (index, row) => {
                this.historyChange(index, row)
              }
            },
            {
              label: '删除',
              type: 'text',
              icon: 'el-icon-delete',
              disabled: false,
              method: (index, row) => {
                this.delChange(index, row)
              }
            }
          ]
       },//操作按钮
      export_excle_flag: false,//
      total: 0, //总条数
      pager_layout: "total,sizes,prev, pager, next, jumper",//组件布局
      equipmentId: undefined //设备id
    };
  },
computed: { },
created() {
  this.initgetinfocountes();
  this.initquerylistes();
  this.initallcountes();
  this.initoperationslnp();
  this.initoperationsdtpe();
},
activated() { },
mounted() {
},
destroyed() { },
methods: {
  //点击迁移记录
  historyChange(row) {
    this.$router.push({
      name: "migrationhistory",
      params: {
        equipmentrow: row
      }
    });
  },
  //点击编辑
  editChange(row) {
    this.$router.push({
      name: "equipmentedit",
      params: {
        equipmentrow: row
      }
    });
  },
  //点击删除
  delChange(index,row) {
    console.log(index,row)
    this.dialogModalDel = true;
    this.equipmentId = row.id;
  },
  //删除模态框点击确定
  gotoDel() {
    console.log(this.equipmentId)
    delinfobyids.post({ id: this.equipmentId }, this).then(res => {
      if (res !== null) {
        this.$message({
          message: "删除成功",
          type: "success"
        });
        this.dialogModalDel = false;
        this.initquerylistes();
        this.initallcountes();
        this.initgetinfocountes();
      } else {
        this.$message({
          message: "删除失败",
          type: "error"
        });
      }
    });
  },
  // 分页
  handleSizeChange(val) {
    this.formData.pageSize = val;
    this.initquerylistes();
    this.initallcountes();
    this.initgetinfocountes();
  },
  handleCurrentChange(val) {
    this.formData.pageIndex = val;
    this.initquerylistes();
    this.initallcountes();
    this.initgetinfocountes();
  },
  //多选项
   handleSelectionChange (val) {
    console.log('选中的', val)
  },
  //根据检索条件查询客户信息列表
  initquerylistes() {
    let input = this._.cloneDeep(this.formData);
    querylistes.post(input, this).then(res => {
      if (res.code == 200) {
        if (res.data) {
          res.data.map(item => {
            item.stationName = item.stationName ? item.stationName : "库存"
          })
          this.tableData = res.data;
        } else {
          this.tableData = [];
        }
      }
    });
  },
  //根据检索条件查询客户信息列表计数
  initallcountes() {
    let input = this._.cloneDeep(this.formData);
    allcountes.post(input, this).then(res => {
      if (res !== null) {
        this.total = res;
      }
    });
  },
},
watch: { },
components: { equimentcomponent }
};
</script>

<style scoped>
.cursor:hover {
  color: #409eff;
  cursor: pointer;
}
</style>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Element-UI 是一套基于 Vue.js 2.0 的 UI 组件库,提供了丰富的组件和模板,方便我们快速开发页面。其中,Table 组件是常用的数据展示组件之一,我们可以通过封装一个 TableList 组件来简化 Table 的使用。 以下是一个简单的 TableList 封装示例: ```vue <template> <div> <el-table :data="tableData" :height="height" :max-height="maxHeight" :stripe="stripe" :border="border" :default-sort="defaultSort" :show-header="showHeader" :highlight-current-row="highlightCurrentRow"> <el-table-column v-for="(column, index) in columns" :key="index" :label="column.label" :prop="column.prop" :width="column.width" :min-width="column.minWidth" :fixed="column.fixed" :resizable="column.resizable" :formatter="column.formatter" :show-overflow-tooltip="column.showOverflowTooltip"></el-table-column> </el-table> </div> </template> <script> export default { name: "TableList", props: { tableData: { type: Array, default: () => [] }, columns: { type: Array, default: () => [] }, height: { type: String, default: "" }, maxHeight: { type: String, default: "" }, stripe: { type: Boolean, default: true }, border: { type: Boolean, default: true }, defaultSort: { type: Object, default: () => {} }, showHeader: { type: Boolean, default: true }, highlightCurrentRow: { type: Boolean, default: true } } }; </script> ``` 在这个组件中,我们定义了以下 props: - `tableData`:表格数据,类型为 Array。 - `columns`:表格列的配置项,类型为 Array。每个配置项包含 `label`(列名)、`prop`(对应数据源的字段名)、`width`(列宽度)等属性。 - `height`:表格高度,类型为 String。 - `maxHeight`:表格最大高度,类型为 String。 - `stripe`:是否显示斑马纹,类型为 Boolean。 - `border`:是否显示边框,类型为 Boolean。 - `defaultSort`:默认排序规则,类型为 Object,包含 `prop`(排序字段名)和 `order`(排序方式)两个属性。 - `showHeader`:是否显示表头,类型为 Boolean。 - `highlightCurrentRow`:是否高亮当前行,类型为 Boolean。 通过封装这个 TableList 组件,我们可以在项目中更加方便地使用 Element-UITable 组件,同时也可以减少代码重复。 ### 回答2: element-ui是一款基于Vue.js的UI组件库,而tableList是element-ui中的一个组件封装tableList组件的主要作用是用于展示和管理数据表格。它提供了丰富的功能和选项,使得我们可以快速地构建出符合需求的数据表格界面。 首先,我们可以通过tableList组件来定义表格的列,包括列的名称、宽度、对齐方式等。可以根据实际需求设置不同的列,并且还可以对列进行排序、隐藏等操作。 其次,tableList组件还支持分页功能,可以根据数据的大小自动生成相应的分页器,方便用户浏览和查找数据。 另外,tableList还提供了搜索和筛选的功能,用户可以根据表格中的内容进行搜索和筛选操作,以方便快速找到想要的数据。 此外,tableList还支持多选、单选等操作,用户可以通过复选框或者单选按钮来选择需要的数据进行操作。 总的来说,tableList是element-ui中的一个封装组件,它简化了数据表格的开发和管理,提供了丰富的功能和选项,使得我们能够更加灵活和高效地展示和管理数据表格。 ### 回答3: element-ui封装中的tableList是对element-ui中的Table组件进行封装后的一个自定义组件。该组件可以方便地在项目中使用Table,减少重复代码的编写。 tableList的特点是具有高度的可定制性和扩展性。通过传入不同的参数和配置,可以实现不同的表格功能和样式。例如,可以通过指定columns来定义表格的列数和展示内容;通过指定data来传入数据源;通过设置pagination来实现分页等功能。 在tableList组件中,我们还可以通过插槽(slot)的方式来自定义表格的一些样式和功能。比如,可以使用slot="header"来自定义表头,使用slot-scope在插槽内部可以访问到每一行的数据。 除了基本的表格功能外,tableList还可以在数据源发生变化时进行自动刷新,通过设置@refresh事件来实现数据的实时更新。 总之,element-ui封装tableList组件可以帮助我们快速搭建出功能齐全、扩展性强的表格展示页面。它简化了我们对Table组件的使用,提高了开发效率,同时保留了element-ui原本的特性和优势。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值