Vue实现学生信息录入系统

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>学生信息录入</title>
<style>
*{
     
    margin: 0;
    padding: 0;
    list-style: none;
    text-decoration: none;
    box-sizing: border-box;
}
#app{
     
    width: 1000px;
    background-color: #eee;
    margin: 0 auto;
    position: relative;
    min-height: 600px;
    padding-top: 50px;
}
.add{
     
    position: absolute;
    right: 10px;
    top: 10px;
    width: 50px;
    height: 30px;
    background-color: tomato;
    border: none;
    outline: none;
}
.addInfo{
     
    /* display: flex; */
    /* justify-content: center; */
    padding: 5px;
    width: 500px;
    height: 300px;
    border: 1px solid rebeccapurple;
    position: fixed;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
    background-color: lightcyan;

}
.addInfo p{
     
    text-align: left;
    display: block;
    width: 400px;
    height: 50px;
}
.addInfo p input{
     
    outline: none;
    border: 1px solid #ff6700;
}
.addInfo p:last-child input{
     
    width: 80px;
    height: 40px;
    margin: 0 20px;
    background-color: lightseagreen;
    outline: none;
    border: none;
}
.addInfo p:last-child{
     
    /* height: 30px; */
    margin-left: 50px;
    text-align: center;
}
.editInfo{
     
    /* display: flex; */
    /* justify-content: center; */
    padding: 5px;
    width: 500px;
    height: 300px;
    border: 1px solid rebeccapurple;
    position: fixed;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
    background-color: lightcyan;

}
.editInfo p{
     
    text-align: left;
    display: block;
    width: 400px;
    height: 50px;
}
.editInfo p input{
     
    outline: none;
    border: 1px solid #ff6700;
}
.editInfo p:last-child input{
     
    width: 80px;
    height: 40px;
    margin: 0 20px;
    background-color: lightseagreen;
    outline: none;
    border: none;
}
.editInfo p:last-child{
     
    /* height: 30px; */
    margin-left: 50px;
    text-align: center;
}
table{
     
    width: 600px;
    border: 2px solid lightseagreen;
    text-align: center;
    margin:0 auto;
}
table tbody input{
     
    width: 45px;
    height: 25px;
    outline: none;
    border: 1px solid green;
    font-size: 18px;
}
table tbody td:nth-child(6) input{
     
    background-color: lightgreen;
    font-size: 14px;
    color: #333;
}
table tbody td:nth-child(7) input{
     
    background-color: tomato;
    color: #eee;
    font-size: 14px;
}
</style>
</head>
<body>
    <div id="app">
        <button class="add" @click="showAddInfo">添加</button>
        <div class="addInfo" v-if="isShow1">
            <p>
                <label for="name">姓名 :</label>
                <input id="name" type="text" v-model="name">
            </p>
            <p>
                <label for="sex">性别 :</label>
                <label for="sex"></label>
                <input v-model="gender" value="男" name="sex" type="radio">
                <label for="sex"></label>
                <input v-model="gender" value="女" name="sex" type="radio">
            </p>
            <p>
                <label for="birthday">出生年月 :</label>
                <input id="birthday" type="text" v-model="birthday">
            </p>
            <p>
                <label for="education">学历 :</label>
                <input id="education" type="text" v-model="education">
            </p>
            <p>
                <label for="remark">备注 :</label>
                <input id="remark" type="text" v-model="remark">
            </p>
            <p>
                <input type="button" value="添加" @click="addStudentInfo">
                <!-- <input type="button" value="确定修改" @click="editConfirm"> -->
                <input type="button" value="取消" @click="hideAddInfo1">
            </p>
        </div>
        <div class="editInfo" v-if="isShow2">
            <p>
                <label for="name">姓名 :</label>
                <input id="name" type="text" v-model="name">
            </p>
            <p>
                <label for="sex">性别 :</label>
                <label for="sex"></label>
                <input v-model="gender" value="男" name="sex" type="radio">
                <label for="sex"></label>
                <input v-model="gender" value="女" name="sex" type="radio">
            </p>
            <p>
                <label for="birthday">出生年月 :</label>
                <input id="birthday" type="text" v-model="birthday">
            </p>
            <p>
                <label for="education">学历 :</label>
                <input id="education" type="text" v-model="education">
            </p>
            <p>
                <label for="remark">备注 :</label>
                <input id="remark" type="text" v-model="remark">
            </p>
            <p>
                <input type="button" value="修改" @click="editConfirm">
                <!-- <input type="button" value="确定修改" @click="editConfirm"> -->
                <input type="button" value="取消" @click="hideAddInfo2">
            </p>
        </div>
        <div class="showInfo">
            <table>
                <thead>
                    <th>姓名</th>
                    <th>性别</th>
                    <th>出生年月</th>
                    <th>学历</th>
                    <th>备注</th>
                    <th>修改</th>
                    <th>删除</th>
                </thead>
                <tbody>
                    <tr v-for="(item,index) in students" :key="index">
                        <td>{
     {
     item.name}}</td>
                        <td>{
     {
     item.gender}}</td>
                        <td>{
     {
     item.birthday}}</td>
                        <td>{
     {
     item.education}}</td>
                        <td>{
     {
     item.remark}}</td>
                        <td><input type="button" value="修改" @click="editInfo(item,index)"></td>
                        <td><input type="button" value="删除" @click="remove(item,index)"></td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
<script src="../../vue.js"></script>
<script>
    var app = new Vue({
     
        el:"#app",
        data:{
     
            isShow1:false,
            isShow2:false,
            students:[{
     
                "id":"1",
                "name":"小明",
                "gender":"男",
                "birthday":"1997年6月",
                "education":"本科",
                "remark":"无"
            },
            {
     
                "id":"2",
                "name":"小红",
                "gender":"女",
                "birthday":"1920年6月",
                "education":"专科",
                "remark":"无"
            },
            {
     
                "id":"3",
                "name":"小白",
                "gender":"男",
                "birthday":"2000年6月",
                "education":"本科",
                "remark":"无"
            }],
            index:0,
            name:"",
            id:'',
            gender:"男",
            birthday:"",
            education:"",
            remark:"",
        },
        methods:{
     
            showAddInfo(){
     
                this.isShow1 = true;
            },
            hideAddInfo1(){
     
                this.isShow1 = false;
                this.name = "";
                this.gender = "男";
                this.birthday = "";
                this.education = "";
                this.remark = "";
            },
            showAddInfo2(){
     
                this.isShow2 = true;
            },
            hideAddInfo2(){
     
                this.isShow2 = false;
                this.name = "";
                this.gender = "男";
                this.birthday = "";
                this.education = "";
                this.remark = "";
            },
            addStudentInfo(){
     
                this.students.push({
     
                    "name":this.name,
                    "gender":this.gender,
                    "birthday":this.birthday,
                    "education":this.education,
                    "remark":this.remark
                    })
                this.name = "";
                this.gender = "男";
                this.birthday = "";
                this.education = "";
                this.remark = "";
                this.hideAddInfo1();
            },
            editConfirm(){
     
                //数组必须使用Vue重写的方法,有七个方法
                // push()
                // pop()
                // shift()
                // unshift()
                // splice()
                // sort()
                // reverse()
                this.students[this.index].name = this.name;
                this.students[this.index].gender = this.gender;
                this.students[this.index].birthday = this.birthday;
                this.students[this.index].education = this.education;
                this.students[this.index].remark = this.remark;
                this.students[this.index].name=this.name;
                this.hideAddInfo2();
            },
            editInfo(item,num){
     
                this.showAddInfo2();
                this.index = num;
                this.name = this.students[num].name;
                this.gender = this.students[num].gender;
                this.birthday = this.students[num].birthday;
                this.education = this.students[num].education;
                this.remark = this.students[num].remark;
                this.students[num].name=this.name;
            },
            remove(item,num){
     
                this.students.splice(num,1);
                console.log(num);
            }
        }
    })
</script>
</body>
</html>

你可能感兴趣的:(小练习,vue,vue.js)