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