Vue教程05(综合小案例)

小案例

实现对表单数据的添加,删除和关键字查询的操作

表单布局

使用bootstrap来设置table,在vscode中安装bootstrap插件

删除记录

删除记录的实现逻辑:点击删除链接,获取要删除记录的id,然后调用数组的splice方法来移除记录,
==注意:方法名称不要使用delete! ==
数组常用的循环方法比较

循环方法 说明
forEach 不可终止循环
some 返回true终止循环
findIndex 返回true可以终止循环,返回满足条件的索引
filter 过滤数组,返回过滤后的数组

通过数组的some方法来循环判断

del(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);
	}

综合案例 源码如下:

<!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>Document</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>
            <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>操作</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()
                }, {
                    id: 3,
                    name: '王五',
                    cTime: new Date()
                }, {
                    id: 4,
                    name: '小明',
                    cTime: new Date()
                }]
            },
            methods: {
                add() {
                    this.list.push({
                        id: this.id,
                        name: this.name,
                        cTime: new Date()
                    });
                    this.id = this.name = '';
                },
                del(id) {
                    // alert(id);
                    this.list.some((item, i) => {
                        if (item.id == id) {
                            this.list.splice(i, 1);
                            return true;
                        }
                    })
                },
                search(keywords) {
                    // 保存新的数组
                    // var newList = []
                    // this.list.forEach(item => {
                    //         // 判断循环的记录是否包含的查询的关键字
                    //         if (item.name.indexOf(keywords) != -1) {
                    //             // 将循环的记录添加到新的数组中
                    //             newList.push(item)
                    //         }
                    //     })
                    //     // 返回数组信息
                    // return newList
                    return this.list.filter(item => {
                        if (item.name.includes(keywords)) {
                            return item
                        }
                    })
                }

            }
        })
    </script>
</body>

</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章