vue simple student information management case

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>学生信息管理</title>
    <link rel="stylesheet" href="./lib/bootstrap.css">
    <script src="./lib/vue.js"></script>
    <style type="text/css">
        #app{
            margin: 10px;
        }
    </style>
</head>
<body>
<div id="app">
    <form class="form-inline">
        <div class="form-group">
            <label>学号:</label>
            <input type="text" class="form-control" v-model="stuNo">
        </div>&nbsp;&nbsp;
        <div class="form-group">
            <label>姓名:</label>
            <input type="email" class="form-control" v-model="name" @keyup.enter="add">
        </div> 
        <input type="button" class="btn btn-primary" value="添加" @click="add">
            
        <div class="form-group">
            <<>label</Search Keyword Name:>label
            input type="email" class="form-control" v-model="keywords" @keyup.enter="search(keywords)" v-focus>
        </div>
    </form>
    <br/>
    <table class="table table-bordered" >
        <thead>
        <th>学号</th>
        <th>姓名</th>
        <th>添加时间</th>
        <th>操作</th>
        </thead>
        <tbody v-for="(item,i) in search(keywords)" :key="item.stuNo" >
        <tr>
            <td>{{item.stuNo}}</td>
            <td>{{item.name}}</td>
            <td>{{item.cTime | dateFormat}}</td>
            <td><a href="" @click.prevent="del(item.stuNo)">Remove </ A > </ TD > 
        </ TR > 
        </ tbody > 
    </ Table > 
</ div > 

< Script > 
    // custom focus automatically acquired global instruction 
    Vue.directive ( ' Focus ' , {
         // when bound when the element is inserted into the DOM ...... 
        inserted the: function (EL) {
             // focused element 
            el.focus () 
        } 
    }) 
    var VM =  new new Vue ({ 
        EL: ' #app ',
        data:{
            stuNo:'',
            name:'',
            keywords:'',
            list:[
                {
                    stuNo:1710204016,
                    name:'刘小红',
                    cTime:new Date()
                },
                {
                    stuNo:1710204007,
                    name:'李大明',
                    cTime:new Date()
                }
            ]
        },
        methods:{
            add(){
                var newInfo = {stuNo:this.stuNo, name:this.name, cTime:new Date()}
                this.list.push(newInfo)
                this.stuNo=this.name=''
            },
            del(stuNo){
                this.list.some((item,i)=>{
                    if(item.stuNo===stuNo){
                        this.list.splice(i,1)
                        return true;
                    }
                })
            },
            search(keywords){
                // var newList = []
                // this.list.forEach(item=>{
                //     if(item.name.indexOf(keywords)!=-1){
                //         newList.push(item)
                //     }
                // })
                // return newList
               return this.list.filter(item=>{
                    if(item.name.includes(keywords)){
                        return item
                    }
                })
            }
        },
        filters:{
            dateFormat:function(dateStr){
                var year = dateStr.getFullYear()
                var mouth = (dateStr.getMonth() + 1).toString().padStart(2,'0')
                var date = (dateStr.getDate()).toString().padStart(2,'0')
                var h = (dateStr.getHours()).toString().padStart(2,'0')
                var m = (dateStr.getMinutes()).toString().padStart(2,'0')
                var s = (dateStr.getSeconds()).toString().padStart(2,'0')
                return `${year}-${mouth}-${date} ${h}:${m}:${s}`
            }
        }
    })
</script>
</body>
</html>

 

Guess you like

Origin www.cnblogs.com/zhahuhu/p/11618599.html