基于上篇的后端接口,开发前端接口调用

12 篇文章 0 订阅
2 篇文章 0 订阅
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8"/>
    <!-- import CSS -->
    <link
            rel="stylesheet"
            href="https://unpkg.com/element-ui/lib/theme-chalk/index.css"
    />
    <style>
        form {
            width: 600px;
            height: 400px;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background-color: #ccc;
            display: none;
            border-radius: 8px;
        }

        form div {
            width: 600px;
            height: 50px;
            line-height: 50px;
            text-align: center;
            border: 1px solid #ccc;

        }

        form div lable {
            width: 100px;
            height: 30px;
            line-height: 30px;
            text-align: center;
            display: inline-block;
        }

        form div input {
            width: 200px;
            height: 30px;
            border: 1px solid #ccc;
            border-radius: 5px;
            outline: none;
        }
    </style>
</head>
<body>
<div id="app">
    <!--    搜索框-->
    <el-input v-model="input" placeholder="通过id搜索"></el-input>
    <el-button type="primary" @click="handleSearch">搜索</el-button>
    <el-button type="primary" @click="addFn">添加</el-button>
    <el-button type="primary" @click="showAll">显示全部</el-button>
    <!--遍历-->
    <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="id" label="编号" width="180"></el-table-column>
        <el-table-column prop="name" label="姓名" width="180">
        </el-table-column>
        <el-table-column prop="age" label="年龄" width="180"></el-table-column>
        <el-table-column prop="sex" label="性别"></el-table-column>
        <el-table-column prop="class1" label="班级"></el-table-column>
        <el-table-column label="操作">
            <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>
    
</div>
</body>
<!-- import Vue before Element -->
<script src="https://unpkg.com/vue@2/dist/vue.js"></script>
<!-- import JavaScript -->
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<!--引入axios-->
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
    new Vue({
        el: "#app",
        data: function () {
            return {
                tableData: [],
                input: "",
                id: "",
                name: "",
                age: '',
                sex: '',
                class1: ''
            };
        },

        methods: {
            showAll() {
                axios
                    .get(`http://localhost:9999/student/findAll`)
                    .then((res) => {
                        // console.log(res.data);
                        this.tableData = res.data;
                    })
                    .catch((error) => {
                        console.log(error);
                    });
            },
            
            async addFn() {
                try {
                    const {value: id} = await this.$prompt('请输入 ID', '提示')
                    const {value: name} = await this.$prompt('请输入名称', '提示')
                    const {value: age} = await this.$prompt('请输入年龄', '提示')
                    const {value: sex} = await this.$prompt('请输入性别', '提示')
                    const {value: class1} = await this.$prompt('请输入班级', '提示')

                    // 将数据发送到后端
                    await axios.get(`http://localhost:9999/student/add?id=${id}&name=${name}&age=${age}&sex=${sex}&class1=${class1}`)

                    this.$message.success('保存成功')
                    this.showAll()
                } catch (error) {
                    console.error(error)
                    this.$message.error('保存失败')
                }
            },
            
            // 搜索方法,将搜索到的数据替换为原来的数据
            handleSearch() {
                axios
                    .get(`http://localhost:9999/student/findById/${this.input}`)
                    .then((res) => {
                        console.log(res.data);
                        this.$message({
                            message: "搜索成功",
                            type: "success",
                        });
                        // 将对象转化为数组
                        res.data = [res.data];
                        this.tableData = res.data;
                    })
                    .catch((error) => {
                        console.log(error);
                    });
            },

            handleEdit(index, rows) {
                console.log(index, rows);
                id = rows.id;
                console.log(id);
                this.$prompt("请输入姓名", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    inputErrorMessage: "请输入2-4个汉字",
                })
                    .then(({value}) => {
                        const name = value;
                        this.$prompt("请输入年龄", "提示", {
                            confirmButtonText: "确定",
                            cancelButtonText: "取消",
                            inputErrorMessage: "请输入正整数",
                        })
                            .then(({value}) => {
                                const age = value;
                                this.$prompt("请输入性别", "提示", {
                                    confirmButtonText: "确定",
                                    cancelButtonText: "取消",
                                    inputErrorMessage: "请输入2-4个汉字",
                                })
                                    .then(({value}) => {
                                        const sex = value;
                                        this.$prompt("请输入班级", "提示", {
                                            confirmButtonText: "确定",
                                            cancelButtonText: "取消",
                                            inputErrorMessage: "请输入2-4个汉字",
                                        })
                                            .then(({value}) => {
                                                const class1 = value;
                                                console.log(name, age, sex, class1);
                                                this.$message({
                                                    type: "success",
                                                    message: "修改成功!",
                                                });
                                                axios.get(
                                                    `http://localhost:9999/student/update/${id}?&name=${name}&age=${age}&sex=${sex}&class1=${class1}`)
                                                    .then((res) => {
                                                        console.log(res.data);
                                                        //     页面刷新
                                                        window.location.reload();
                                                    })
                                                    .catch((error) => {
                                                        console.log(error);
                                                    });
                                            })
                                            .catch(() => {
                                                this.$message({
                                                    type: "info",
                                                    message: "取消输入",
                                                });
                                            });
                                    })
                                    .catch(() => {
                                        this.$message({
                                            type: "info",
                                            message: "取消输入",
                                        });
                                    });
                            })
                            .catch(() => {
                                this.$message({
                                    type: "info",
                                    message: "取消输入",
                                });
                            });
                    })
                    .catch(() => {
                        this.$message({
                            type: "info",
                            message: "取消输入",
                        });
                    });
            },

            // 删除
            handleDelete(index, row) {
                axios
                    .get(`http://localhost:9999/student/deleteById/${row.id}`)
                    .then((res) => {
                        this.$message({
                            message: "删除成功",
                            type: "success",
                        });
                        // 页面刷新
                        window.location.reload();
                    })
                    .catch((error) => {
                        console.log(error);
                    });
            },
        },

        // 页面加载时调用
        mounted() {
            axios
                .get("http://localhost:9999/student/findAll")
                .then((res) => {
                    this.tableData = res.data;
                })
                .catch((error) => {
                    console.log(error);
                });
        }
        ,
    })
    ;
</script>

</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lionliu0519

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值