每天记录一个小知识3----elementUI多选,分页保留所有选中数据

此代码示例展示了在Vue.js中如何实现父组件与子组件之间的数据通信。父组件包含一个表格和按钮,通过调用子组件方法更新表格数据。子组件是一个对话框,包含搜索表单和可选择的表格,用户可以选择表格行并将其发送回父组件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

父组件

<template>
  <div>
    <el-button type="primary" plain size="mini" @click="select" icon="el-icon-plus">选择</el-button>
    <el-table v-loading="loading" :data="selectData" border style="margin-top: 10px;">
      <el-table-column prop="username" label="名称" align="center" width="150px" />
      <el-table-column prop="mobile" label="手机号" align="center" width="150px" />
      <el-table-column prop="lastLoginDate" label="最后登录时间" align="center" width="190px" />
      <el-table-column label="操作" align="center" class-name="small-padding">
        <template slot-scope="scope">
          <el-button size="mini" type="danger" icon="el-icon-delete" @click="handleDelete2(scope.row,scope.$index)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <Select ref="select" @select="getselect" :tableData='ids'></Select>
  </div>
</template>

<script>
  import Select from '@/components/Select';
  export default{
    components: {
      Select
    },
    data(){
      return{
        loading:false,
        ids:[],
        selectData:[],
      }
    },
    
    methods:{
      select(){
        this.$refs.select.getSelect();
      },
      getselect(e){
        if(e){
           this.ids=[...e];
          this.selectData=[...e]
        }
      
      },
      handleDelete2(row,index){
        this.ids.splice(index,1);
        this.selectData.splice(index,1)
      }
    }
  }
</script>

<style>
</style>

子组件

<template>
  <el-dialog :visible.sync="Visible" width='80%'>
    <el-form :inline="true" :model="queryParams" class="demo-form-inline" style="margin-top: 20px;">
      <el-form-item label="名称">
        <el-input v-model="queryParams.username" placeholder="请输入名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="昵称" align="center">
        <el-input v-model="queryParams.nickName" placeholder="请输入昵称" clearable></el-input>
      </el-form-item>
      <el-form-item label="联系方式" align="center">
        <el-input v-model="queryParams.mobile" placeholder="请输入联系方式" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" class="el-icon-search" @click="onSubmit1">搜索</el-button>
      </el-form-item>
    </el-form>

    <el-form>
      <el-table :data="List" v-loading="loading" @select="handleRowClick" @select-all="handleAllclick" :row-key="row => { return row.id }"
        ref="tableDataList" border>
        <el-table-column type="selection"  align="center" />
        <el-table-column property="username" label="名称" ></el-table-column>
        <el-table-column property="nickName" label="昵称" ></el-table-column>
        <el-table-column property="mobile" label="联系方式"></el-table-column>
      </el-table>
      <pagination small :page-size="10" v-show="total>0" :total="total" :page.sync="queryParams.pageNumber"
        :limit.sync="queryParams.pageSize" @pagination="getSelect" />
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm2">确 定</el-button>
      <el-button @click="cancel2">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { selectList} from '@/api/index.js';
  export default{
    props:{
      tableData:{
        type:Array,
        default:()=>[]
      }

    },
    data(){
      return{
        Visible:false,
        List:[],
        loading:true,
        total:0,
        ids:[],
        selectData:[],
        queryParams: {
          pageNumber: 1,
          pageSize: 10,
        },
      }
    },
    watch:{
       tableData:{
         handler(news,olds){
           this.selectData= news
         },
         immediate:true,
       },

    },
    methods:{
      //当用户手动勾选全选 Checkbox 时触发的事件
      handleAllclick(selection){
        let _this = this;
        let selectionIdList = [];
        let idList = [];
        if(selection.length > 0){
          // 判断是否再当前选中组中
          selection.forEach(m => {
           selectionIdList.push(m.id)
          })
          _this.ids.forEach(m => {
           idList.push(m.id)
          })
          selectionIdList.forEach((v,n)=>{
            if(idList.indexOf(v) < 0){
              _this.ids.push(selection[n]);
            }
          })
        }else{
          _this.List.forEach(v => {
            _this.ids.forEach((m,n) => {
              if(v.id == m.id){
                _this.ids.splice(n, 1)
              }
            })
          })
        }
      },
//当用户手动勾选数据行的 Checkbox 时触发的事件
      handleRowClick(selection, row) {
          // 点击行
          let _this = this;
          let selectionIdList = [];
          // 判断是否再当前选中组中
          selection.forEach(m => {
           selectionIdList.push(m.id)
          })
          let idList = [];
          _this.ids.forEach(m => {
           idList.push(m.id)
          })
          if (selectionIdList.indexOf(row.id) < 0) {
           _this.ids.forEach((v, i) => {
            if (v.id == row.id) {
             _this.ids.splice(i, 1)
            }
           })
          } else {
           if (idList.indexOf(row.id) < 0) {
            _this.ids.push(row)
           }
          }
         },

      onSubmit1() {
        this.getSelect();
      },
     
      getSelect() {
        this.Visible = true;
        this.ids = [...this.selectData];
        this.loading = false;
        selectList(this.queryParams).then((res) => {
          if (res.code == 200) {
            this.List = res.result.list;
            this.total = res.result.total;
            this.$nextTick(() => {
              res.result.list.forEach(item => {
                this.ids.forEach(row => {
                  if (item.id == row.id) {
                    this.$refs.tableDataList.toggleRowSelection(item);
                  }
                });
              })
            });
          }
        })
      },
      submitForm2(){
        this.Visible = false;
        this.$emit('select', this.ids)
      },
      cancel2() {
        this.Visible = false;
      },
      
    }
  }
</script>

<style>
</style>

对于 ElementUI 表格的分页回显,可以通过以下步骤实现: 1. 在父组件中定义一个数组 selectedRows,用于存储所有选中的行数据。 2. 在表格组件中添加一个 @selection-change 事件监听器,每当选中数据发生变化时,将最新的选中数据更新到 selectedRows 数组中。 3. 在表格组件中添加一个 :row-key 属性,用于指定表格行数据的唯一标识符。 4. 在表格组件中添加一个 :highlight-current-row="true" 属性,用于高亮当前行。 5. 在表格组件中添加一个 :default-selection 属性,用于设置表格初始化时默认选中的行数据。 6.分页组件中添加一个 @size-change 事件监听器,每当分页大小发生变化时,将当前页选中的行数据更新到 selectedRows 数组中。 7.分页组件中添加一个 :current-change 属性,用于设置当前页数发生变化时的回调函数。 8. 在回调函数中,根据当前页数和每页显示的行数,计算出当前页的行数据在 selectedRows 数组中的下标范围,并将对应的行数据设置为表格的默认选中数据。 9. 在表格组件中添加一个 :selection 属性,用于设置表格的选中数据,这里可以直接将 selectedRows 数组传递给该属性即可。 具体代码实现可以参考以下示例: ```html <template> <div> <el-table :data="tableData" :row-key="row => row.id" :highlight-current-row="true" :default-selection="defaultSelection" :selection="selectedRows" @selection-change="handleSelectionChange" > <el-table-column type="selection"></el-table-column> <el-table-column prop="id" label="ID"></el-table-column> <el-table-column prop="name" label="Name"></el-table-column> <el-table-column prop="age" label="Age"></el-table-column> </el-table> <el-pagination :total="total" :page-size="pageSize" :current-page="currentPage" @size-change="handleSizeChange" :current-change="handleCurrentChange" ></el-pagination> </div> </template> <script> export default { data() { return { tableData: [], selectedRows: [], defaultSelection: [], total: 0, pageSize: 10, currentPage: 1 }; }, mounted() { // 初始化表格数据 this.getTableData(); }, methods: { getTableData() { // 获取表格数据 // ... }, handleSelectionChange(selectedRows) { // 更新选中数据 this.selectedRows = selectedRows; }, handleSizeChange(pageSize) { // 分页大小发生变化时更新选中数据 this.updateSelectedRows(); }, handleCurrentChange(currentPage) { // 当前页数发生变化时更新选中数据 this.currentPage = currentPage; this.updateSelectedRows(); }, updateSelectedRows() { // 计算当前页的选中数据在 selectedRows 数组中的下标范围 const start = (this.currentPage - 1) * this.pageSize; const end = start + this.pageSize; // 将对应的行数据设置为表格的默认选中数据 this.defaultSelection = this.selectedRows.slice(start, end); } } }; </script> ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值