云开发-web应用中使用数据库
WEB前端开发社区 昨天
如何在 web 应用中使用数据库
<div id="app">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">web应用中使用数据库</h3>
</div>
<div class="panel-body form-inline">
<label>
Name:
<input type="text" class="form-control" v-model="name" />
</label>
<input
type="button"
value="添加"
class="btn btn-primary"
@click="add()"
/>
<label>
搜索名称关键字:
<input type="text" class="form-control" v-model="keywords" />
</label>
<input
type="button"
value="查找"
class="btn btn-primary"
@click="search(keywords)"
/>
</div>
</div>
<table class="table table-bordered table-hover table-striped">
<thead>
<tr>
<th>things</th>
<th>delete</th>
<th>finsih</th>
</tr>
</thead>
<tbody>
<tr v-for="item in list" :key="item._id">
<td :class="[item.complete?'active':'']" v-text="item.name"></td>
<td>
<a href="" @click.prevent="del(item._id)">删除</a>
</td>
<td>
<a href="" @click.prevent="complete(item._id,item.complete)"
>{{item.complete?'取消完成':'已完成'}}</a
>
</td>
</tr>
</tbody>
</table>
</div>
<script src="./lib/vue-2.4.0.js"></script><script src="https://imgcache.qq.com/qcloud/tcbjs/1.3.5/tcb.js"></script><link rel="stylesheet" href="./lib/bootstrap-3.3.7.css" />
说明:这里的 vue 和 bootstrap 文件可以通过 npm 自行下载
const app = tcb.init({ env: "你的环境id",});app .auth() .signInAnonymously() .then(() => { // alert("登录云开发成功!"); });const db = app.database();const collection = db.collection("todo");
mounted() { console.log("mounted"); collection.get().then((res) => { // console.log(res) this.list = res.data; }); },
search(keywords) { console.log(keywords); collection .where({ name: { $regex: keywords + ".*", }, }) .get() .then((res) => { console.log(res); this.list = res.data; }); },
add() { collection .add({ name: this.name, complete: false, }) .then((res) => { this.name = ""; }); collection.get().then((res) => { this.list = res.data; this.search("") }); },
del(id) { console.log(id); collection .doc(id) .remove() .then((res) => { console.log(res); }); collection.get().then((res) => { this.list = res.data; this.search("") }); },
complete(id,complete){ console.log(id) comolete = !complete collection .doc(id) .update({ complete:comolete }) collection.get().then((res) => { // console.log(res) this.list = res.data; this.search("") }); }
<!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/vue-2.4.0.js"></script>
<script src="https://imgcache.qq.com/qcloud/tcbjs/1.3.5/tcb.js"></script>
<link rel="stylesheet" href="./lib/bootstrap-3.3.7.css" />
<style>
.active {
text-decoration: line-through;
color: blueviolet;
}
</style>
</head>
<body>
<div id="app">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">web应用中使用数据库</h3>
</div>
<div class="panel-body form-inline">
<label>
Thing:
<input type="text" class="form-control" v-model="name" />
</label>
<input
type="button"
value="添加"
class="btn btn-primary"
@click="add()"
/>
<label>
搜索名称关键字:
<input type="text" class="form-control" v-model="keywords" />
</label>
<input
type="button"
value="查找"
class="btn btn-primary"
@click="search(keywords)"
/>
</div>
</div>
<table class="table table-bordered table-hover table-striped">
<thead>
<tr>
<th>things</th>
<th>delete</th>
<th>finsih</th>
</tr>
</thead>
<tbody>
<tr v-for="item in list" :key="item._id">
<td :class="[item.complete?'active':'']" v-text="item.name"></td>
<td>
<a href="" @click.prevent="del(item._id)">删除</a>
</td>
<td>
<a href="" @click.prevent="complete(item._id,item.complete)"
>{{item.complete?'取消完成':'已完成'}}</a
>
</td>
</tr>
</tbody>
</table>
</div>
<script>
const app = tcb.init({
env: "xxx",
});
app
.auth()
.signInAnonymously()
.then(() => {
// alert("登录云开发成功!");
});
const db = app.database();
const collection = db.collection("todo");
var vm = new Vue({
el: "#app",
data: {
name: "",
keywords: "",
list: [],
},
update() {
this.search("");
},
mounted() {
console.log("mounted");
collection.get().then((res) => {
// console.log(res)
this.list = res.data;
});
},
methods: {
add() {
collection
.add({
name: this.name,
complete: false,
})
.then((res) => {
this.name = "";
});
collection.get().then((res) => {
this.list = res.data;
this.search("");
});
},
del(id) {
console.log(id);
collection
.doc(id)
.remove()
.then((res) => {
console.log(res);
});
collection.get().then((res) => {
this.list = res.data;
this.search("");
});
},
search(keywords) {
console.log(keywords);
collection
.where({
name: {
$regex: keywords + ".*",
},
})
.get()
.then((res) => {
console.log(res);
this.list = res.data;
});
},
complete(id, complete) {
console.log(id);
comolete = !complete;
collection.doc(id).update({
complete: comolete,
});
collection.get().then((res) => {
// console.log(res)
this.list = res.data;
this.search("");
});
},
},
});
</script>
</body>
</html>
收藏此内容的人还喜欢
赞 (0)