Vue系列之 => 结合ajax完成列表增删查

时间:2023-03-09 08:38:02
Vue系列之 => 结合ajax完成列表增删查
 <!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: ,
name: 'shop1',
Ctime: new Date()
},
{
id: ,
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 === ) {
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 === ) {
//添加成功后再调用一次查询请求
this.getAllList();
this.name = "";
} else {
alert('添加失败');
}
})
},
del(id) {
this.$http.get('cgi-bin/vuedata.py' + id).then(result => {
if (result.status === ) {
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: ,
name: 'shop1',
Ctime: new Date()
},
{
id: ,
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 === ) {
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 === ) {
//添加成功后再调用一次查询请求
this.getAllList();
this.name = "";
} else {
alert('添加失败');
}
})
},
del(id) {
this.$http.get('cgi-bin/vuedata.py' + id).then(result => {
if (result.status === ) {
this.getAllList()
} else {
alert('删除失败')
}
})
}
}
})
</script>
</body>
</html>