element+Vue2,在一个页面跳转到另一个页面,并自动选中table的某一行

  • 需求:点击A页面的某处,跳转到B页面并选中B页面表格的某一行(点击B页面的搜索后需要清空默认选择的状态)
  • 环境:vue2、element的table,table允许多选
  • 知识点:主要使用到table的这两属性::row-key="":reserve-selection="true和表格的toggleRowSelection方法,还有getList()的回调函数的使用就是getlList要有返回值,通知create里在getlis调取成功t后再做什么,不然渲染报错
    在这里插入图片描述
    在这里插入图片描述
  • 代码
// html
<el-table 
            :header-cell-style="{background:'#d0d0d0',color:'#000'}" 
            ref="reportmul"
            :data="tableData"
            style="width: 100%"
            height="700"
            @selection-change="handleSelectionChange"
            @row-click="rowClick"  
            :row-style="rowStyle" 
            :row-class-name="rowClassName"
            
            :row-key="getRowKeys"
           
          >
          <el-table-column type="selection"  align="center" :reserve-selection="true"></el-table-column>
		// .......
</el-table>

// B页面js
	// 其他页面跳转到B页面时需要再created里判断是不是跳转而来,通常是判断路由有没有带参数,然后执行getList获取页面数据,有数据后才能设置默认选中操作!
data() {
      return{
          searchVal:'',// 搜索
          multipleSelection:[],// 当前选择的行
          tableData:[],// table数据
          }
         },
created(){
    // 初始化
    if(this.$route.params.input){
      this.searchVal = this.$route.params.input
      await this.getList().then(res=>{
       
         /**
         * 如果跳转过来,就给multipleSelection 赋值初始值
         */
        this.multipleSelection[0] = this.tableData[0]
        this.$refs.reportmul.toggleRowSelection(this.tableData[0],true)//这句重点
      })
    }
   this.getList()
  },
 methods: {
	getList(obj={}){
     return listgetpage(xxx).then(res=>{
              this.tableData  = res.data.result
             
      })
     },

	/** 搜索 */
    onSearch(){
      this.getList({input:this.searchVal}).then(()=>{
      // 搜索后清空默认选择
        // this.multipleSelection =[]
        this.$refs.reportmul.clearSelection()
      }
     )
      

    },

}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值