반응형
index.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>kkwproject</title> <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> </head> <body> <div class="container"> <div id="app"></div> </div> <!-- built files will be auto injected --> </body> <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <!-- Latest compiled and minified JavaScript --> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script> </html> | cs |
main.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | // The Vue build version to load with the `import` command // (runtime-only or standalone) has been set in webpack.base.conf with an alias. import Vue from 'vue' import App from './App' import router from './router' Vue.config.productionTip = false /* eslint-disable no-new */ new Vue({ el: '#app', router, components: { App }, template: '<App/>' }) | cs |
App.vue
1 2 3 4 5 6 7 8 9 10 11 12 | <template> <div id="app" style="text-align:center"> <img src="./assets/logo.png"> <router-view/> </div> </template> <script> export default { name: 'App', } </script> | cs |
router의 index.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import Vue from 'vue' import Router from 'vue-router' import admin from '@/components/admin' Vue.use(Router) export default new Router({ routes: [ { path: '/', name: 'admin', component: admin } ] }) | cs |
components의 admin.vue
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 | <template> <div class="container"> <div style="width:50%; padding-right : 30px; float:left;"> <br><br> <h2> User Management </h2> <div class ="input-group" style="margin-bottom:10px;"> <input type="text" class="form-control" placeholder="이름" v-model="name" @keyup.enter ="createUser(name)"> <input type="text" class="form-control" placeholder="나이" v-model="age" @keyup.enter ="createUser(age)"> <input type="text" class="form-control" placeholder="성별" v-model="sex" @keyup.enter ="createUser(sex)"> </div> <span class="input-group-btn"> <button class="btn btn-success" type="button" @click="createUser(str)"> 추가 </button> </span> <div v-if="isModify"> <input type="text" class="form-control" placeholder="이름" v-model="modifyName" @keyup.enter ="modifyUser(modifyName)"> <input type="text" class="form-control" placeholder="나이" v-model="modifyAge" @keyup.enter ="modifyUser(modifyAge)"> <input type="text" class="form-control" placeholder="성별" v-model="modifySex" @keyup.enter ="modifyUser(modifySex)"> <button class="btn btn-success" type="button" @click="modifyUser(modifyStr)"> 수정완료 </button> </div> <br> <ul class="list-group"> <li class="list-group-item" v-for="(user, index) in pageUsers[curpageNumber]"> <strong>이름 : </strong>{{ user.name }} <strong> 나이 : </strong> {{ user.age }} <strong> 성별 : </strong> {{ user.sex }} <div class="btn-group pull-right" style="font-size: 12px; line-height: 1;"> <button type="button" class="btn-link dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> 더보기 <span class="caret"></span> </button> <ul class="dropdown-menu"> <li><a href="#" @click="modifyUser(index)"> 수정 </a></li> <li><a href="#" @click="deleteUser(index)"> 삭제 </a></li> </ul> </div> </li> </ul> <button type="button" class="btn btn-warning" @click="pagePrev">◀ prev</button> <button type="button" class="btn btn-warning" @click="pageNext">next ▶</button> </div> <div style="width:50%; padding-left : 30px; float:right;"> <div class="btn-group"> <button type="button" class="btn btn-primary" @click="searchOn">검색</button> <button type="button" class="btn btn-primary" @click="filterOn">필터링</button> </div> <div v-if="isSearch"> <h2> Search </h2> <div class ="input-group" style="margin-bottom:10px;"> <input type="text" class="form-control" placeholder="이름" v-model="searchName" @keyup.enter ="searchUser(searchName)"> <input type="text" class="form-control" placeholder="나이" v-model="searchAge" @keyup.enter ="searchUser(searchAge)"> <input type="text" class="form-control" placeholder="성별" v-model="searchSex" @keyup.enter ="searchUser(searchSex)"> </div> <span class="input-group-btn"> <button class="btn btn-info" type="button" @click="searchUser(str)"> 검색 </button> </span> <br> <ul class="list-group"> <li class="list-group-item" v-for="(user, index) in searchUsers"> <strong>이름 : </strong>{{ user.name }} <strong> 나이 : </strong> {{ user.age }} <strong> 성별 : </strong> {{ user.sex }} </li> </ul> </div> <div v-if="!isSearch"> <h2> Filtering </h2> <div class ="input-group" style="margin-bottom:10px;"> <div class ="input-group" style="margin-bottom:10px; width:540px" > <input type="text" class="form-control" placeholder="이곳에 입력하세요." v-model="filterText"> </div> </div> <ul class="list-group"> <li class="list-group-item" v-for="(user, index) in filterUsers"> <strong>이름 : </strong>{{ user.name }} <strong> 나이 : </strong> {{ user.age }} <strong> 성별 : </strong> {{ user.sex }} </li> </ul> </div> </div> </div> </template> <script> export default { name: 'Admin', props: ['msg'], data(){ return { str:{ name:null, age:null, sex:null, }, name:null, age:null, sex:null, users: [ { name:"고관우", age:"27", sex:"남", }, { name:"abc", age:"28", sex:"남", }, { name:"고고고", age:"39", sex:"여", } ], isModify: false, modifyStr:{ name:null, age:null, sex:null, }, modifyName:null, modifyAge:null, modifySex:null, modifyIdx:null, isSearch:true, searchStr:{ name:null, age:null, sex:null, }, searchName:null, searchAge:null, searchSex:null, searchUsers: [], filterText:null, filterUsers: [], usersNumber: 0, // 이거는 나중에 수정하기 curpageNumber: 1, maxpageNumber: 1, pageUsers: [[]], } }, created:function(){ this.usersNumber = this.users.length; this.pageUsers = [[]]; this.maxpageNumber = this.usersNumber / 10 + 1; for(var i = 0; i < this.maxpageNumber; i++) this.pageUsers.push([]); var page = 1; var num = 0; for(var i = 0; i < this.usersNumber; i++){ this.pageUsers[page].push(this.users[i]); num++; if(num == 10){ num = 0; page++; } } }, methods:{ deleteUser:function(index){ this.users.splice(index,1); this.usersNumber--; }, createUser:function(val){ this.users.push({ name: val.name, age: val.age, sex: val.sex }); this.usersNumber++; this.pageUsers = [[]]; this.maxpageNumber = this.usersNumber / 10 + 1; for(var i = 0; i < this.maxpageNumber; i++) this.pageUsers.push([]); var page = 1; var num = 0; for(var i = 0; i < this.usersNumber; i++){ this.pageUsers[page].push(this.users[i]); num++; if(num == 10){ num = 0; page++; } } }, modifyUser:function(val){ if(!this.isModify) { this.isModify = true; this.modifyIdx = val; } else{ if(val.name === undefined || val.age === undefined || val.sex === undefined) { this.modifyIdx = val; return; } this.users[this.modifyIdx] = { name: val.name, age: val.age, sex: val.sex }; this.isModify = false; this.modifyName = null; this.modifyAge = null; this.modifySex = null; this.modifyIdx = null; } this.setUsers(); }, searchOn:function(){ this.isSearch = true; }, searchUser:function(){ this.searchUsers = []; var JSONstr = JSON.stringify(this.searchStr); for(var i = 0 ; i < this.users.length; i++){ var JSONusers = JSON.stringify(this.users[i]); if(JSONstr === JSONusers){ this.searchUsers.push(this.users[i]); } } }, filterOn:function(){ this.isSearch = false; }, pagePrev:function(){ if(this.curpageNumber - 1 > 0) this.curpageNumber--; }, pageNext:function(){ if(this.curpageNumber + 1 <= this.maxpageNumber) this.curpageNumber++; }, setUsers:function(){ this.pageUsers = [[]]; this.maxpageNumber = this.usersNumber / 10 + 1; for(var i = 0; i < this.maxpageNumber; i++) this.pageUsers.push([]); var page = 1; var num = 0; for(var i = 0; i < this.usersNumber; i++){ this.pageUsers[page].push(this.users[i]); num++; if(num == 10){ num = 0; page++; } } } }, watch:{ name:function(){ this.str.name = this.name; }, age:function(){ this.str.age = this.age; }, sex:function(){ this.str.sex = this.sex; }, modifyName:function(){ this.modifyStr.name = this.modifyName; }, modifyAge:function(){ this.modifyStr.age = this.modifyAge; }, modifySex:function(){ this.modifyStr.sex = this.modifySex; }, searchName:function(){ this.searchStr.name = this.searchName; }, searchAge:function(){ this.searchStr.age = this.searchAge; }, searchSex:function(){ this.searchStr.sex = this.searchSex; }, filterText:function(){ this.filterUsers=[]; if(this.filterText === '') return; for(var i = 0 ; i < this.users.length; i++){ if(this.users[i].name === undefined || this.users[i].age === undefined || this.users[i].sex === undefined) continue; if(this.users[i].name === null || this.users[i].age === null || this.users[i].sex === null) continue; if(this.users[i].name.includes(this.filterText) || this.users[i].age.includes(this.filterText) || this.users[i].sex.includes(this.filterText)){ this.filterUsers.push(this.users[i]); } } }, users:function(){ this.pageUsers = [[]]; this.maxpageNumber = this.usersNumber / 10 + 1; for(var i = 0; i < this.maxpageNumber; i++) this.pageUsers.push([]); var page = 1; var num = 0; for(var i = 0; i < this.usersNumber; i++){ this.pageUsers[page].push(this.users[i]); num++; if(num == 10){ num = 0; page++; } } } } } </script> | cs |
뷰를 이용하여 아래와 같이 User Management를 만들어 볼 수 있다.
이름, 나이, 성별을 추가할 수 있고
수정 / 삭제 기능 및 리스트가 10개를 넘어가면 페이징 동작이 실행된다.
그리고 검색 및 필터링 기능을 포함하고 있다.
이 프로그램을 돌려보며 다양한 내용을 이해해보자.
지금은 그냥 배열에 저장하는 정도이지만, axios get을 통해 DB에서 가져와 이렇게 출력하는 것도 만들어 볼 수 있다.
반응형
'Basic > VueJS' 카테고리의 다른 글
[VueJS] 컴포넌트, Props, 상위 하위 컴포넌트 (0) | 2018.08.07 |
---|---|
[VueJS] 다양한 엘리먼트 및 엘리먼트 수식어 예제 (2) | 2018.08.05 |
[VueJS] 할일 리스트 구현 (4) | 2018.08.02 |
[VueJS] Event bus를 이용한 컴포넌트 통신 (0) | 2018.08.01 |
[VueJS] 키보드 및 마우스를 이용한 이벤트 동작 (0) | 2018.07.28 |