当前位置:网站首页>小程序云开发表单提交并在页面中获取数据
小程序云开发表单提交并在页面中获取数据
2022-07-17 05:05:00 【小青年一枚】
当遇到表单录入时,录入好的数据提交保存到云数据库后,在其他页面展示刚提交到数据库的数据时,今天自己尝试做一遍。
表单页面:

代码如下:
<view class="container">
<form action="" bindsubmit="addBtn">
<view class="view-li">
<label>姓名:</label>
<input type="text" name="name" placeholder="请输入" placeholder-class="color" class="ipt" value='{
{form_info}}'></input>
</view>
<view class="view-li">
<label>性别:</label>
<input type="text" name="gender" placeholder="请输入" placeholder-class="color" class="ipt" value='{
{form_info}}'></input>
</view>
<view class="view-li">
<label>身份证号:</label>
<input type="idcard" name="idcard" placeholder="请输入" placeholder-class="color" class="ipt" value='{
{form_info}}'></input>
</view>
<view class="view-li">
<label>手机号:</label>
<input type="number" name="phoneNub" placeholder="请输入" placeholder-class="color" class="ipt" value='{
{form_info}}'></input>
</view>
<view class="view-li">
<label>职业:</label>
<input type="text" name="occupation" placeholder="请输入" placeholder-class="color" class="ipt" value='{
{form_info}}'></input>
</view>
<view class="view-li">
<label>目的地:</label>
<input type="text" name="destination" placeholder="请输入" placeholder-class="color" class="ipt" value='{
{form_info}}'></input>
</view>
<view class="view-li">
<label>交通工具:</label>
<input type="text" name="vehicle" placeholder="请输入" placeholder-class="color" class="ipt" value='{
{form_info}}'></input>
</view>
<button type="primary" form-type="submit">提交</button>
<button type="primary" form-type="reset">重置</button>
</form>
</view>js如下:
//index.js
//import WxValidate from '../../utils/WxValidate';
const app = getApp()
const db = wx.cloud.database()//调用默认云环境的数据库引用
Page({
data: {
dataObj:"",
number:'',
phoneNub:''
},
onLoad() {
},
//提交表单添加到数据库
addBtn: function(e){
var resValue=e.detail.value;
resValue.buyerCreateTime = new Date();//提交时新增默认时间,在获取数据时以时间排序
db.collection("list").add({
data:resValue,
}).then(e=>{
this.setData({
form_info: '',//提交后清空输入框数据
}),
console.log(e)
})
},
})
在提交方法里面默认增加了默认时间,在获取数据时用做以时间排序展示。提交到云数据库后的数据如下图:

接下来看下如何在其他页面获取数据并展示到页面中
<view class="view-li" wx:for="{
{dataArr}}" wx:key="index">
<view>{
{index+1}}、{
{item.names}}</view>
<view>{
{item.gender}}</view>
<view>{
{item.idcard}}</view>
<view>{
{item.phoneNub}}</view>
<view>{
{item.occupation}}</view>
<view>{
{item.destination}}</view>
</view>//index.js
const app = getApp()
const db = wx.cloud.database()//调用默认云环境的数据库引用
var myVlu=""//声明一个为空的变量
Page({
data: {
dataArr:""
},
//获取数据并显示在页面中
getData(){
db.collection("list").orderBy("buyerCreateTime","desc").get().then(res=>{//首先获取数据集合,查询数据,
this.setData({
dataArr:res.data
})
})
},
// 生命周期函数
onLoad: function(options) {
this.getData();
},
})

在获取数据时,使用orderBy()排序,(asc正序 、desc倒序),以默认时间来排序,buyerCreateTime在上面说过了,在表单提交的时候绑定默认时间,然后在选择使用正序还是倒序。
已上便是小程序表单提交云数据库后,再获取数据库中的数据并渲染到页面上。做个笔记记录,欢迎大家点评
边栏推荐
猜你喜欢

Asynchronous data SMS verification code

IDL 6S查找表

游玩数据获取与数据分析、数据挖掘 【2022.5.30】

Cve-2020-10199 recurrence of nexus repository manager3 remote command execution vulnerability

基于RTX30显卡的ArcGIS Pro2.8深度学习环境配置

Simple use of directexchange switches.

Cve-2017-12635 CouchDB vertical privilege bypass vulnerability recurrence

HarmonyOS第二次培训笔记

机器学习之PCA特征降维+案例实践

这么6的刷题网站你不会没听说过吧?你已经out 了?
随机推荐
mysql数据库实验实训6,数据视图(详细)
Sleuth getting started
01_电影推荐(ContentBased)_物品画像
【C语言_复习_学习第二课】什么是进制?进制之间应该如何转换
Implementation idea of log adding to database
轮播图节流阀原理及本地存储归总
elment-ui使用方法
学习C语言的第四天
mysql数据库实验实训5,数据查询yggl数据库查询(详细)
The difference between junit4 and junit5
Cve-2022-23131 ZABBIX SAML SSO authentication bypass vulnerability
【C语言_学习_考试_复习第三课】ASCII码与C语言概述
基于RTX30显卡的ArcGIS Pro2.8深度学习环境配置
SQL语句学习
PCA feature dimensionality reduction of machine learning + case practice
读论文《Learning to Measure Changes: Fully Convolutional Siamese Metric Networks for Scene Change Detec》
Harmonyos fourth training notes
图片上传的逻辑
Topicexchange switch is simple to use.
小程序editor富文本编辑使用及rich-text解析富文本