<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <script src="../libs/vue1026.js"></script> </head> <style> #app { width: 800px; margin: 20px auto; } #tb { width: 800px; border-collapse: collapse; margin: 20px auto; } #tb th { background-color: #0094ff; color: white; font-size: 16px; padding: 5px; text-align: center; border: 1px solid black; } #tb td { padding: 5px; text-align: center; border: 1px solid black; } </style> <body> <div id="app"> <input type="text" v-model="name"> <button @click="addData">添加數據</button><br> <input type="text" placeholder="請輸入篩選內容" v-model="uname"> <table id="tb"> <tr> <th>編號</th> <th>名稱</th> <th>時間</th> <th>操作</th> </tr> <tr v-if="list.length==0"> <td colspan="4">當前已經沒有數據</td> </tr> <tr v-for="item in list | filterBy uname in 'name'"> <td>{{$index+1}}</td> <td>{{item.name}}</td> <td>{{item.ctime}}</td> <td><a href="javascript:;" @click="remove($index)">刪除</a></td> </tr> </table> </div> </body> <script> new Vue({ el:'#app', data:{ list:[ {name:'寶馬',ctime:new Date()}, {name:'奔馳',ctime:new Date()} ], name:'', uname:'' }, methods:{ addData:function(){ var str = {name:this.name,ctime:new Date()}; this.list.push(str); this.name=""; }, remove:function(id){ this.list.splice(id,1) } } }) </script> </html>
