vue自學小demo----前端

vue學習的小demo,實現簡單的頁面項目的增刪

 

 

代碼如下

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Vue</title>
    <script src="./vue.js"> </script>
    <link rel="stylesheet" href="./bootstrap.css">
</head>
   <body>
       
      <div id="app" > 
         <dive class="panel panel-primary">
         <div class="panel-heading"> 
             <h3 class="panel-title">添加品牌</h3>
         </div>
        <dive 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="ok">
                <label >
                        搜索:<input type="text"  class="form-control" v-model="keywords">
                    </label>
       
            <!-- form---inline 使得顯示一行 -->
         </dive>
         </dive>
          <table class="table table-bordered table-hover table-striped">
              <thead>
                  <tr>
                      <th>id</th>
                      <th>名字</th>
                      <th>ctime</th>
                      <th>opertation</th>
                      
                  </tr>
              </thead>
              <tbody>
                    <thead>
                            <tr v-for="item in serrch(keywords)" :key="item.id">
                                <th>{{item.id}}</th>
                                <th>{{item.name}}</th>
                                <th>{{item.ctime}}</th>
                                <th><a href="" @click.prevent="del(item.id)">刪除 </a></th>
                            </tr>
                        </thead>
              </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()}
          ]  
            },
            methods: {
                ok(){
                    // 2console.log("ok")
                    var add2={id:this.id,name:this.name,ctime: new Date() }
                    this.list.push(add2),
                    this.id=this.name=''
                }, 
                   
                del(id){ 
                    // this.list.some((item,i)=>{
                    //     if(item.id==id ){
                    //         this.list.splice(i,1)
                    //     }
                    // })
                    var i=this.list.length
                    var j=0
                    for(j=0;j<i;j++){
                        if(this.list[j].id==id){
                            this.list.splice(j,1)
                        }}},
               serrch(keywords){
                   var newlist=[]
                   this.list.forEach(item=>{
                       if(item.name.indexOf(keywords)!=-1)  {
                     
                           newlist.push(item)
                       }
                   })
                   return newlist
               }
            }
        });
      </script>
   </body>
</html> 

 

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