五.品牌管理案例

2022-06-28 19:56:54 浏览数 (1)

本文最后更新于 872 天前,其中的信息可能已经有所发展或是发生改变。

代码语言:javascript复制
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>品牌管理</title>
    <script src="../lib/vue-2.4.0.js"></script>
</head>
<body>
    <div id="app">
            <label >Id:<input type="text" v-model="inputId"></label>
            <label >Name:<input type="text" v-model="inputName"></label>
            <input type="button" @click="create" value="增加">
            <label >搜索名字关键字:<input type="text" v-model="keyWord"></label>
            <table border="1px" style="width: 100%">
            <tr>
                <th>Id</th>
                <th>Name</th>
                <th>CreateTime</th>
                <th>Operation</th>
            </tr>
            <tr v-for="item in search(keyWord)" :key="item.id" >
                <td v-text="item.id"></td>
                <td v-text="item.name"></td>
                <td v-text="item.createTime"></td>
                <td><a @click.prevent=delate(item.id) href="">删除</a></td>
            </tr>
        </table>

    </div>
    <script>
        var vm=new Vue({
            el:"#app",
            data:{
                inputId:"",
                inputName:"",
                keyWord:"",
                list:[{id:"401",name:"奔驰",createTime:new Date()},
                    {id:"402",name:"宝马",createTime:new Date()},
                    {id:"403",name:"奔奔",createTime:new Date()},
                ],
            },
            methods:{
                delate(id){
                    console.log("delate" id)
                    // this.list.some((item,i)=>{
                    //     if(item.id==id){
                    //         this.list.splice(i,1)
                    //         return true
                    //     }
                    // })
                    var index=this.list.findIndex((item)=>{
                        if(item.id==id){
                            return true
                        }
                    })
                    this.list.splice(index,1)
                },
                create(){
                    console.log("create");
                    this.list.push({id:this.inputId,name:this.inputName,createTime:new Date()})
                    this.inputId=this.inputName=""
                },
                search(keyWord){
                    // var newList=[];
                    // this.list.forEach(element => {
                    //     if(element.name.indexOf(keyWord)!=-1){
                    //         newList.push(element)
                    //     }
                    // });
                    // return newList
                    return this.list.filter(item=>{
                        if(item.name.includes(keyWord)){
                            return item
                        }
                    })
                }
            }
        })
    </script>
</body>
</html>

Post Views: 295

0 人点赞