Java Script/Vue.js

[Vue] Vue 복습문제 - 검색, 체크박스, 삭제 #0417수업 #2교시

웨일파도 2023. 4. 17. 15:57
반응형

<html>
<head>
    <link rel="stylesheet" href="index.css">
    <script src="js/jquery.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <style>
          table, td, th{
            border : 1px solid #ccc;
            font-size: 20px;
            text-align: center;
            border-collapse: collapse;
            padding : 5px 20px;
        }
    </style>
</head>
<body>      
    <div id="app">
        <div>
            <lable>입력 : <input type="text" v-model="name" @keyup.enter="fnclick()"></lable>
            <select>
                <option id=0 value="machine">:: 전체 ::</option>
                <option id=1 value="machine">기계</option>
                <option id=2 value="elec">전기전자</option>
                <option id=3 value="computer">컴퓨터정보</option>
            </select>
            <button @click="fnclick()" style="margin-bottom: 20px;">click</button>
        </div>
        <div class="table-list">
            <table>                   
                <thead>
                    <tr>                            
                        <th scope="col">No.</th>
                        <th scope="col">학번</th>
                        <th scope="col">이름</th>
                        <th scope="col">학과</th>
                        <th scope="col">학년</th>
                        <th scope="col">키</th>
                        <th scope="col">몸무게</th>
                    </tr>
                </thead>
                <tbody>

                    <tr v-for="(item, index) in list" v-if = "nameFlg != ''">
                        <template v-if="item.stu_name == nameFlg">
                            <td>{{index + 1}}</td> 
                            <td>{{item.stu_no}}</td>
                            <td>{{item.stu_name}}</td>
                            <td>{{item.stu_dept}}</td>
                            <td>{{item.stu_grade}}</td>
                            <td>{{item.stu_height}}</td>
                            <td>{{item.stu_weight}}</td>
                        </template>  
                    </tr>

                    <tr v-else>
                        <td>{{index + 1}}</td> 
                        <td>{{item.stu_no}}</td>
                        <td>{{item.stu_name}}</td>
                        <td>{{item.stu_dept}}</td>
                        <td>{{item.stu_grade}}</td>
                        <td>{{item.stu_height}}</td>
                        <td>{{item.stu_weight}}</td> 
                    </tr>
                        
                    
                </tbody>                   
            </table>
        </div>
    </div>      
</body>
</html>
<script>
    var app = new Vue({ 
    el: '#app',
    data: {
        seen: true
        , list : [],
        name : "",
        nameFlg : ""
    }, methods: {
            fnTest : function(){
                var self = this;
                $.ajax({
                    url:"js/MOCK_DATA2.json",
                    dataType:"json",
                    success:function(data) {
                        self.list = data;
                        
                }
            });
        },
        fnclick : function() {
            this.nameFlg = this.name;
        }
    }
    , created: function () {
		this.fnTest();
        this.fnclick();
	}
});
</script>

체크박스

 

삭제 기능

<html>
<head>
    <link rel="stylesheet" href="index.css">
    <script src="js/jquery.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <style>
          table, td, th{
            border : 1px solid #ccc;
            font-size: 20px;
            text-align: center;
            border-collapse: collapse;
            padding : 5px 20px;
        }
        .btn {
            margin-top: 20px;
            padding: 10px;
            font-size: 15px;
            border-radius: 5px;
        }
    </style>
</head>
<body>      
    <div id="app">
        <div>
            <lable>입력 : <input type="text"></lable>
            <select v-model="selectItem">
                <option id=0 value="all">:: 전체 ::</option>
                <option id=1 value="기계">기계</option>
                <option id=2 value="전기전자">전기전자</option>
                <option id=3 value="컴퓨터정보">컴퓨터정보</option>
            </select>
            <button @click="fnclick()" style="margin-bottom: 20px;">click</button>
        </div>
        <div class="table-list">
            <table>                   
                <thead>
                    <tr>   
                        <th scope="col"></th>
                        <th scope="col">No.</th>
                        <th scope="col">학번</th>
                        <th scope="col">이름</th>
                        <th scope="col">학과</th>
                        <th scope="col">학년</th>
                        <th scope="col">키</th>
                        <th scope="col">몸무게</th>
                    </tr>
                </thead>
                <tbody>
                    
                    <!-- ★ v-vind:value -->
                    <tr v-for="(item, index) in list" v-if = "selectItem != 'all'">
                        <template v-if="item.stu_dept == selectItem">
                            <td><input type="checkbox" v-bind:value="item.stu_no" v-model="checkList"></td> 
                            <td>{{index + 1}}</td> 
                            <td>{{item.stu_no}}</td>
                            <td>{{item.stu_name}}</td>
                            <td>{{item.stu_dept}}</td>
                            <td>{{item.stu_grade}}</td>
                            <td>{{item.stu_height}}</td>
                            <td>{{item.stu_weight}}</td>
                        </template>
                     
                    <tr v-else>
                        <td><input type="checkbox" v-bind:value="item.stu_no" v-model="checkList"></td> 
                        <td>{{index + 1}}</td> 
                        <td>{{item.stu_no}}</td>
                        <td>{{item.stu_name}}</td>
                        <td>{{item.stu_dept}}</td>
                        <td>{{item.stu_grade}}</td>
                        <td>{{item.stu_height}}</td>
                        <td>{{item.stu_weight}}</td>  
                    </tr> 

                    </tr>
                    
                </tbody>                   
            </table>
        </div>
        <button @click="fnDelete" class="btn">Delete</button>
    </div>      
</body>
</html>
<script>
    var app = new Vue({ 
    el: '#app',
    data: {
        seen: true, 
        list : [],
        name : "",
        nameFlg : "",
        checkList : [],
        selectItem : "",
       
        stu_weight : ""
    }, methods: {
            fnTest : function(){
                var self = this;
                $.ajax({
                    url:"js/MOCK_DATA2.json",
                    dataType:"json",
                    success:function(data) {
                        self.list = data;
                        
                }
            });
        },
        fnclick : function() {
            console.log(this.selectItem);

            

        },
        fnDelete : function() {
            console.log(this.checkList);

            for(let i=this.list.length-1; i>=0; i--) {
                for (let j=0; j<this.checkList.length; j++) {
                    if (this.list[i].stu_no == this.checkList[j]) {
                        this.list.splice(i,1);
                        break;
                    }
                } 
            }
        }
    }
    , created: function () {
		this.fnTest();
	}
});
</script>

 

 

반응형