vue 通用table组件(子组件)获取父类分页组件的currentPage和pageSize

子组件:
<template>
  <el-table :data="tableData" :newPage="newPage" :newPageSize="newPageSize" border>
    <el-table-column
      type="index"
      :index="indexMethod"
      width="50"
      align="center"
    ></el-table-column>
    <!--prop: 字段名name, label: 展示的名称, fixed: 是否需要固定(left, right), minWidth: 设置列的最小宽度(不传默认值),align 设置列的相对位置-->
    <el-table-column v-for="(item,key) in tableKey"
                     :key="key"
                     :prop="item.prop"
                     :label="item.label"
                     :minWidth="item.minWidth"
                     :align="item.align"
                     :fixed="item.fixed"
    ></el-table-column>

  </el-table>

</template>

<script>

  export default{

    name: 'newTable',

    data(){

      return{

      }

    },

    methods: {
      //翻页修改索引
      indexMethod(index){
        return (this.newPage-1)*this.newPageSize+index+1;
      },
    },

    props:{
      tableKey: {
        type: Array,
        default: []
      },
      tableData: {
        type: Array,
        default: []
      },
      //newPage和newPageSize完全是为了实现自定义分页的索引
      newPage:{
        type:Number,
        default:1
      },
      newPageSize:{
        type:Number,
        default:10
      }

    },

  }

</script>

父组件:

<template>
  <div>
    <newTable :tableData="tableData" :tableKey="tableKey" :newPage="newPage" :newPageSize="newPageSize" v-loading="loading" style="width: 95%;margin-top: 5px;margin-left: 25px;"></newTable>
    <div class="block" style="width: 95%;text-align: center;margin-left: 25px">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="recordSize">
      </el-pagination>
    </div>
    
</template>

<script>

  import newTable from '../../components/newTable';
  import $ from "jquery";
  export default {
    data() {
      return {
        //table
        tableData: [],
        tableKey: [],
        loading:true,
        //pagination
        currentPage: 1,//初始化当前页码
        pageSize:10,//初始化页面显示条数
        recordSize:0,//初始化数据总数
      };
    },

    //页面加载完成时调用
    mounted() {
      console.log("111");
      //表格字段和样式初始化
      this.getTableKey();
      //表格数据初始化
      this.getTableData();
    },

    //局部注册组件
    components: {
      newTable,
    },

    methods:{

      back(){
        this.$router.go(-1);//返回上一层
      },

      //获取指定类型页面的字段,名称,格式等信息
      getTableKey(){
        let that = this;
        let param={
          "XX" : this.$route.query.type,
          "XX" : this.$route.query.type
        };
        $.ajax({
          async: true,
          cache: false,
          //后台用@RequestBody接收数据,如果没有contentType如下设置则会提示not support
          contentType: "application/json;charset=UTF-8",
          //对于特殊的数据做一次转义
          data:JSON.stringify(param),
          type: "POST",
          dataType: "text",
          url: "XXX",
          success: function(result) {
            that.tableKey = eval(result);
          },
          error: function(data) {
            // 请求失败
            that.$alert("Server error, please contact administrator", "", {
              confirmButtonText: "Comfirm",
              callback: action => {}
            });
          }
        });
      },

      //请求数据
      getTableData(){
        let that = this;
        that.loading = true;
        let param={
          "XX" : this.$route.query.date,
          "XX" : this.$route.query.type,
          "rows" : that.pageSize,
          "page" : that.currentPage,
        };
        $.ajax({
          async: true,
          cache: false,
          //后台用@RequestBody接收数据,如果没有contentType如下设置则会提示not support
          contentType: "application/json;charset=UTF-8",
          //对于特殊的数据做一次转义
          data:JSON.stringify(param),
          type: "POST",
          dataType: "json",
          url: "XXX",
          success: function(result) {
            that.recordSize = XXX;
            that.tableData = result;
            that.loading = false;
          },
          error: function(data) {
            that.loading = true;
            // 请求失败
            that.$alert("Server error, please contact administrator", "", {
              confirmButtonText: "Comfirm",
              callback: action => {}
            });
          }
        });
      },

      //分页
      handleSizeChange(val) {
        this.pageSize = val;
        this.newPageSize = val;
        this.getTableData();
      },
      handleCurrentChange(val) {
        this.currentPage = val;
        this.newPage = val;
        this.getTableData();

      }

    },
    beforeRouteLeave(to,from,next){
      to.meta.keepAlive = true;
      next();
    }
  };
</script>


<style scoped>
  .el-table th>.cell {
    text-align: center;
  }
</style>

上面的代码因为一些原因做了处理,主要的方式就是在子组件的tabel上面绑定参数,然后在props中设置类型和初始值

在父组件中也绑定一样的参数(最好不要和其他参数有冲突)

绑定值后,在父组件中相应方法中获取值即可

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
实现跨页多选需要记录选中的数据的ID,可以使用Vue的Reactive响应式对象来进行数据绑定和监听。具体实现步骤如下: 1. 定义一个空数组selected,用来存储选中的数据的ID。 ```javascript import { reactive } from 'vue'; const state = reactive({ selected: [] }); ``` 2. 在表格的每一行中添加一个多选框,绑定一个change事件,根据选中状态添加或删除选中的数据的ID。 ```html <template> <a-table :dataSource="data" :columns="columns"> <template #selection="{row}"> <a-checkbox v-model:checked="isSelected(row.id)" @change="handleSelect(row.id, $event.target.checked)"></a-checkbox> </template> </a-table> </template> ``` ```javascript const handleSelect = (id, checked) => { if (checked) { state.selected.push(id); } else { state.selected.splice(state.selected.indexOf(id), 1); } }; ``` 3. 在表格的分页组件中添加一个自定义的选中项数量显示组件,根据selected数组的长度来显示选中项的数量。 ```html <template> <div> <a-pagination :total="total" :current="current" @change="handleChange"></a-pagination> <span>已选中 {{ selected.length }} 项</span> </div> </template> ``` 4. 在分页组件的change事件中,重新加载当前页的数据时,需要将之前选中的数据的ID与当前页的数据进行比对,保留选中状态。 ```javascript const handleChange = (page, pageSize) => { // 获取当前页的数据 const newData = fetchData(page, pageSize); // 比对选中状态 const selectedIds = new Set(state.selected); newData.forEach(item => { if (selectedIds.has(item.id)) { item.selected = true; } }); // 更新表格数据 state.data = newData; }; ``` 这样就可以实现跨页多选了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

menglixiazhiweizhi

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

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

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

打赏作者

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

抵扣说明:

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

余额充值