实现vue中 固定数据的简单的增删改查

本文介绍如何在Vue应用中实现针对固定数据源的简单增删改查操作,包括基本的 CRUD 功能实现步骤和关键代码展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实现vue中 固定数据的增删改查

<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="js/jquery.js" type="text/javascript" charset="utf-8"></script>
    <script src="js/vue.js" type="text/javascript" charset="utf-8"></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) {
				//这个id==0说明不是处于编辑状态
                if (this.rowtemplate.Id == 0&&this.rowtemplate.Name!='') {
                    //设置当前新增行的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>
Vue前端项目中实现增删改查功能,并且后端数据写死的情况下,可以模拟出一套完整的CRUD操作流程。下面是一个简单的介绍: ### 前提准备 由于后端数据固定的(即“写死”),我们可以在前端通过JavaScript数组的形式直接存储和管理数据。 --- ### 实现步骤 #### 1. 创建Vue组件结构 创建一个包含表单输入、按钮以及表格展示的界面布局。例如: ```html <template> <div> <!-- 表单区域 --> <form @submit.prevent="addData"> 名称:<input v-model="newItem.name" /> 年龄:<input v-model="newItem.age" type="number" /> <button>添加</button> </form> <!-- 数据列表 --> <table border="1"> <thead> <tr><th>ID</th><th>名称</th><th>年龄</th><th>操作</th></tr> </thead> <tbody> <tr v-for="(item, index) in list" :key="index"> <td>{{ item.id }}</td> <td>{{ item.name }}</td> <td>{{ item.age }}</td> <td> <button @click="edit(item)">编辑</button> <button @click="del(index)">删除</button> </td> </tr> </tbody> </table> </div> </template> ``` --- #### 2. 定义数据模型及初始化方法 在`data()`函数中声明必要的变量,并提供初始的数据源。 ```javascript <script> export default { data() { return { newItem: { name: "", age: null }, // 新增项临时保存区 editIndex: -1, // 当前编辑条目的索引,默认值表示未选择 list: [ // 初始化假定已有的固定数据集 { id: 1, name: "张三", age: 20 }, { id: 2, name: "李四", age: 25 } ] }; }, methods: { addData() { if (this.newItem.name && this.newItem.age !== null) { const maxId = Math.max(...this.list.map(item => item.id), 0); this.list.push({ id: maxId + 1, ...this.newItem }); this.resetForm(); // 清空表单项内容 } else alert("请完整填写信息!"); }, del(index) { this.list.splice(index, 1); // 直接移除对应位置的对象元素 }, edit(item) { Object.assign(this.newItem, JSON.parse(JSON.stringify(item))); // 深拷贝一份到新对象内供修改 this.editIndex = this.list.indexOf(item); }, resetForm() { this.newItem = { name: "", age: null }; // 还原默认状态为空白段 this.editIndex = -1; // 编辑模式退出标记复位 } } }; </script> ``` 上述代码实现了基本的功能需求:用户可通过交互式页面完成对虚拟数据库记录的操作——包括增加一条新的记录、更新已有记录的信息、从集合里剔除指定成员等动作。 --- ### 总结说明 这种做法非常适合学习阶段或者快速原型设计场景下测试前后分离架构下的业务逻辑验证过程。尽管这里的后台服务并没有真正部署起来,但是借助于客户端内存中的伪持久化机制仍然能够很好地演示整个工作流的所有环节。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值