Vue.js——3.增刪改查


vue  寫假后台  bootstrap 做的樣式   

 

 

代碼

<!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>Title</title>
    <script src="vue-dev/dist/vue.js"></script>
    <link rel="stylesheet" href="lib/bootstrap/css/bootstrap.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:
            </label>
            <input type="text" class="form-control" v-model="id">
            <label>
                Name:
            </label>
            <input type="text" class="form-control" v-model="name">
            <button type="button" class="btn" @click="add">添加</button>
            <button type="button" class="btn" @click="update">修改</button>
        </div>
    </div>

    <table class="table table-hover table-bordered table-striped">
        <thead>
        <tr>
            <th>Id</th>
            <th>Name</th>
            <th>Ctime</th>
            <th>Operation</th>
                    </tr>
        </thead>
        <tbody>
        <tr v-for="(item) in list">
            <td>{{item.id}}</td>
            <td>{{item.name}}</td>
            <td>{{item.ctime}}</td>
            <td><button type="button" class="btn btn-info" @click="bind(item.id)">修改</button>
                <button type="button" class="btn btn-warning" @click="del(item.id)">刪除</button>
            </td>
        </tr>
        </tbody>
    </table>
</div>
<script>
    let vm=new Vue({
        el:'#app',//當前我們Vue實例 要控制的標簽
        data:{//數據
            id:'',
            name:'',
            index:'',//用於修改
            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=''
            },
            update(){
                if (this.index==""){
                    alert('未選擇數據')
                    return
                }
                // 找到索引
                this.list[this.index].id=this.id
                this.list[this.index].name=this.name
                this.list[this.index].ctimec=new Date()
                this.id=this.name=this.index=''

            },

            bind(id){
                //根據id 找索引
                this.list.some((item,i)=>{
                    if (item.id==id){
                        this.id=item.id;
                        this.name=item.name
                        this.index=i
                        alert(i)
                        return true;
                    }
                })
            },
            del(id){
                //根據id找索引
               let index= this.list.findIndex(item =>{
                   if (item.id==id){
                       return true
                   }
               })
                //刪除數組元素
                this.list.splice(index,1)
            }

        }
    })
</script>
</body>
</html>

  

總結:

還可以 bootstarp 還不熟練     好用是真的  一起666666


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM