<template>
<div id="app">
<el-button type="primary" @click="dialogVisible = true">添加</el-button>
<el-table :data="tableData">
<el-table-column label="学号" prop="id"> </el-table-column>
<el-table-column label="姓名" prop="name"> </el-table-column>
<el-table-column label="生日" prop="birthday"> </el-table-column>
<el-table-column label="学院" prop="college"> </el-table-column>
<el-table-column label="专业" prop="major"> </el-table-column>
<el-table-column align="right">
<template v-slot="scope"
><el-button type="primary" @click="modify(scope.row)" plain style="width: 50px; font-size: 13px"
>修改</el-button
>
<el-popconfirm
confirmButtonText="好的"
cancelButtonText="不用了"
icon="el-icon-info"
iconColor="red"
@confirm="deleteStudent(scope.row.id)"
title="这是一段内容确定删除吗?"
>
<template #reference>
<el-button type="danger">删除</el-button>
</template>
</el-popconfirm>
</template>
</el-table-column>
</el-table>
<el-pagination
background
@current-change="handleCurrentChange"
layout="prev,pager,next"
:total="totalNum"
@next-click="getList(pageNum+1)"
@prev-click="getList(pageNum-1)"
>
</el-pagination>
<el-dialog
title="提示"
v-model="dialogVisible"
width="30%"
:before-close="handleClose"
>
<el-form
:model="ruleForm"
status-icon
ref="ruleForm"
label-width="100px"
class="demo-ruleForm"
>
<el-input type="text" v-model="ruleForm.id" v-show="false"></el-input>
<el-form-item label="姓名">
<el-input
type="text"
v-model="ruleForm.name"
></el-input>
</el-form-item>
<el-form-item label="生日">
<el-input
type="date"
v-model="ruleForm.birthday"
></el-input>
</el-form-item>
<el-form-item label="学院">
<el-input v-model.number="ruleForm.college"></el-input>
</el-form-item>
<el-form-item label="专业">
<el-input v-model.number="ruleForm.major"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary"
>提交</el-button>
<el-button @click="resetForm('ruleForm')">重置</el-button>
</el-form-item>
</el-form>
<template #footer>
<span class="dialog-footer">
<el-button @click="dialogVisible = false">取 消</el-button>
<el-button type="primary" @click="submitForm()" >
确 定</el-button>
</span>
</template>
</el-dialog>
</div>
</template>
<script>
import axios from "axios";
// import axios from "axios";
export default {
data() {
return {
tableData: [],
search: "",
url: "http://localhost:9090/crud",
pageNum: 1,
pageSize: 10,
dialogVisible: false,
ruleForm: {},
totalNum: 0,
};
},
methods: {
handleCurrentChange(pageNum){
this.getList(pageNum)
},
deleteStudent(id) {
let that = this;
axios
.get(this.url + "/delete?id=" + id)
.then(function (res) {
if (res.data.success) {
that.$message({
type: "success",
message: res.data.data,
})
that.getList(that.pageNum);
} else {
that.$message({
type: "error",
message: res.data.message,
});
}
})
.catch(function (res) {
console.log(res);
});
},
getList(pageNum) {
let that = this;
axios
.post(this.url + "/list", {
pageNum: pageNum,
pageSize: this.pageSize,
})
.then(function (res) {
console.log(res);
that.tableData = res.data.data.records;
that.totalNum = res.data.data.total
})
.catch(function (error) {
alert("error!");
console.log(error);
});
},
modify(student){
this.ruleForm = student
this.dialogVisible = true
}
,
resetForm(){
this.ruleForm = {}
},
submitForm(){
console.log(this.ruleForm)
let that = this
if(!this.ruleForm.id){
axios.post(this.url + "/add",this.ruleForm)
.then(function (res) {
console.log(res);
if (res.data.success) {
that.$message({
type: "success",
message: res.data.data,
});
that.getList(that.pageNum);
// 跳转成功,弹框失效
that.ruleForm = {}
that.dialogVisible = false
} else {
that.$message({
type: "error",
message: res.data.message,
});
}
})
.catch(function (error) {
alert("error!");
console.log(error);
});
} else {
axios.post(this.url+'/modify',this.ruleForm).then(function (res) {
console.log(res)
if(res.data.success){
that.$message({
type: 'success',
message: res.data.data
})
that.getList(1)
that.ruleForm = {}
that.dialogVisible = false
}else{
that.$message({
type: 'error',
message: res.data.message
})
}
}).catch(function (error) {
alert('error!')
console.log(error)
})
}
}
},
created() {
this.getList(1);
},
};
</script>
<style>
</style>