微信小程序實現(xiàn)用table顯示數(shù)據(jù)庫反饋的多條數(shù)據(jù)功能示例
更新時間:2019年05月07日 11:54:05 作者:清風思月
這篇文章主要介紹了微信小程序實現(xiàn)用table顯示數(shù)據(jù)庫反饋的多條數(shù)據(jù)功能,涉及微信小程序wx.request訪問php后臺及返回結果的顯示布局相關操作技巧,需要的朋友可以參考下
本文實例講述了微信小程序實現(xiàn)用table顯示數(shù)據(jù)庫反饋的多條數(shù)據(jù)功能。分享給大家供大家參考,具體如下:
解決了微信小程序自定義表格,并顯示的多條數(shù)據(jù)的問題。
index.wxml
<view> <text>我的調查問卷</text> <scroll-view scroll-x="true" style=" white-space: nowrap; display: flex"> <view class="table"> <view class="tr bg-header"> <view class="th">姓名</view> <view class="th">性別</view> <view class="th">年齡</view> </view> <view class="tr bg-items" wx:for = "{{items}}" wx:key=""> <text class="td">{{item.name}}</text> <text class="td">{{item.gender}}</text> <text class="td">{{item.age}}</text> </view> </view> </scroll-view> <button bindtap="change">查看我的數(shù)據(jù)</button> </view>
index.js
Page({ /** * 頁面的初始數(shù)據(jù) */ data: { items:[]//定義變長數(shù)組 }, /** * 生命周期函數(shù)--監(jiān)聽頁面加載 */ onLoad: function () { }, /** * 生命周期函數(shù)--監(jiān)聽頁面顯示 */ onShow: function () { }, /** * 生命周期函數(shù)--監(jiān)聽頁面隱藏 */ onHide: function () { }, change:function(e){ var that=this; wx.request({ url: 'https://../data.php',//自己的服務器地址 header: { "Content-Type": "application/json" }, method: "POST", success: function (res) { for (var i = 0, len = res.data.length; i < len; i++){ that.data.items[i] = res.data[i]; } that.setData({ items: that.data.items }) }, fail: function (res) { wx.showToast({ 'title': 'request fail' }) } }) } })
data.php
<?php $servername = "localhost"; $username = "root"; $password = "***";//數(shù)據(jù)庫連接密碼 $dbname = "mydb"; // 創(chuàng)建連接 $conn = new mysqli($servername, $username, $password, $dbname); // 檢測連接 if ($conn->connect_error) { die("connect server fail: " . $conn->connect_error); } $query = "select * from table"; $result = mysqli_query($conn,$query); $data = array(); while ($rows = mysqli_fetch_assoc($result)) { $data[] = $rows; } echo json_encode($data); $conn->close(); ?>
文章有借鑒,并非全部自創(chuàng)
希望本文所述對大家微信小程序開發(fā)有所幫助。
相關文章
Jquery和JS用外部變量獲取Ajax返回的參數(shù)值的方法實例(超簡單)
Jquery和JS用外部變量獲取Ajax返回的參數(shù)值的方法實例(超簡單),需要的朋友可以參考一下2013-06-06JavaScript JSON.stringify()的使用總結
JSON是一種輕量級數(shù)據(jù)格式,可以方便地表示復雜數(shù)據(jù)結構。JSON對象有兩個方法:stringify()和parse()。在簡單的情況下,這兩個方法分別可以將JavaScript序列化為JSON字符串,以及將JSON解析為原生JavaScript值。本文著重介紹JSON.stringify()的使用方法和注意事項。2021-05-05JavaScript實現(xiàn)鼠標滾輪控制頁面圖片切換功能示例
這篇文章主要介紹了JavaScript實現(xiàn)鼠標滾輪控制頁面圖片切換功能,涉及javascript事件響應及頁面元素動態(tài)操作相關實現(xiàn)技巧,需要的朋友可以參考下2017-10-10探究Javascript模板引擎mustache.js使用方法
這篇文章主要為大家介紹了Javascript模板引擎mustache.js使用方法,mustache.js是一個簡單強大的Javascript模板引擎,使用它可以簡化在js代碼中的html編寫,壓縮后只有9KB,非常值得在項目中使用,感興趣的小伙伴們可以參考一下2016-01-01