Vue2.JS 实现商品的增删改查

VUE2.JS实现简单的商品的增删改查
有疑问看源码 ~~代码中注释很详细

<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>商品管理-综合案例</title>
    <style>
        * {
            padding: 0;
            margin: 0;
            box-sizing: border-box;
        }

        table {
            width: 80%;
            margin: 50px auto;
        }

        h3 {
            text-align: center;
            margin: 20px auto;
        }

        table tr th {
            border-bottom: 3px solid darkblue;
        }

        table tr th,
        table tr td {
            height: 40px;
            line-height: 40px;
            text-align: center;
            background-color: rgba(137, 43, 226, 0.215);
        }

        .btn {
            width: 120px;
            height: 40px;
            border: none;
            border-radius: 15px;
            text-align: center;
            outline: none;
            border: 2px solid seagreen;
            cursor: pointer;
        }

        .btn-success {
            background-color: rgba(127, 255, 212, 0.418);
        }

        .btn-success:hover {
            background-color: rgb(127, 255, 212);
        }

        .btn-danger {
            background-color: rgba(255, 55, 0, 0.311);
        }

        .btn-danger:hover {
            background-color: red;
        }

        input {
            display: block;
            width: 60%;
            height: 40px;
            border: 2px solid fuchsia;
            border-radius: 6px;
            outline: none;
            font-size: 13px;
            padding-left: 20px;
            margin: auto;
        }
    </style>
</head>

<body>
    <div id="app">
        <!-- @keyup.enter="submitGoods" 用户按下回车键 触发事件 -->
        <!--     v-model:提供给 表单 数据的双向绑定指令  -->
        <h3>商品管理系统 <small>{{totalGoods}} 个商品</small></h3>
        <input type="text" name="goodsInfo" id="goodsInfo" v-model.lazy.trim="goodsInfo" @keyup.enter="submitGoods"
            placeholder="请输入新增商品,格式: 商品名称:商品价格">
        <table>
            <tr>
                <th>序号</th>
                <th>名称</th>
                <th>单价</th>
                <th>操作</th>
            </tr>
            <!--  v-for:一种循环指令,用于数据/集合数据的循环,称为 列表渲染指令! -->
            <!-- :key动态属性,添加正在循环的对象的属性(唯一) -->
            <tr v-for="(goods,index) in goodsList" :key="goods.id">
                <td>{{index + 1}}</td>
                <td>{{goods.name}}</td>
                <td>{{goods.price | formatPrice}}</td>
                <td>
                    <button class="btn btn-success" @click="editGoods(goods)">编辑</button>
                    <button class="btn btn-danger" @click="delGoods(goods.id)">编辑</button>
                </td>
            </tr>
        </table>
    </div>

    <script src="./vue.js"></script>
    <script>
        const app = new Vue({
            el: "#app",
            data: {
                //声明一个新增商品的变量
                goodsInfo: "",

                //存放一个新增商品的变量
                editId: "",

                //声明商品数据
                goodsList: [
                    { id: 3, name: "宝马7系", price: 860000 },
                    { id: 2, name: "奥迪A6", price: 450000 },
                    { id: 1, name: "凯迪拉克CT5", price: 400000 }
                ]
            },
            //计算属性
            computed: {
                totalGoods: function () {
                    return this.goodsList.length
                }
            },
            //局部过滤器
            filters: {
                formatPrice: value => {
                    return `${parseInt(value).toFixed(2)}`
                }
            },

            methods: {
                submitGoods() {
                    console.log("用户点击了回车,准备提交数据")
                    //判断数据不能为空
                    if (this.goodsInfo === "") {
                        alert("商品数据不能为空")
                        return
                    }
                    //判断是否包含:符号
                    if (this.goodsInfo.indexOf(":") < 0) {
                        alert("商品数据中没有包含:符号")
                        return
                    }

                    //判断是否包含了多个:符号
                    if (this.goodsInfo.indexOf(":") !== this.goodsInfo.lastIndexOf(":")) {
                        alert("用户输入了多个:符号")
                        return
                    }

                    //:符号不能在头尾位置出现
                    if (this.goodsInfo.startsWith(":") || this.goodsInfo.endsWith(":")) {
                        alert(":这个符号不能出现在开头结尾的位置")
                        return
                    }
                    //拆分用户输入的数据,并解构赋值
                    //
                    let [name, price] = this.goodsInfo.split(":")



                    if (this.editId) {
                        //如果editId中有数据,表示正在编辑商品
                        let index = this.goodsList.findIndex(goods => goods.id === this.editId)
                        //根据索引编辑商品
                        this.goodsList[index].name = name
                        this.goodsList[index].price = price
                        //清空输入框,清空editId
                        this.editId = ""
                        this.goodsInfo = ""

                    } else {
                        //如果没有,表示新增
                        //计算新id数据
                        let id = this.goodsList.length > 0 ? this.goodsList[0].id + 1 : 1

                        //[开头位置] 新增数据
                        this.goodsList.unshift({ id, name, price })

                        //清空表单数据
                        this.goodsInfo = ""
                    }

                },
                //展示编辑数据
                editGoods(goods) {
                    //给编辑的id编号赋值
                    this.editId = goods.id
                    //给输入框绑定的变量赋值(展示编辑数据)
                    this.goodsInfo = goods.name + ":" + goods.price

                },
                //删除数据
                delGoods(id) {
                    //二次确认
                    const result = confirm("确定要删除该商品吗?")
                    if (!result) return

                    //删除商品
                    let index = this.goodsList.findIndex(goods => goods.id === id)
                    this.goodsList.splice(index, 1)
                }
            }

        })
    </script>
</body>

</html>

页面展示效果如下
Vue2.JS 实现商品的增删改查_第1张图片

你可能感兴趣的:(javascript,前端,vue.js,程序人生)