用Spring Boot+Vue做微人事项目第十四天
目录
职位管理批量删除
后端代码
controller
@DeleteMapping("/")
public RespBean deletePositionByIds(Integer[] ids){
if (positionService.deletePositionByIds(ids) == ids.length){
return RespBean.ok("删除成功");
}else{
return RespBean.error("删除失败");
}
}
service
/**
* 批量删除职位
* @param ids
* @return
*/
public int deletePositionByIds(Integer[] ids) {
for (Integer id : ids) {
System.out.println("id=========================================="+id);
}
return positionMapper.deletePositionByIds(ids);
}
dao
int deletePositionByIds(@Param("ids") Integer[] ids);
mapper
<!--批量删除职位-->
<delete id="deletePositionByIds">
delete from position where id in
<foreach collection="ids" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</delete>
前端代码
html
批量删除首先要有一个批量删除的按钮,multipleSelection.length是为了显示要删除多少条数据
<el-button style="margin-top: 10px" type="danger" size="small" :disabled="multipleSelection.length==0" @click="deleteMany">批量删除</el-button>
<div class="posManaMain">
<el-table
:data="positions"
stripe
size="small"
border
@selection-change="handleSelectionChange"
style="width: 70%">
</el-table>
还要可以选择多条数据,选择多个就需要用一个变量来接受,我这是用的是multipleSelection,也需要在return里面给它赋初始值为数组 multipleSelection:[ ]
data() {
return {
pos: {
name: ''
},
dialogVisible: false,
updatePos: {
name: ''
},
positions: [],
multipleSelection: []
}
},
要在查询的表格添加@selection-change="handleSelectionChange"方法,因为没有这个方法,勾选了要删除的数据之后,点击批量删除的按钮,会没有效果
handleSelectionChange(val) {
this.multipleSelection = val
console.log(val)
},
批量删除的方法是@click="deleteMany"
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: '已取消删除'
});
});
},
前端全部代码
<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>
效果