vue增删改查(示例)

html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.staticfile.org/vue/2.7.0/vue.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
</head>
<body>
    <div id="box">
       
        <div class="head1">
            <p><span>用户权限/</span>用户列表</p>
        </div>
        <div class="bottom">
            <p><span><button id="tianjia" @click="tianjia">添加</button></span>
                <span>角色 <input type="text" id="ipt"placeholder="全部" v-model="ipt"></span>
                <span><button id="sousuo"@click="sousuo()">搜索</button></span>
                <span>
                            <input id="ipt1" type="text" placeholder="修改后称昵" v-model="xgcn">
                            <input id="ipt1" type="text" placeholder="修改后用户" v-model="xgyh">
                            <input id="ipt1" type="text" placeholder="修改后角色" v-model="xgjs">
                            <button id="xgbc" @click="qrbc()">点击修改保存</button>
                 </span>
            </p>
            <table id="tab" border="1">
                <tr id="tr1">
                    <th>序号</th>
                    <th>称昵</th>
                    <th>用户名</th>
                    <th>角色</th>
                    <th>创建时间</th>
                    <th>最近修改时间</th>
                    <th>管理</th>
                </tr>
                <tbody id="tbd">
                    <tr v-for="(item,index) in str1==0?arr:arr3">
                        <td>{{item.id}}</td>
                        <td>{{item.chengni}}</td>
                        <td>{{item.yonghuming}}</td>
                        <td>{{item.jiaose}}</td>
                        <td>9.27</td>
                        <td>9.27</td>
                        <td><button class="xiugai" @click="xiugai(index)">修改</button>
                            <button class="shanchu" @click="del(index)">删除</button></td>
                    </tr>
                </tbody>
            </table>
        </div>
        <div class="bottom1" v-show="show1">
            <input type="text" placeholder="称昵" v-model="chengni"><br>
            <input type="text" placeholder="用户名" v-model="yonghuming"><br>
            <input type="text" placeholder="角色" v-model="jiaose"><br>
            <button id="queren" @click="queren">确认添加</button><button id="quxiao" @click="quxiao">取消</button>
        </div>
    </div>
</body>

</html>

style:

<style>
    * {
        margin: 0;
        padding: 0;
    }

    ul li {
        list-style: none;
    }

    body {
        background: #e7e6e6;
    }

    .head1 {
        margin-left: 20px;
        margin-top: 10px;
        margin-bottom: 10px;
    }

    .head1 p span {
        color: #767676;
    } 

    .bottom {
        width: 92rem;
        height: 700px;
        background: #fff;
        margin-top: 20px;
        margin: auto;
        position: relative;
    }

    .bottom p {
        padding-top: 20px;
    }

    .bottom p span {
        margin-left: 25px;
    }

    .bottom p span button {
        width: 70px;
        height: 35px;
        background: #0076fb;
        color: #fff;
        border-radius: 10px;
        border: none;
    }

    #ipt {
        width: 400px;
        height: 32px;
        border: 1px solid #ccc;
        outline: none;
        border-radius: 5px;
    }
    #ipt1{
        width: 100px;
        height: 32px;
        border: 1px solid #ccc;
        outline: none;
        border-radius: 5px;
        margin-left: 5px;
        margin-right: 5px;
    }
    #tab {
        width: 1400px;
        height: 30px;
        margin-left: 25px;
        margin-top: 10px;
        border-collapse: collapse;
    }

    .bottom1 {
        width: 400px;
        height: 300px;
        background: rgba(0, 0, 0, 0.3);
        border-radius: 10px;
        position: absolute;
        top: 190px;
        left: 500px;
        /* display: none; */
    }

    .bottom1 input {
        width: 200px;
        height: 40px;
        border: none;
        margin-left: 100px;
        margin-top: 15px;
        border-radius: 10px;
    }

    .bottom1 button {
        width: 80px;
        height: 40px;
        border: none;
        margin-left: 80px;
        margin-top: 40px;
        border-radius: 10px;
    }

    #queren {
        background: #0076fb;
        color: #fff;
    }

    #quxiao {
        background: #767676;
        color: #fff;
    }

    #tbd tr td {
        height: 50px;
        text-align: center;
    }

    .xiugai {
        width: 80px;
        height: 40px;
        border: none;
        color: #fff;
        background: green;
        border-radius: 20px;
    }

    .shanchu {
        width: 80px;
        height: 40px;
        border: none;
        color: #fff;
        background: rgb(158, 8, 8);
        border-radius: 20px;
        margin-left: 10px;
    }
    #xgbc{
        width: 100px;
    }
    table tr:nth-child(odd){
        background: rgb(242, 144, 144);
    }
    table tr:nth-child(even){
        background: rgb(149, 149, 237);
    }
    table tr:hover{
        background: rgb(228, 228, 153);
    }
    #tr1{
        background: #fff;
    }
</style>

script:

<script>
        new Vue({
            el: '#box',
            data() {
                return {
                    show1: false,
                    chengni: '',
                    yonghuming: '',
                    jiaose: '',
                    ipt: '',
                    str1:0,
                    arr3:'',
                    xgcn:'',
                    xgyh:'',
                    xgjs:'',
                    index:'',   //点击修改后,把修改的下标传进来,(这里的index是它的空值)
                    arr: [{
                            id: 1,
                            chengni: 'zhangsa',
                            yonghuming: 12,
                            jiaose: 123
                        },
                        {
                            id: 1,
                            chengni: 'zhangsa',
                            yonghuming: 12,
                            jiaose: 123
                        },
                        {
                            id: 1,
                            chengni: 'zhangsa',
                            yonghuming: 12,
                            jiaose: 123
                        }
                    ]
                }
            },
            methods: {
        // 点击添加显示
                tianjia() {
                    this.show1 = true
                },
        // 点击取消隐藏
                quxiao() {
                    this.show1 = false
                },
        // 确认添加
                queren() {
                    let obj = {
                        id: 1,
                        chengni: this.chengni,
                        yonghuming: this.yonghuming,
                        jiaose: this.jiaose,

                    }
                    this.arr.push(obj)
                },
        // 删除
                del(a) {
                    if (confirm('确定要删除吗')==true){
                        this.arr.splice(a, 1) 
                    }else{
                        return false;
                    }
                    
                },
        // 修改
                xiugai(index){
                    this.index = index
                    this.xgcn= this.arr[index].chengni
                    this.xgyh= this.arr[index].yonghuming
                    this.xgjs= this.arr[index].jiaose
                },

        // 点击确认保存
        qrbc(){
            this.arr[this.index].chengni=this.xgcn
            this.arr[this.index].yonghuming=this.xgyh
            this.arr[this.index].jiaose=this.xgjs
            
        },             
        // 查询
                sousuo() {
                    if (this.ipt == '') {
                this.str1 = 0
                return
            }
            this.str1 = 2
            this.arr3 = this.arr.filter((res) => {
                return res.chengni.includes(this.ipt)
            })
            console.log(this.ipt);
        }

            },
        })
    </script>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮你解答这个问题。 在 Vue 中实现增删改查操作,通常需要在组件中定义对应的方法,然后在模板中调用这些方法。下面是一个简单的示例,演示了如何在 Vue 中实现增删改查操作: ```html <template> <div> <h2>用户列表</h2> <ul> <li v-for="(user, index) in users" :key="user.id"> {{ user.name }} <button @click="editUser(index)">编辑</button> <button @click="deleteUser(index)">删除</button> </li> </ul> <h2>添加用户</h2> <form @submit.prevent="addUser"> <label> 姓名: <input type="text" v-model="newUser.name"> </label> <label> 年龄: <input type="text" v-model="newUser.age"> </label> <button type="submit">添加</button> </form> </div> </template> <script> export default { data() { return { users: [ { id: 1, name: '张三', age: 20 }, { id: 2, name: '李四', age: 22 }, { id: 3, name: '王五', age: 25 }, ], newUser: { name: '', age: '', }, editingIndex: -1, } }, methods: { addUser() { if (this.newUser.name && this.newUser.age) { const id = this.users.length + 1 this.users.push({ id, ...this.newUser }) this.newUser = { name: '', age: '' } } else { alert('请输入完整信息') } }, editUser(index) { this.newUser = { ...this.users[index] } this.editingIndex = index }, saveUser() { if (this.newUser.name && this.newUser.age) { this.users.splice(this.editingIndex, 1, this.newUser) this.newUser = { name: '', age: '' } this.editingIndex = -1 } else { alert('请输入完整信息') } }, cancelEdit() { this.newUser = { name: '', age: '' } this.editingIndex = -1 }, deleteUser(index) { this.users.splice(index, 1) }, }, } </script> ``` 在上面的代码中,我们定义了一个包含增删改查操作的用户列表组件。其中,`users` 数组存储了所有用户信息,`newUser` 对象用于添加或编辑用户时输入的信息,`editingIndex` 变量用于记录当前正在编辑的用户的索引。 在模板中,我们使用 `v-for` 指令循环渲染用户列表,并为每个用户渲染一个编辑和删除按钮。当用户点击编辑按钮时,我们会调用 `editUser` 方法,将当前用户的信息赋值给 `newUser` 对象,并将 `editingIndex` 设置为当前用户的索引。当用户点击删除按钮时,我们会调用 `deleteUser` 方法,从 `users` 数组中删除对应的用户。 在添加用户部分,我们使用 `v-model` 指令将输入框的值绑定到 `newUser` 对象上,并在表单提交时调用 `addUser` 方法,将 `newUser` 对象添加到 `users` 数组中。 在编辑用户部分,我们在 `newUser` 对象中存储当前正在编辑的用户的信息,并将 `editingIndex` 设置为当前用户的索引。当用户点击保存按钮时,我们会调用 `saveUser` 方法,将 `newUser` 对象更新到 `users` 数组中,并清空 `newUser` 对象和 `editingIndex` 变量。当用户点击取消按钮时,我们会调用 `cancelEdit` 方法,将 `newUser` 对象清空,并将 `editingIndex` 变量重置为 -1。 这就是一个简单的 Vue 增删改查示例,希望对你有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值