Vue系列之 => 結合ajax完成列表增刪查
阿新 • • 發佈:2018-12-25
1 <!DOCTYPE html> 2 <html lang="en"> 3 4 <head> 5 <meta charset="UTF-8"> 6 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 7 <meta http-equiv="X-UA-Compatible" content="ie=edge"> 8 <title>Document</title> 9<script src="./lib/jquery2.1.4.min.js"></script> 10 <script src="./lib/Vue2.5.17.js"></script> 11 <script src="https://cdn.bootcss.com/vue-resource/1.5.1/vue-resource.min.js"></script> 12 <link rel="stylesheet" href="./lib/bootstrap-3.3.7-dist/css/bootstrap.css"> 13</head> 14 <style> 15 16 </style> 17 18 <body> 19 <div id="app"> 20 21 <div class="panel panel-primary"> 22 <div class="panel-heading"> 23 <h3 class="panel-title">Panel title</h3> 24</div> 25 <div class="panel-body form-inline"> 26 <label for="name">name: 27 <input type="text" class="form-control" v-model="name"> 28 </label> 29 <input type="button" class="btn btn-primary" value="add" @click="add"> 30 </div> 31 </div> 32 33 <table class="table table-hover"> 34 <thead> 35 <tr> 36 <th>ID</th> 37 <th>Name</th> 38 <th>Ctime</th> 39 <th>Do</th> 40 </tr> 41 </thead> 42 <tbody> 43 <tr v-for="item in list" :key="item.id"> 44 <td>{{ item.id }}</td> 45 <td>{{ item.name }}</td> 46 <td>{{ item.Ctime }}</td> 47 <td> 48 <a href="#" @click.prevent="del(item.id)">Del</a> 49 </td> 50 </tr> 51 </tbody> 52 </table> 53 54 55 </div> 56 57 <script> 58 var vm = new Vue({ 59 el : '#app', 60 data : { 61 name : '', 62 list : [ 63 { id : 1, name : 'shop1', Ctime : new Date() }, 64 { id : 2, name : 'shop2', Ctime : new Date() } 65 ] 66 }, 67 created() {//頁面載入的時候呼叫getAllList 68 this.getAllList(); 69 }, 70 methods: { 71 getAllList(){ 72 this.$http.get('http://192.168.10.10/cgi-bin/vuedata.py?action=querylist').then( result => { 73 if(result.status === 200){ 74 var listres = JSON.parse(result.bodyText)['message']; 75 this.list = listres; 76 }else{ 77 alert('資料請求失敗'); 78 } 79 }) 80 }, 81 add(){ 82 this.$http.post('http://192.168.10.10/cgi-bin/vuedata.py',{name : this.name},{emulateJSON:true}).then(result => { 83 if (result.status === 200){ 84 //新增成功後再呼叫一次查詢請求 85 this.getAllList(); 86 this.name = ""; 87 }else { 88 alert('新增失敗'); 89 } 90 }) 91 }, 92 del(id){ 93 this.$http.get('http://192.168.10.10/cgi-bin/vuedata.py' + id).then(result=>{ 94 if(result.status === 200){ 95 this.getAllList() 96 }else{ 97 alert('刪除失敗') 98 } 99 }) 100 } 101 }, 102 103 104 105 }) 106 </script> 107 </body> 108 109 </html>
優化:使用vueresource全域性配置資料介面的根域名
1 <!DOCTYPE html> 2 <html lang="en"> 3 4 <head> 5 <meta charset="UTF-8"> 6 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 7 <meta http-equiv="X-UA-Compatible" content="ie=edge"> 8 <title>Document</title> 9 <script src="./lib/jquery2.1.4.min.js"></script> 10 <script src="./lib/Vue2.5.17.js"></script> 11 <script src="https://cdn.bootcss.com/vue-resource/1.5.1/vue-resource.min.js"></script> 12 <link rel="stylesheet" href="./lib/bootstrap-3.3.7-dist/css/bootstrap.css"> 13 </head> 14 <style> 15 16 </style> 17 18 <body> 19 <div id="app"> 20 21 <div class="panel panel-primary"> 22 <div class="panel-heading"> 23 <h3 class="panel-title">Panel title</h3> 24 </div> 25 <div class="panel-body form-inline"> 26 <label for="name">name: 27 <input type="text" class="form-control" v-model="name"> 28 </label> 29 <input type="button" class="btn btn-primary" value="add" @click="add"> 30 </div> 31 </div> 32 33 <table class="table table-hover"> 34 <thead> 35 <tr> 36 <th>ID</th> 37 <th>Name</th> 38 <th>Ctime</th> 39 <th>Do</th> 40 </tr> 41 </thead> 42 <tbody> 43 <tr v-for="item in list" :key="item.id"> 44 <td>{{ item.id }}</td> 45 <td>{{ item.name }}</td> 46 <td>{{ item.Ctime }}</td> 47 <td> 48 <a href="#" @click.prevent="del(item.id)">Del</a> 49 </td> 50 </tr> 51 </tbody> 52 </table> 53 54 55 </div> 56 57 <script> 58 // 通過vue-resource設定根域名 59 Vue.http.options.root = 'http://192.168.10.10'; 60 61 var vm = new Vue({ 62 el : '#app', 63 data : { 64 name : '', 65 list : [ 66 { id : 1, name : 'shop1', Ctime : new Date() }, 67 { id : 2, name : 'shop2', Ctime : new Date() } 68 ] 69 }, 70 created() {//頁面載入的時候呼叫getAllList 71 this.getAllList(); 72 }, 73 methods: { 74 getAllList(){ 75 this.$http.get('cgi-bin/vuedata.py?action=querylist').then( result => { 76 if(result.status === 200){ 77 var listres = JSON.parse(result.bodyText)['message']; 78 this.list = listres; 79 }else{ 80 alert('資料請求失敗'); 81 } 82 }) 83 }, 84 add(){ 85 this.$http.post('cgi-bin/vuedata.py',{name : this.name},{emulateJSON:true}).then(result => { 86 if (result.status === 200){ 87 //新增成功後再呼叫一次查詢請求 88 this.getAllList(); 89 this.name = ""; 90 }else { 91 alert('新增失敗'); 92 } 93 }) 94 }, 95 del(id){ 96 this.$http.get('cgi-bin/vuedata.py' + id).then(result=>{ 97 if(result.status === 200){ 98 this.getAllList() 99 }else{ 100 alert('刪除失敗') 101 } 102 }) 103 } 104 }, 105 }) 106 </script> 107 </body> 108 109 </html>
優化:通過vue-resource設定根域名,全域性啟用emulateJSON選項
<! DOCTYPE html> < html lang= "en">< head> < meta charset= "UTF-8"> < meta name= "viewport" content= "width=device-width, initial-scale=1.0"> < meta http-equiv= "X-UA-Compatible" content= "ie=edge"> < title>Document</ title> < script src= "./lib/jquery2.1.4.min.js"></ script> < script src= "./lib/Vue2.5.17.js"></ script> < script src= "https://cdn.bootcss.com/vue-resource/1.5.1/vue-resource.min.js"></ script> < link rel= "stylesheet" href= "./lib/bootstrap-3.3.7-dist/css/bootstrap.css"> </ head> < style>
</ style>
< body> < div id= "app"> < div class= "panel panel-primary"> < div class= "panel-heading"> < h3 class= "panel-title">Panel title</ h3> </ div> < div class= "panel-body form-inline"> < label for= "name">name: < input type= "text" class= "form-control" v-model= "name"> </ label> < input type= "button" class= "btn btn-primary" value= "add" @click= "add"> </ div> </ div>
< table class= "table table-hover"> < thead> < tr> < th>ID</ th> < th>Name</ th> < th>Ctime</ th> < th>Do</ th> </ tr> </ thead> < tbody> < tr v-for= "item in list" :key= "item.id"> < td>{{ item.id }}</ td> < td>{{ item.name }}</ td> < td>{{ item.Ctime }}</ td> < td> < a href= "#" @click.prevent= "del(item.id)">Del</ a> </ td> </ tr> </ tbody> </ table> </ div>
< script> // 通過vue-resource設定根域名 Vue.http.options.root = 'http://192.168.10.10'; //全域性啟用emulateJSON選項 Vue.http.options.emulateJSON = true;
var vm = new Vue({ el : '#app', data : { name : '', list : [ { id : 1, name : 'shop1', Ctime : new Date() }, { id : 2, name : 'shop2', Ctime : new Date() } ] }, created() { //頁面載入的時候呼叫getAllList this. getAllList(); }, methods: { getAllList(){ this.$http. get( 'cgi-bin/vuedata.py?action=querylist'). then( result => { if(result.status === 200){ var listres = JSON. parse(result.bodyText)[ 'message']; this.list = listres; } else{ alert( '資料請求失敗'); } }) }, add(){ //第三個引數{emulateJSON:true} 配置到全域性 this.$http. post( 'cgi-bin/vuedata.py',{name : this.name}). then( result => { if (result.status === 200){ //新增成功後再呼叫一次查詢請求 this. getAllList(); this.name = ""; } else { alert( '新增失敗'); } }) }, del( id){ this.$http. get( 'cgi-bin/vuedata.py' + id). then( result =>{ if(result.status === 200){ this. getAllList() } else{ alert( '刪除失敗') } }) } } }) </ script> </ body>
</ html>
<! DOCTYPE html> < html lang= "en">
< head> < meta charset= "UTF-8"> < meta name= "viewport" content= "width=device-width, initial-scale=1.0"> < meta http-equiv= "X-UA-Compatible" content= "ie=edge"> < title>Document</ title> < script src= "./lib/jquery2.1.4.min.js"></ script> < script src= "./lib/Vue2.5.17.js"></ script> < script src= "https://cdn.bootcss.com/vue-resource/1.5.1/vue-resource.min.js"></ script> < link rel= "stylesheet" href= "./lib/bootstrap-3.3.7-dist/css/bootstrap.css"> </ head> < style>
</ style>
< body> < div id= "app"> < div class= "panel panel-primary"> < div class= "panel-heading"> < h3 class= "panel-title">Panel title</ h3> </ div> < div class= "panel-body form-inline"> < label for= "name">name: < input type= "text" class= "form-control" v-model= "name"> </ label> < input type= "button" class= "btn btn-primary" value= "add" @click= "add"> </ div> </ div>
< table class= "table table-hover"> < thead> < tr> < th>ID</ th> < th>Name</ th> < th>Ctime</ th> < th>Do</ th> </ tr> </ thead> < tbody> < tr v-for= "item in list" :key= "item.id"> < td>{{ item.id }}</ td> < td>{{ item.name }}</ td> < td>{{ item.Ctime }}</ td> < td> < a href= "#" @click.prevent= "del(item.id)">Del</ a> </ td> </ tr> </ tbody> </ table> </ div>
< script> // 通過vue-resource設定根域名 Vue.http.options.root = 'http://192.168.10.10'; //全域性啟用emulateJSON選項 Vue.http.options.emulateJSON = true;
var vm = new Vue({ el : '#app', data : { name : '', list : [ { id : 1, name : 'shop1', Ctime : new Date() }, { id : 2, name : 'shop2', Ctime : new Date() } ] }, created() { //頁面載入的時候呼叫getAllList this. getAllList(); }, methods: { getAllList(){ this.$http. get( 'cgi-bin/vuedata.py?action=querylist'). then( result => { if(result.status === 200){ var listres = JSON. parse(result.bodyText)[ 'message']; this.list = listres; } else{ alert( '資料請求失敗'); } }) }, add(){ //第三個引數{emulateJSON:true} 配置到全域性 this.$http. post( 'cgi-bin/vuedata.py',{name : this.name}). then( result => { if (result.status === 200){ //新增成功後再呼叫一次查詢請求 this. getAllList(); this.name = ""; } else { alert( '新增失敗'); } }) }, del( id){ this.$http. get( 'cgi-bin/vuedata.py' + id). then( result =>{ if(result.status === 200){ this. getAllList() } else{ alert( '刪除失敗') } }) } } }) </ script> </ body>
</ html>