记录一下Vue + element 获取表格选中行的数据

Element表格需要使用可以多选的表格,需要绑定一个事件,@selection-change="selsChange"

在vue的script区的methods中写这个方法:

 selsChange: function (sels) {
     this.sels = sels;
 },

这个方法的意思是将选中的行的数据赋值给sels,选中的数据是对象,而在实际业务中,比如说批量删除,需要的是选中的数据中的id

 //批量删除
      batchRemove: function () {
      var ids = this.sels.map(item => item.id);
       this.$confirm('确认删除选中记录吗?', '提示', {
         type: 'warning'
            }).then(() => {
              this.listLoading = true;
              //NProgress.start();
                    let para = { ids: ids };
                    this.$http.post('/searchMasterMsg/dels',ids).then((res) => {
                        this.listLoading = false;
                        //NProgress.done();
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        this.getSearchMasterMsgs();
                    });
                }).catch(() => {

                });
            }
        },

这一行代码

var ids = this.sels.map(item => item.id);

就是将选中的数据对象取出来遍历然后将数据对象的id返回给变量ids,返回的ids是一个数组


版权声明:本文为m0_46680540原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。