欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

品牌案例-Vue完成数据的添加,删除以及数据过滤功能

程序员文章站 2022-03-03 09:12:29
...
<!DOCTYPE html>
<html lang="en" xmlns:v-model="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <div id="app">
        <div class="panel panel-primary">
            <div class="panel-heading">
                <h3 class="panel-title">添加品牌</h3>
            </div>
            <div class="panel-body form-inline">
                <label>
                    Id:
                    <input type="text" class="form-control" v-model="id">
                </label>
                <label>
                    Name:
                    <input type="text" class="form-control" v-model="name">
                </label>

                <input type="button" value="添加" class="btn btn-primary" @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="" @click.prevent="del(item.id)">删除</a></td>
                </tr>
            </tbody>
        </table>
    </div>

    <script>
        var vm = new Vue({
            el: '#app',
            data: {
                id: '',
                name: '',
                keywords: '',
                list: [
                    { id: 1, name: '奔驰', ctime: new Date()},
                    { id: 2, 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){
                    /*this.list.some((item, i) => {
                        if(item.id = id){
                            this.list.splice(i,1)
                            return
                        }
                    })*/
                    var index = this.list.findIndex(item => {
                        if(item.id == id){
                            return true
                        }
                    })
                    this.list.splice(index,1)
                },
                search(keywords){
                    /*var newList = []
                    this.list.forEach(item => {
                        if(item.name.indexOf(keywords) != -1){
                            newList.push(item)
                        }
                    })
                    return newList*/
                    var newList = this.list.filter(item => {
                        if(item.name.includes(keywords)){
                            return true
                        }
                    })
                    return newList
                }
            }
        })
    </script>

</body>
</html>