直接贴代码:
<!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> <link rel="stylesheet" href="./lib/bootstrap.min.css"> <script src="./lib/vue.min.js"></script> </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: <input type="text" class="form-control" v-model="id"> </label> <label for=""> Name: <!-- 按键修饰符.后面可以跟码值 --> <input type="text" class="form-control" v-model="name" @keyup.enter="add"> </label> <input type="button" value="添加" class="btn btn-primary" @click="add"> <label for=""> 搜索名称关键字: <input type="text" class="form-control" v-model="keywords"> </label> </div> </div> <table class="table table-bordered table-hover table-strip"> <thead> <tr> <th>Id</th> <th>Name</th> <th>Ctime</th> <th>Operation</th> </tr> </thead> <tbody> <tr v-for="item in search(keywords)" :key="item.id"> <td>{{item.id}}</td> <td>{{item.name}}</td> <td>{{item.ctime | dateFormat(pattern="")}}</td> <td><a href="#" @click="del(item.id)">删除</a></td> </tr> </tbody> </table> </div> <div id="app2"> {{dt|dateFormat(pattern='')}} <!-- 优先调用私有过滤器 --> </div> <script> // 自定义全局按键修饰符 Vue.config.keyCodes.f2=113 // 全局过滤器应该放在相关的vue实例前面 Vue.filter('dateFormat',function(dateStr,pattern=''){ var dt=new Date(dateStr) var y=dt.getFullYear() // padStart()是es6用来填充字符串的,前一个参数用来定义有字符创的长度,后一个用来定义用什么填充 var m=(dt.getMonth()+1).toString().padStart(2,'0'); var d=dt.getDate() if(pattern.toLowerCase0==='yyy-mm-dd'){ return `${y}-${m}-${d}` }else{ var h=dt.getHours() var min=dt.getMinutes() var s=dt.getSeconds() return `${y}-${m}-${d} ${h}:${min}:${s}` } //return y+'-'+m+'-'+d }) var vm=new Vue({ el:'#app', data:{ id:'', name:'', keywords:'', list:[ {id:1,name:'奔驰',ctime:new Date()}, {id:2,name:'宝马',ctime:new Date()} ] }, methods:{ add(){ var obj={}; obj.id=this.id; obj.name=this.name; obj.ctime=new Date(); this.list.push(obj); }, del(id){ this.list.some((item,i)=>{//删除数组中执行操作后返回值为true的项 if(item.id==id){ this.list.splice(i,1) return true; } }) //var index=this.list.findIndex(item=>{//遍历数组,并返回满足条件返回true的数组项的索引 // if(item.id==id){ //return true; //} //}) //this.list.splice(index,1) }, search(keywords){ var newList=[] this.list.forEach(item=>{ if(item.name.indexOf(keywords)!=-1){//空字符串包含在所有字符串中,因此空值会返回所有列表 newList.push(item) } }) return newList; // var newList=this.list.filter(item=>{ // if(item.name.includes(keywords)){//es6新方法 // return item; // } // }) // return newList; } } }) var vm2=new Vue({ el:"#app2", data:{ dt:new Date() }, methods:{}, // 定义私有过滤器 filters:{ dateFormat:function(dateStr,pattern=''){ var dt=new Date(dateStr) var y=dt.getFullYear() var m=dt.getMonth()+1 var d=dt.getDate() if(pattern.toLowerCase0==='yyy-mm-dd'){ return `${y}-${m}-${d}` }else{ var h=dt.getHours() var min=dt.getMinutes() var s=dt.getSeconds() return `${y}-${m}-${d} ${h}:${min}:${s}~~~~` } } } }) </script> </body> </html>
效果图: