美文网首页
vue实现增删改查--demo

vue实现增删改查--demo

作者: believedream | 来源:发表于2017-05-05 09:31 被阅读0次

    刚才看了一个vue的demo,可以实现增删改查,感觉十分经典,我在这里总结下;还是老规矩先要看看我们达到的目标;

    image.png

    就如上图,我们可以实现增删改查的功能;

    话不多说开始写代码:
    首先我们需要引入一些东西辅助我们

    //引入下我们需要的bootstrap
    <link href="Content/bootstrap/css/bootstrap.css" rel="stylesheet" />
        <style type="text/css">
            table thead tr th {
                text-align:center;
            }
        </style>
    

    我们还需要引入js

    //这是我们需要引入vue的插件
        <script src="Content/vue/dist/vue.js"></script>
    

    接下来介绍html的主要内容:

    //定义 id="app",由于后面的vue的绑定虚拟节点需要使用
    <div style="padding:20px;" id="app">
    //中间主要使用表格的方式,和使用bootstrap是表格变得漂亮点
            <div class="panel panel-primary">
                <div class="panel-heading">用户管理</div>
                <table class="table table-bordered table-striped text-center">
                    <thead>
                        <tr>
                            <th>用户名</th>
                            <th>年龄</th>
                            <th>毕业学校</th>
                            <th>备注</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
      //这里主要是vue的语法 , v-for是循环变
                        <template v-for="row in rows ">
    //渲染数据
                            <tr><td>{{row.Name}}</td><td>{{row.Age}}</td><td>{{row.School}}</td><td>{{row.Remark}}</td>
    绑定点击事件
                            <td><a href="#" @click="Edit(row)">编辑</a>&nbsp;&nbsp;<a href="#" @click="Delete(row.Id)">删除</a></td>
                            </tr>
                        </template>
    //v-model 与 angular 的 ng-model 很像 使用方式下能通
                        <tr>
                            <td><input type="text" class="form-control" v-model="rowtemplate.Name" /></td>
                            <td><input type="text" class="form-control" v-model="rowtemplate.Age" /></td>
                            <td><select class="form-control" v-model="rowtemplate.School">
                        <option>中山小学</option>
                        <option>复兴中学</option>
                        <option>光明小学</option>
                    </select></td>
                            <td><input type="text" class="form-control" v-model="rowtemplate.Remark" /></td>
    //继续绑定事件
                            <td><button type="button" class="btn btn-primary" v-on:click="Save">保存</button></td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
    

    现在分析js的主要内容:

    <script type="text/javascript">
            //Model  准备一些我们需要的数据
            var data = {
                rows: [
                { Id: 1, Name: '小明', Age: 18, School: '光明小学', Remark: '三好学生' },
                { Id: 2, Name: '小刚', Age: 20, School: '复兴中学', Remark: '优秀班干部' },
                { Id: 3, Name: '吉姆格林', Age: 19, School: '光明小学', Remark: '吉姆做了汽车公司经理' },
                { Id: 4, Name: '李雷', Age: 25, School: '复兴中学', Remark: '不老实的家伙' },
                { Id: 5, Name: '韩梅梅', Age: 22, School: '光明小学', Remark: '在一起' },
                ],
                rowtemplate: { Id: 0, Name: '', Age: '', School: '', Remark: '' }
            };
        //ViewModel 这是vue的特殊语法,我们需要绑定id,和我们需要使用的数据;
        var vue = new Vue({
            el: '#app',
            data: data,
    //methods,主要是写方法,方法在 ‘#app' 可以使用;
            methods: {
                Save: function (event) {
                    if (this.rowtemplate.Id == 0) {
                        //设置当前新增行的Id
                        this.rowtemplate.Id = this.rows.length + 1;
                        this.rows.push(this.rowtemplate);
                    }
                    
                    //还原模板
                    this.rowtemplate = { Id: 0, Name: '', Age: '', School: '', Remark: '' }
                },
                Delete: function (id) {
                    //实际项目中参数操作肯定会涉及到id去后台删除,这里只是展示,先这么处理。
                    for (var i=0;i<this.rows.length;i++){
                        if (this.rows[i].Id == id) {
                            this.rows.splice(i, 1);
                            break;
                        }
                    }
                },
                Edit: function (row) {
                    this.rowtemplate = row;
                }
            }
        });
        </script>
    

    接下来补充完整代码,复制就可以运行哟;

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title></title>
        <link href="Content/bootstrap/css/bootstrap.css" rel="stylesheet" />
        <style type="text/css">
            table thead tr th {
                text-align:center;
            }
        </style>
    </head>
    <body>
        <div style="padding:20px;" id="app">
            <div class="panel panel-primary">
                <div class="panel-heading">用户管理</div>
                <table class="table table-bordered table-striped text-center">
                    <thead>
                        <tr>
                            <th>用户名</th>
                            <th>年龄</th>
                            <th>毕业学校</th>
                            <th>备注</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        <template v-for="row in rows ">
                            <tr><td>{{row.Name}}</td><td>{{row.Age}}</td><td>{{row.School}}</td><td>{{row.Remark}}</td>
                            <td><a href="#" @click="Edit(row)">编辑</a>&nbsp;&nbsp;<a href="#" @click="Delete(row.Id)">删除</a></td>
                            </tr>
                        </template>
                        <tr>
                            <td><input type="text" class="form-control" v-model="rowtemplate.Name" /></td>
                            <td><input type="text" class="form-control" v-model="rowtemplate.Age" /></td>
                            <td><select class="form-control" v-model="rowtemplate.School">
                        <option>中山小学</option>
                        <option>复兴中学</option>
                        <option>光明小学</option>
                    </select></td>
                            <td><input type="text" class="form-control" v-model="rowtemplate.Remark" /></td>
                            <td><button type="button" class="btn btn-primary" v-on:click="Save">保存</button></td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
    <!--    <script src="http://code.jquery.com/jquery-latest.js"></script>-->
        <script src="Content/vue/dist/vue.js"></script>
        <script type="text/javascript">
            //Model
            var data = {
                rows: [
                { Id: 1, Name: '小明', Age: 18, School: '光明小学', Remark: '三好学生' },
                { Id: 2, Name: '小刚', Age: 20, School: '复兴中学', Remark: '优秀班干部' },
                { Id: 3, Name: '吉姆格林', Age: 19, School: '光明小学', Remark: '吉姆做了汽车公司经理' },
                { Id: 4, Name: '李雷', Age: 25, School: '复兴中学', Remark: '不老实的家伙' },
                { Id: 5, Name: '韩梅梅', Age: 22, School: '光明小学', Remark: '在一起' },
                ],
                rowtemplate: { Id: 0, Name: '', Age: '', School: '', Remark: '' }
            };
        //ViewModel
        var vue = new Vue({
            el: '#app',
            data: data,
            methods: {
                Save: function (event) {
                    if (this.rowtemplate.Id == 0) {
                        //设置当前新增行的Id
                        this.rowtemplate.Id = this.rows.length + 1;
                        this.rows.push(this.rowtemplate);
                    }
                    
                    //还原模板
                    this.rowtemplate = { Id: 0, Name: '', Age: '', School: '', Remark: '' }
                },
                Delete: function (id) {
                    //实际项目中参数操作肯定会涉及到id去后台删除,这里只是展示,先这么处理。
                    for (var i=0;i<this.rows.length;i++){
                        if (this.rows[i].Id == id) {
                            this.rows.splice(i, 1);
                            break;
                        }
                    }
                },
                Edit: function (row) {
                    this.rowtemplate = row;
                }
            }
        });
        </script>
    </body>
    </html>
    

    补充语句,在代码中我们使用了@click 与 v-on:click绑定事件,其实@方式是简写方式,两种并无区别;

    相关文章

      网友评论

          本文标题:vue实现增删改查--demo

          本文链接:https://www.haomeiwen.com/subject/snsjtxtx.html