vue el-table实现多选框回填的示例代码

 更新时间:2024年01月15日 11:30:33   作者:guochanof  
摘要:Vue多选框回填是实现表单数据高效处理的常见需求,本文主要介绍了vue el-table实现多选框回填的示例代码,具有一定的参考价值,感兴趣的可以了解一下

主要代码:

    //选中列,所有列,表名
    toggleSelection(selectRows, totalRows, tablename) {
      this.$refs.table.clearSelection();
      if (selectRows.length > 0) {
        this.$nextTick(() => {
          selectRows.forEach(item => {
            totalRows.forEach(item1 => {
              if (item.userId == item1.userId) {
                this.$refs.table.toggleRowSelection(item1);
              }
            });
          });
        });
      }
    },

效果:

html

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="880px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="add">
        <el-form-item label="分组名称" prop="nums">
          <el-input v-model="form.nums" placeholder="请输分组名称" />
        </el-form-item>

        <el-form-item label="人员" prop="names">
          <el-input v-model="form.names" type="textarea" style="width:500px" />
          <el-input v-model="form.userIds" type="textarea" style="width:500px" />
          <el-button type="primary" plain size="mini" @click="selectProject" style="margin-left:20px">人员选择</el-button>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>



    <!-- 项目选择 -->
    <el-dialog title="选择人员" :visible.sync="projectOpen" width="1000px" :append-to-body="true" @close="cancelSelsectProject" :close-on-click-modal="false">
      <el-form :model="projectQueryParams" ref="projectQueryForm" :inline="true" v-show="showSearch" label-width="68px">

        <el-form-item label="部门" prop="deptId">
          <el-select v-model="projectQueryParams.deptId" filterable clearable placeholder="请选择部门">
            <el-option v-for="item in deptOptions" :key="item.id" :label="item.label" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="姓名" prop="nickName">
          <el-input v-model="projectQueryParams.nickName" placeholder="姓名" clearable />
        </el-form-item>
        <el-form-item label="编制" prop="name">
          <el-select v-model="projectQueryParams.value" placeholder="请选择">
            <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="projectHandleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="projectResetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table v-loading="loading" :data="projectList" ref="table" @row-click="projectSelectRow" @selection-change="handleSelectionChange" :row-key="row=>row.userId" :highlight-current-row="true" @cell-dblclick="dblclickRow(row)" border>
        <el-table-column type="selection" width="50" align="center" />
        <el-table-column label="部门" align="center" prop="dept.deptName" />
        <el-table-column label="姓名" align="center" prop="nickName" />
        <el-table-column label="编制" align="center" prop="types" />
      </el-table>

      <pagination v-show="projectTotal > 0" :total="projectTotal" :page.sync="projectQueryParams.pageNum" :limit.sync="projectQueryParams.pageSize" @pagination="getProjectList" />

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitSelectProject">确 定</el-button>
        <el-button @click="cancelSelsectProject">取 消</el-button>
      </div>
    </el-dialog>

js

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.projectRow = selection;
      this.ids = selection.map(item => item.userId);
      this.names = selection.map(item => item.nickName);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    
    //打开项目选择弹窗
    selectProject() {
      this.projectOpen = true;
      this.getProjectList();
    },

    /** 查询项目列表 */
    getProjectList() {
      this.loading = true;
      let rows = this.projectRow;
      listUser(this.projectQueryParams).then(response => {
        
        this.projectList = response.rows;
        this.projectTotal = response.total;
        
        let selectRows = this.projectRow;
        let totalRows = response.rows;
        
        this.loading = false;
        this.toggleSelection(selectRows, totalRows);
      });
    },

    //选中列,所有列,表名
    toggleSelection(selectRows, totalRows, tablename) {
      this.$refs.table.clearSelection();
      if (selectRows.length > 0) {
        this.$nextTick(() => {
          selectRows.forEach(item => {
            totalRows.forEach(item1 => {
              if (item.userId == item1.userId) {
                this.$refs.table.toggleRowSelection(item1);
              }
            });
          });
        });
      }
    },

到此这篇关于vue el-table实现多选框回填的示例代码的文章就介绍到这了,更多相关vue el-table多选框回填内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家! 

相关文章

  • Vue.js单向绑定和双向绑定实例分析

    Vue.js单向绑定和双向绑定实例分析

    这篇文章主要介绍了Vue.js单向绑定和双向绑定,结合实例形式分析了vue.js单向绑定与双向绑定相关原理、实现方法及操作技巧,需要的朋友可以参考下
    2018-08-08
  • Vue动态组件实例解析

    Vue动态组件实例解析

    让多个组件使用同一个挂载点,并动态切换,这就是动态组件。这篇文章主要介绍了Vue动态组件 ,需要的朋友可以参考下
    2017-08-08
  • vue3界面使用router及使用watch监听router的改变

    vue3界面使用router及使用watch监听router的改变

    vue2中使用router非常简单,但是vue3中略微有些改变,通过本文讲解下他的改变,对vue3 watch监听router相关知识感兴趣的朋友一起看看吧
    2022-11-11
  • vue+element项目中过滤输入框特殊字符小结

    vue+element项目中过滤输入框特殊字符小结

    这篇文章主要介绍了vue+element项目中过滤输入框特殊字符小结,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-08-08
  • vue实现定义一个全局实例Vue.prototype

    vue实现定义一个全局实例Vue.prototype

    这篇文章主要介绍了vue实现定义一个全局实例Vue.prototype,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-07-07
  • vue+element 实现商城主题开发的示例代码

    vue+element 实现商城主题开发的示例代码

    这篇文章主要介绍了vue+element 实现商城主题开发的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-03-03
  • 详解本地Vue项目请求本地Node.js服务器的配置方法

    详解本地Vue项目请求本地Node.js服务器的配置方法

    本文只针对自己需要本地模拟接口于是搭建一个本地node服务器供自己测试使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-03-03
  • 解读vue生成的文件目录结构及说明

    解读vue生成的文件目录结构及说明

    本篇文章主要介绍了解读vue生成的文件目录结构及说明,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-11-11
  • vue3 typescript封装axios过程示例

    vue3 typescript封装axios过程示例

    这篇文章主要为大家介绍了vue3 typescript封装axios过程示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-10-10
  • vue实现微信公众号h5跳转小程序的示例代码

    vue实现微信公众号h5跳转小程序的示例代码

    本文主要介绍了vue实现微信公众号h5跳转小程序的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-08-08

最新评论