Vue基础知识系列(三)品牌管理案例

198 阅读6分钟

品牌管理案例

1、添加新品牌

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>01品牌管理案例</title>
    <script src="lib/vue-2.4.0.js"></script>
    <link rel="stylesheet" href="lib/bootstrap-3.3.7.css">
</head>
<body>
<div id="app">
    <div class="panel panel-primary">
        <div class="panel-heading">
            <h3 class="panel-title">添加品牌</h3>
        </div>
        <!--form-inline 让自己写的盒子在同一行显示-->
        <div class="panel-body form-inline">
            <label>
                Id:
                <input type="text" v-model="id">
            </label>
            <label>
                Name:
                <input type="text" v-model="name">
            </label>
            <!--在Vue中,添加事件绑定机制,如果给事件添加了小括号,那么就可以传参了-->
            <input type="button" value="添加" class="btn btn-primary" v-on:click="add">
        </div>
    </div>
    <table class="table table-bordered table-hover table-striped">
        <thead>
        <tr>
            <th>Id</th>
            <th>Name</th>
            <th>Ctime</th>
            <th>Operation</th>
        </tr>
        </thead>
        <tbody>
        <tr v-for="item in list" :key="item.id">
            <td>{{item.id}}</td>
            <td>{{item.name}}</td>
            <td>{{item.ctime}}</td>
            <td>
                <a href="#">删除</a>
            </td>
        </tr>
        </tbody>
    </table>
</div>
<script>
    var vm = new Vue({
        el: "#app",
        data: {
            id: "",
            name: "",
            ctime: "",
            list: [
                {id: 1, name: "奔驰", ctime: new Date()},
                {id: 2, name: "众泰", ctime: new Date()},
                {id: 3, name: "奥拓", ctime: new Date()},
            ]
        },
        methods: {
            add() {
                /*
                获取到id和name,直接从data上面获取
                组织出一个对象
                把这个对象,调用数组的方法,添加到data上的list中
                */
                var car = {id: this.id, name: this.name, ctime: new Date()}
                this.list.push(car)
                this.id = this.name = ""
            }
        }
    })
</script>
</body>
</html>

2、删除品牌

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>01品牌管理案例</title>
    <script src="lib/vue-2.4.0.js"></script>
    <link rel="stylesheet" href="lib/bootstrap-3.3.7.css">
</head>
<body>
<div id="app">
    <div class="panel panel-primary">
        <div class="panel-heading">
            <h3 class="panel-title">添加品牌</h3>
        </div>
        <!--form-inline 让自己写的盒子在同一行显示-->
        <div class="panel-body form-inline">
            <label>
                Id:
                <input type="text" v-model="id">
            </label>
            <label>
                Name:
                <input type="text" v-model="name">
            </label>
            <!--在Vue中,添加事件绑定机制,如果给事件添加了小括号,那么就可以传参了-->
            <input type="button" value="添加" class="btn btn-primary" v-on:click="add">
        </div>
    </div>
    <table class="table table-bordered table-hover table-striped">
        <thead>
        <tr>
            <th>Id</th>
            <th>Name</th>
            <th>Ctime</th>
            <th>Operation</th>
        </tr>
        </thead>
        <tbody>
        <tr v-for="item in list" :key="item.id">
            <td>{{item.id}}</td>
            <td>{{item.name}}</td>
            <td>{{item.ctime}}</td>
            <td>
                <a href="#" v-on:click.prevent="del(item.id)">删除</a>
            </td>
        </tr>
        </tbody>
    </table>
</div>
<script>
    var vm = new Vue({
        el: "#app",
        data: {
            id: "",
            name: "",
            ctime: "",
            list: [
                {id: 1, name: "奔驰", ctime: new Date()},
                {id: 2, name: "众泰", ctime: new Date()},
                {id: 3, name: "奥拓", ctime: new Date()},
            ]
        },
        methods: {
            add() {
                /*
                获取到id和name,直接从data上面获取
                组织出一个对象
                把这个对象,调用数组的方法,添加到data上的list中
                */
                var car = {id: this.id, name: this.name, ctime: new Date()}
                this.list.push(car)
                this.id = this.name = ""
            },
            del(id) {
                /*
                * 如何根据ID,找到要删除的信息的索引
                *
                * 如果找到索引了,直接调用数组的splice方法
                * */
                this.list.some((item, i) => {
                    if (item.id == id) {
                        //    在数组的some方法中,如果return true ,就会立即终止这个数组的后续循环
                        this.list.splice(i, 1)
                        return true
                    }
                })
            }
        }
    })
</script>
</body>
</html>

或者也可以写成这样

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>01品牌管理案例</title>
    <script src="lib/vue-2.4.0.js"></script>
    <link rel="stylesheet" href="lib/bootstrap-3.3.7.css">
</head>
<body>
<div id="app">
    <div class="panel panel-primary">
        <div class="panel-heading">
            <h3 class="panel-title">添加品牌</h3>
        </div>
        <!--form-inline 让自己写的盒子在同一行显示-->
        <div class="panel-body form-inline">
            <label>
                Id:
                <input type="text" v-model="id">
            </label>
            <label>
                Name:
                <input type="text" v-model="name">
            </label>
            <!--在Vue中,添加事件绑定机制,如果给事件添加了小括号,那么就可以传参了-->
            <input type="button" value="添加" class="btn btn-primary" v-on:click="add">
        </div>
    </div>
    <table class="table table-bordered table-hover table-striped">
        <thead>
        <tr>
            <th>Id</th>
            <th>Name</th>
            <th>Ctime</th>
            <th>Operation</th>
        </tr>
        </thead>
        <tbody>
        <tr v-for="item in list" :key="item.id">
            <td>{{item.id}}</td>
            <td>{{item.name}}</td>
            <td>{{item.ctime}}</td>
            <td>
                <a href="#" v-on:click.prevent="del(item.id)">删除</a>
            </td>
        </tr>
        </tbody>
    </table>
</div>
<script>
    var vm = new Vue({
        el: "#app",
        data: {
            id: "",
            name: "",
            ctime: "",
            list: [
                {id: 1, name: "奔驰", ctime: new Date()},
                {id: 2, name: "众泰", ctime: new Date()},
                {id: 3, name: "奥拓", ctime: new Date()},
            ]
        },
        methods: {
            add() {
                /*
                获取到id和name,直接从data上面获取
                组织出一个对象
                把这个对象,调用数组的方法,添加到data上的list中
                */
                var car = {id: this.id, name: this.name, ctime: new Date()}
                this.list.push(car)
                this.id = this.name = ""
            },
            del(id) {
                var index = this.list.findIndex(item => {
                    if (item.id == id) {
                        return true;
                    }
                })
                this.list.splice(index,1)
            }
        }
    })
</script>
</body>
</html>

3、根据条件筛选品牌

<tbody>
      <tr v-for="item in search(searchName)">
        <td>{{item.id}}</td>
        <td>{{item.name}}</td>
        <td>{{item.ctime}}</td>
        <td>
          <a href="#" @click.prevent="del(item.id)">删除</a>
        </td>
      </tr>
    </tbody>

  • search 过滤方法中,使用 数组的 filter 方法进行过滤:
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>01品牌管理案例</title>
    <script src="lib/vue-2.4.0.js"></script>
    <link rel="stylesheet" href="lib/bootstrap-3.3.7.css">
</head>
<body>
<div id="app">
    <div class="panel panel-primary">
        <div class="panel-heading">
            <h3 class="panel-title">添加品牌</h3>
        </div>
        <!--form-inline 让自己写的盒子在同一行显示-->
        <div class="panel-body form-inline">
            <label>
                Id:
                <input type="text" v-model="id">
            </label>
            <label>
                Name:
                <input type="text" v-model="name">
            </label>
            <!--在Vue中,添加事件绑定机制,如果给事件添加了小括号,那么就可以传参了-->
            <input type="button" value="添加" class="btn btn-primary" v-on:click="add">
            <label>
                搜索关键字:
                <input type="text" class="form-control" v-model="keywords">
            </label>
        </div>
    </div>
    <table class="table table-bordered table-hover table-striped">
        <thead>
        <tr>
            <th>Id</th>
            <th>Name</th>
            <th>Ctime</th>
            <th>Operation</th>
        </tr>
        </thead>
        <tbody>
        <tr v-for="item in search(keywords)" :key="item.id">
            <td>{{item.id}}</td>
            <td>{{item.name}}</td>
            <td>{{item.ctime}}</td>
            <td>
                <a href="#" v-on:click.prevent="del(item.id)">删除</a>
            </td>
        </tr>
        </tbody>
    </table>
</div>
<script>
    var vm = new Vue({
        el: "#app",
        data: {
            id: "",
            name: "",
            ctime: "",
            keywords:"",
            list: [
                {id: 1, name: "奔驰", ctime: new Date()},
                {id: 2, name: "众泰", ctime: new Date()},
                {id: 3, name: "奥拓", ctime: new Date()},
            ]
        },
        methods: {
            add() {
                var car = {id: this.id, name: this.name, ctime: new Date()}
                this.list.push(car)
                this.id = this.name = ""
            },
            del(id) {
                var index = this.list.findIndex(item => {
                    if (item.id == id) {
                        return true;
                    }
                })
                this.list.splice(index,1)
            },
            search(keywords){
                var newList=this.list.filter(item=>{
                    if (item.name.includes(keywords)){
                        return item
                    }
                })
                        return newList
            }
        }
    })
</script>
</body>
</html>