案例

作者: AMONTOP | 来源:发表于2018-02-05 16:22 被阅读0次

    1.v-for制作表格

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <style>
            table{
                width: 600px;
                border: 2px solid orange;
                text-align: center;
            }
            thead{
                background-color: orange;
            }
        </style>
    </head>
    <body>
    <div id="app">
        <table>
            <thead>
                <tr>
                    <td>姓名</td>
                    <td>年龄</td>
                    <td>性别</td>
                </tr>
            </thead>
            <tbody>
                <tr v-for="p in persons">
                    <td>{{p.name}}</td>
                    <td>{{p.age}}</td>
                    <td>{{p.sex}}</td>
                </tr>
            </tbody>
        </table>
    </div>
    
    <script src="../js/vue.min.js"></script>
    <script>
        new Vue({
            el:'#app',
            data:{
                persons:[
                    {name:'张三',age:18,sex:'男'},
                    {name:'李四',age:28,sex:'女'},
                    {name:'王五',age:23,sex:'男'},
                    {name:'赵六',age:30,sex:'女'},
                ]
            }
        })
    </script>
    </body>
    </html>
    

    效果图:


    image.png
    1. v-bind案例,包含v-for,给类进行绑定
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <style>
            .active{
                background-color: orange;
                font-size: 20px;
                color: #fff;
            }
        </style>
    </head>
    <body>
    <div id="app">
        <!--给类动态绑定-->
        <!--<p v-for="(college, index) in colleges" :class="index === 2 ? 'active':''">-->
        <p v-for="(college, index) in colleges" :class="index === activeIndex ? 'active':''">
            {{college}}
        </p>
        <p :style="{color:fontColor}">今天天气很好</p>
    </div>
    
    <script src="../js/vue.min.js"></script>
    <script>
        new Vue({
            el:'#app',
            data:{
                colleges:[
                        'IOS学院',
                        'Java学院',
                        'HTML学院',
                        'UI学院',
                        'C++学院'
                ],
                activeIndex:3,
                fontColor:'green'
            }
        })
    </script>
    </body>
    </html>
    

    效果图:


    image.png
    1. 综合小练习(学生录入系统)
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <style>
            #app{
                margin: 50px auto;
                width: 600px;
            }
            fieldset{
                border: 1px solid orange;
                margin-bottom: 20px;
            }
            fieldset input{
                width: 200px;
                height: 30px;
                margin: 10px 0;
            }
            table{
                width: 600px;
                border: 2px solid orange;
                text-align: center;
            }
            thead{
                background-color: orange;
            }
        </style>
    </head>
    <body>
    <div id="app">
        <!--第一部分-->
        <fieldset>
            <legend>拇指哥学生录入系统</legend>
            <div>
                <span>姓名:</span>
                <input type="text" placeholder="请输入姓名" v-model="newStudent.name">
            </div>
            <div>
                <span>年龄:</span>
                <input type="text" placeholder="请输入年龄" v-model="newStudent.age">
            </div>
            <div>
                <span>性别:</span>
                <select v-model="newStudent.sex">
                    <option value="男">男</option>
                    <option value="女">女</option>
                </select>
            </div>
            <div>
                <span>手机:</span>
                <input type="text" placeholder="请输入手机号码" v-model="newStudent.phone">
            </div>
            <button @click="createNewStudent()">创建新用户</button>
        </fieldset>
    
        <!--第二部分-->
        <table>
            <thead>
            <tr>
                <td>姓名</td>
                <td>性别</td>
                <td>年龄</td>
                <td>手机</td>
                <td>删除</td>
            </tr>
            </thead>
            <tbody>
            <tr v-for="(p,index) in persons">
                <td>{{p.name}}</td>
                <td>{{p.sex}}</td>
                <td>{{p.age}}</td>
                <td>{{p.phone}}</td>
                <td>
                    <button @click="deleteStudentMsg(index)">删除</button>
                </td>
            </tr>
            </tbody>
        </table>
    </div>
    
    <script src="../js/vue.min.js"></script>
    <script>
        new Vue({
            el: '#app',
            data: {
                persons: [
                    {name: '张三', age: 20, sex: '男', phone: '198895579'},
                    {name: '李四', age: 30, sex: '女', phone: '156895579'},
                    {name: '王五', age: 20, sex: '男', phone: '168895579'},
                    {name: '赵六', age: 25, sex: '女', phone: '178895579'}
                ],
                newStudent: {name: '', age: 0, sex: '男', phone: ''}
            },
                methods: {
                    //创建一条新纪录
                    createNewStudent(){
                        //姓名不能为空
                        if(this.newStudent.name ===''){
                            alert('姓名不能为空');
                            return ;
                        }
                        //年龄不能小于0
                        if(this.newStudent.age <= 0){
                            alert('年龄不能小于0');
                            return ;
                        }
                        //手机号码
                        if(this.newStudent.phone ===''){
                            alert('手机号码不正确');
                            return ;
                        }
                        //往数组中添加一条新纪录
                        this.persons.unshift(this.newStudent)
                        //清空数据
                        this.newStudent = {name: '', age: 0, sex: '男', phone: ''}
                    },
                    //删除一条学生记录
                    deleteStudentMsg(index){
                        this.persons.splice(index,1);
                    }
                }
        })
    </script>
    
    </body>
    </html>
    

    效果图:


    image.png

    相关文章

      网友评论

          本文标题:案例

          本文链接:https://www.haomeiwen.com/subject/ukkizxtx.html