前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >用Spring Boot+Vue做微人事项目第十四天

用Spring Boot+Vue做微人事项目第十四天

作者头像
Java架构师必看
发布2021-05-14 11:28:55
发布2021-05-14 11:28:55
32600
代码可运行
举报
文章被收录于专栏:Java架构师必看Java架构师必看
运行总次数:0
代码可运行

用Spring Boot+Vue做微人事项目第十四天

强烈推介IDEA2020.2破解激活,IntelliJ IDEA 注册码,2020.2 IDEA 激活码

用Spring Boot+Vue做微人事项目第十四天

目录

用Spring Boot+Vue做微人事项目第十四天

职位管理批量删除

后端代码

前端代码

效果


职位管理批量删除

后端代码

controller

代码语言:javascript
代码运行次数:0
运行
复制
 @DeleteMapping("/")
    public RespBean deletePositionByIds(Integer[] ids){
        if (positionService.deletePositionByIds(ids) == ids.length){
            return RespBean.ok("删除成功");
        }else{
            return RespBean.error("删除失败");
        }
    }

service

代码语言:javascript
代码运行次数:0
运行
复制
   /**
     * 批量删除职位
     * @param ids
     * @return
     */
    public int deletePositionByIds(Integer[] ids) {
        for (Integer id : ids) {
            System.out.println("id=========================================="+id);
        }
        return positionMapper.deletePositionByIds(ids);
    }

dao

代码语言:javascript
代码运行次数:0
运行
复制
int deletePositionByIds(@Param("ids") Integer[] ids);

mapper

代码语言:javascript
代码运行次数:0
运行
复制
<!--批量删除职位-->
  <delete id="deletePositionByIds">
    delete from position where id in
    <foreach collection="ids" item="id" separator="," open="(" close=")">
      #{id}
    </foreach>
  </delete>

前端代码

html

批量删除首先要有一个批量删除的按钮,multipleSelection.length是为了显示要删除多少条数据

代码语言:javascript
代码运行次数:0
运行
复制
<el-button style="margin-top: 10px" type="danger" size="small" :disabled="multipleSelection.length==0" @click="deleteMany">批量删除</el-button>
代码语言:javascript
代码运行次数:0
运行
复制
 <div class="posManaMain">
            <el-table
                    :data="positions"
                    stripe
                    size="small"
                    border
                    @selection-change="handleSelectionChange"
                    style="width: 70%">
            </el-table>

还要可以选择多条数据,选择多个就需要用一个变量来接受,我这是用的是multipleSelection,也需要在return里面给它赋初始值为数组  multipleSelection:[ ]

代码语言:javascript
代码运行次数:0
运行
复制
 data() {
            return {
                pos: {
                    name: ''
                },
                dialogVisible: false,
                updatePos: {
                    name: ''
                },
                positions: [],
                multipleSelection: []
                
            }
        },

要在查询的表格添加@selection-change="handleSelectionChange"方法,因为没有这个方法,勾选了要删除的数据之后,点击批量删除的按钮,会没有效果

代码语言:javascript
代码运行次数:0
运行
复制
 handleSelectionChange(val) {
            this.multipleSelection = val
            console.log(val)
        },

批量删除的方法是@click="deleteMany"

代码语言:javascript
代码运行次数:0
运行
复制
 deleteMany(){
             this.$confirm('此操作将永久删除【'+ this.multipleSelection.length +'】条记录, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
             }).then(() => {
                let ids = '?';
                this.multipleSelection.forEach(item=>{
                   ids += 'ids=' + item.id + '&';
                })
                console.log("ids==================================:"+ids)
                this.deleteRequest("/system/basic/pos/" + ids).then(resp => {
                   if (resp) {
                      this.initPositions();
                   }
                });
             }).catch(() => {
                this.$message({
                   type: 'info',
                   message: '已取消删除'
                });
             });
          },

前端全部代码

代码语言:javascript
代码运行次数:0
运行
复制
<template>
    <div>
        <div>
            <el-input
                    size="small"
                    class="addPosInput"
                    placeholder="添加职位..."
                    prefix-icon="el-icon-plus"
                    @keydown.enter.native="addPosition"
                    v-model="pos.name">
            </el-input>
            <el-button type="primary" icon="el-icon-plus" size="small" @click="addPosition()">添加</el-button>
        </div>
        <!--:data="tableDate 是data数据里面的tableData属性。表格里面显示的数据是json数组"-->
        <!--el-table-column:每一列-->
        <div class="posManaMain">
            <el-table
                    :data="positions"
                    stripe
                    size="small"
                    border
                    @selection-change="handleSelectionChange"
                    style="width: 70%">
                <el-table-column
                        type="selection"
                        width="55">
                </el-table-column>
                <el-table-column
                        prop="id"
                        label="编号"
                        width="55">
                </el-table-column>
                <el-table-column
                        prop="name"
                        label="职位名称"
                        width="120">
                </el-table-column>
                <el-table-column
                        prop="createDate"
                        label="创建时间">
                </el-table-column>
                <el-table-column label="操作">
                    <!--scope.$index:当前操作到第几行 scope.row:这一行对应的json对象 -->
                    <template slot-scope="scope">
                        <el-button
                                size="mini"
                                @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                        <el-button
                                size="mini"
                                type="danger"
                                @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-button style="margin-top: 10px" type="danger" size="small" :disabled="multipleSelection.length==0" @click="deleteMany">批量删除</el-button>
            
        </div>
         <el-dialog
            title="修改职位信息"
            :visible.sync="dialogVisible"
            width="30%">
            <div>
                <el-tag>职位名称</el-tag>
                <el-input class="updatePosInput" size="small" v-model="updatePos.name"></el-input>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button size="small" @click="dialogVisible = false">取 消</el-button>
                <el-button size="small" type="primary" @click="doUpdate">确 定</el-button>
            </span>
        </el-dialog>



    </div>
</template>

<script>
    export default {
        name: "DepMana",
        data() {
            return {
                pos: {
                    name: ''
                },
                dialogVisible: false,
                updatePos: {
                    name: ''
                },
                positions: [],
                multipleSelection: []
                
            }
        },
        //mounted是一个函数
        mounted(){
            this.initPositions();
        },
        methods:{
            deleteMany(){
             this.$confirm('此操作将永久删除【'+ this.multipleSelection.length +'】条记录, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
             }).then(() => {
                let ids = '?';
                this.multipleSelection.forEach(item=>{
                   ids += 'ids=' + item.id + '&';
                })
                console.log("ids==================================:"+ids)
                this.deleteRequest("/system/basic/pos/" + ids).then(resp => {
                   if (resp) {
                      this.initPositions();
                   }
                });
             }).catch(() => {
                this.$message({
                   type: 'info',
                   message: '已取消删除'
                });
             });
          },
          handleSelectionChange(val) {
            this.multipleSelection = val
            console.log(val)
        },

            //定义添加按钮的方法 添加的时候要做判断,看用户是否输入的值,如果没输入就给错误提示
            addPosition(){
                if (this.pos.name){
                    //this.pos :参数是pos
                    this.postRequest("/system/basic/pos/",this.pos).then(resp=>{
                        if(resp){
                            //添加成功之后需要把表格刷新一下  可以直接用initPositions,重新加载数据
                            this.initPositions();
                            this.pos.name='';
                        }
                    })
                } else {
                    this.$message.error("职位名称不可以为空");
                }
            },
            //定义编辑按钮的方法
            handleEdit(index,data){
                 Object.assign(this.updatePos, data);
                 this.dialogVisible = true
            },

            doUpdate() {
            this.putRequest("/system/basic/pos/", this.updatePos).then(resp => {
                if (resp) {
                    this.initPositions();
                    this.updatePos.name = ''
                    this.dialogVisible = false
                }
            })
        },


            //定义删除按钮的方法
            handleDelete(index,data){
                this.$confirm('此操作将永久删除【'+data.name+'】职位, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.deleteRequest("/system/basic/pos/"+data.id).then(resp=>{
                        if (resp){
                            this.initPositions();
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            //定义一个初始化positions的方法
            initPositions(){
                //发送一个get请求去获取数据 请求地址是"/system/basic/pos/"
                this.getRequest("/system/basic/pos/").then(resp =>{
                    //判断如果resp存在的话,请求成功
                    if (resp){
                        //就把positions的值赋值歌resp就行了
                        this.positions=resp;
                    }
                })
            }
        }
    }
</script>

<style>
    .addPosInput {
        width: 300px;
        margin-right: 8px;

    }
    .posManaMain{
        margin-top: 10px;
    }
</style>

效果

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 用Spring Boot+Vue做微人事项目第十四天
    • 职位管理批量删除
    • 后端代码
    • 前端代码
    • 效果
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档