当前位置:网站首页>小程序云开发表单提交并在页面中获取数据
小程序云开发表单提交并在页面中获取数据
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在上面说过了,在表单提交的时候绑定默认时间,然后在选择使用正序还是倒序。
已上便是小程序表单提交云数据库后,再获取数据库中的数据并渲染到页面上。做个笔记记录,欢迎大家点评
边栏推荐
- ModerlArts第一次培训笔记
- 关于当前响应已经调用了方法getOutputStream()
- 获取数组中对象内部的数值最大与最小值多功能版及点名系统完整版并展示效果
- 【C语言—零基础第八课】循环结构与break continue
- Getting started with harmonios
- 简单快速建立pytorch环境YOLOv5目标检测 模型跑起来(超简单)
- 【Es6】利用添加数据,筛选并传输至页面等多项功能实现案例
- Redis installation
- ModelArts第二次培训笔记
- Differences and precautions of fastjson, jackjson and gson
猜你喜欢
PyGame installation -requirement already satisfied
Shallow chat link tracking
无重复字符的最长字串
NVIDIA GeForce Experience登录报错:验证程序加载失败,请检查您的浏览器设置,例如广告拦截程序(解决办法)
Asynchronous data SMS verification code
Harmonyos second training notes
小程序editor富文本编辑使用及rich-text解析富文本
elment-ui使用方法
机器学习之特征提取(类别特征进行数值化、离散化、文本特征进行数值化)
这么6的刷题网站你不会没听说过吧?你已经out 了?
随机推荐
微信小程序状态栏
Cve-2017-12635 CouchDB vertical privilege bypass vulnerability recurrence
【C语言—零基础_学习_复习_第五课】基本运算符的运算性质
Cve-2022-23131 ZABBIX SAML SSO authentication bypass vulnerability
【C语言—零基础第十三课】字符串的奥秘
基于RTX30显卡的ArcGIS Pro2.8深度学习环境配置
学习C语言的第五天
【C语言—零基础第十四课】变量的作用域与存储类
[2022 10th Teddy Cup Challenge] Title A: complete version of pest identification (general idea. Detailed process and code and results CSV in compressed package)
【C语言—零基础第十一课】旋转大转盘之指针
645. 错误的集合
[batch] batch delete intermediate folder - personal research script
ModerlArts第一次培训笔记
Cve-2019-14234 Django jsonfield SQL injection vulnerability
这么6的刷题网站你不会没听说过吧?你已经out 了?
C语言练习题
【C】张梁计算器
读论文《SNUNet-CD: A Densely Connected Siamese Network for Change Detection of VHR Images》
C语言初学者之初识代码专项练习
Feature extraction of machine learning (digitization and discretization of category features and digitization of text features)