index.vue文章来源:https://www.toymoban.com/news/detail-649992.html
<script setup>
import { onMounted, ref } from "vue";
import Edit from "./components/Edit.vue";
import axios from "axios";
// TODO: 列表渲染
const list = ref([]);
const getList = async () => {
const res = await axios.get("/list");
list.value = res.data;
};
onMounted(() => {
getList();
});
// TODO: 删除功能
const onDelete = async (id) => {
console.log("id", id);
await axios.delete(`/del/${id}`);
getList();
};
// TODO: 编辑功能
const editRef = ref(null);
const onEdit = (row) => {
editRef.value.open(row);
};
</script>
<template>
<div class="app">
<el-table :data="list">
<el-table-column label="ID" prop="id"></el-table-column>
<el-table-column label="姓名" prop="name" width="150"></el-table-column>
<el-table-column label="籍贯" prop="place"></el-table-column>
<el-table-column label="操作" width="150">
<template #default="{ row }">
<el-button type="primary" @click="onEdit(row)" link> 编辑 </el-button>
<el-button type="danger" @click="onDelete(row.id)" link>
删除
</el-button>
</template>
</el-table-column>
</el-table>
</div>
<Edit ref="editRef" @on-Update="getList"/>
</template>
<style scoped>
.app {
width: 980px;
margin: 100px auto 0;
}
</style>
edit.vue文章来源地址https://www.toymoban.com/news/detail-649992.html
<script setup>
// TODO: 编辑
const emit = defineEmits(["on-Update"]);
import { ref } from "vue";
import axios from "axios";
// 弹框开关
const dialogVisible = ref(false);
const from = ref({
id: "",
name: "",
place: "",
});
const open = (row) => {
console.log("row", row);
from.value.id = row.id;
from.value.name = row.name;
from.value.place = row.place;
dialogVisible.value = true;
};
const onUpdate = async () => {
await axios.patch(`/edit/${from.value.id}`, {
name: from.value.name,
place: from.value.place,
});
dialogVisible.value = false;
emit("on-Update");
};
defineExpose({
open,
});
</script>
<template>
<el-dialog v-model="dialogVisible" title="编辑" width="400px">
<el-form label-width="50px">
<el-form-item label="姓名">
<el-input v-model="from.name" placeholder="请输入姓名" />
</el-form-item>
<el-form-item label="籍贯">
<el-input v-model="from.place" placeholder="请输入籍贯" />
</el-form-item>
</el-form>
<template #footer>
<span class="dialog-footer">
<el-button @click="dialogVisible = false">取消</el-button>
<el-button type="primary" @click="onUpdate">确认</el-button>
</span>
</template>
</el-dialog>
</template>
<style scoped>
.el-input {
width: 290px;
}
</style>
到了这里,关于vue3表格,编辑案例的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!