Vue結合element實現 取消和確認 實時展現數據效果
<template>
<div>
<el-button type="text" @click="dialogFormVisible = true">添加欄目</el-button>
<el-table
:data="tableData"
style="width: 100%">
<el-table-column
prop="id"
label="ID"
>
</el-table-column>
prop="cname"
label="欄目名稱"
>
</el-table-column>
<el-table-column
prop="type"
label="欄目類型">
</el-table-column>
<el-table-column
label="操作"
>
<template slot-scope="scope">
<el-button @click="handleClick(scope.row)" type="text" size="small">編輯</el-button>
</template>
</el-table-column>
</el-table>
<!-- 分類添加-->
<el-dialog title="添加欄目" :visible.sync="dialogFormVisible">
<el-form :model="form">
<el-form-item label="分類名稱">
</el-form-item>
<el-form-item label="名稱">
<el-input v-model="form.type" autocomplete="off"></el-input>
</el-form-item>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button type="primary" @click="onSubmit">提交</el-button>
<el-button @click="dialogFormVisible=false">取消</el-button>
</div>
</el-dialog>
<!-- 編輯-->
<el-dialog title="修改欄目" :visible.sync="dialogUpdateVisible">
<el-form :model="formUpdate">
<el-form-item label="分類名稱">
<el-input v-model="formUpdate.cname" autocomplete="off"></el-input>
</el-form-item>
<el-form-item label="名稱">
<el-input v-model="formUpdate.type" autocomplete="off"></el-input>
</el-form-item>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button @click="dialogUpdateVisible=false">取消</el-button>
<el-button type="primary" @click="onSubmitUpdate">提交</el-button>
</div>
</el-dialog>
</div>
</template>
<script>
import Category from ‘../../api/category‘;
let {query, insert,update,del} = Category;
export default {
name: "index",
data() {
return {
tableData: [],
dialogFormVisible: false,
dialogUpdateVisible:false,
form: {
cname: ‘‘,
type: ‘‘
},
formUpdate:{
id:‘‘,
cname:‘‘,
type:‘‘
}
};
},
methods: {
getDate() {
query().then(res => {
this.tableData = res.data.data
})
},
onSubmit() {
insert(this.form).then(res => {
this.dialogFormVisible = false;
if (res.data.code == 0) {
this.form.id = res.data.id;
this.tableData.push(this.form);
this.from = {cname: ‘‘, type: ‘‘}
}
});
},
handleClick(row) {
this.dialogUpdateVisible=true;
this.formUpdate=JSON.parse(JSON.stringify(row));
},
onSubmitUpdate(index,row){
this.$confirm(‘此操作將永久修改, 是否繼續?‘, ‘提示‘, {
confirmButtonText: ‘確定‘,
cancelButtonText: ‘取消‘,
type: ‘warning‘
}).then(() => {
update(this.formUpdate).then(res=>{
if (res.data.code==0){
let index=this.tableData.findIndex(ele=>ele.id==this.formUpdate.id);
this.tableData[index]=this.formUpdate
this.dialogUpdateVisible=false
}
});
this.$message({
type: ‘success‘,
message: ‘修改成功!‘
});
}).catch(() => {
this.$message({
type: ‘info‘,
message: ‘已取消修改‘
});
});
},
headleDel(id){
this.$confirm(‘此操作將永久刪除該文件, 是否繼續?‘, ‘提示‘, {
confirmButtonText: ‘確定‘,
cancelButtonText: ‘取消‘,
type: ‘warning‘
}).then(() => {
this.$message({
type: ‘success‘,
message: ‘刪除成功!‘
});
}).catch(() => {
this.$message({
type: ‘info‘,
message: ‘已取消刪除‘
});
});
},
},
beforeMount() {
this.getDate();
}
}
</script>
<style scoped>
</style>
Vue結合element實現 取消和確認 實時展現數據效果