element dialong中的table设置默认选中状态

1、点击按钮弹出一个dialog同时请求table要展示的数据,dialog中有一个table和分页功能,当打开dialog就给table中的复选框默认选中状态。
我是这样操作的,但是没有任何反应。

//页面上的按钮

<el-button type="primary" **@click="addCcObject"**>添加抄送对象</el-button>
addCcObject(){
        this.CCdialog = true;//打开弹出框
        this.doCcData()//获取table中要展示的数据。
      },
//弹出框
<el-dialog title="添加抄送人" :visible.sync="CCdialog" **@open="showCcDialog"**>
        <div style="margin:15px 0">
          <el-input v-model="CCinputValue" placeholder="请输入内容" style="width: 200px" clearable></el-input>
          <el-button type="primary" @click="ccsearchBtn">搜索</el-button>
        </div>
        <el-table :data="ccData"  ref="ccData" @selection-change="handleSelectionChange" border>
          <el-table-column type="selection" width="55">
          </el-table-column>
          <el-table-column property="caption" label="机构名称"></el-table-column>
        </el-table>

        <div style="margin:15px 0">
          <el-pagination
            background
            layout="prev, pager, next"
            :page-size="pageSize"
            :current-page.sync="currentPage"
            @current-change="currentChange"
            :total="count">
          </el-pagination>
        </div>

        <div slot="footer" class="dialog-footer">
          <el-button @click="CCdialog = false">取 消</el-button>
          <el-button type="primary" @click="ccOk">确 定</el-button>
        </div>
      </el-dialog>
      
            
      //弹出框的回调函数
      showCcDialog(){
        this.$nextTick(() => {
          let selected = this.data_cc//默认要选择复选框的数据
          selected.forEach(i => {
             this.$refs.ccData.toggleRowSelection(this.ccData.find(d => d.id === i.id), true)  // 设置默认选中
          })
        })
      },
showCcDialog(){
        this.$nextTick(() => {
          let selected = this.data_cc//默认要选择复选框的数据
          selected.forEach(i => {
            //this.$refs.ccData.toggleRowSelection(this.ccData[2], true)  // 设置默认选中
            //this.$refs.ccData.toggleRowSelection(this.ccData.find(d => d.id === i.id), true)  // 设置默认选中
            this.$refs.ccData.toggleRowSelection(this.ccData.find(d => d.id === i.id), true)  // 设置默认选中
          })
        })
      },
阅读 5.6k
2 个回答

选中事件不应该放在弹出框的回调函数中,应该放在表格数据请求完成的回调中

这个我也遇到了,我的是要通过改变数据重新设置勾选的数据,通过打印数据是改变的,只是表格的勾选状态未变,this.$refs.multipleTable.toggleRowSelection(row,true),当我把这个设置勾选的参数true去掉后,它能正常选中和反选,但是我去掉true后就不能在以前勾选的基础上去掉一些,我怀疑是框架的复用dom,没有更新状态,于是我先this.$refs.multipleTable.clearSelection()去掉勾选状态,再this.$refs.multipleTable.toggleRowSelection(row,true)就可以了

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题