本文實(shí)例講述了微信小程序實(shí)現(xiàn)用table顯示數(shù)據(jù)庫反饋的多條數(shù)據(jù)功能。分享給大家供大家參考,具體如下:
解決了微信小程序自定義表格,并顯示的多條數(shù)據(jù)的問題。
index.wxml
<view> <text>我的調(diào)查問卷</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',//自己的服務(wù)器地址 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ā)有所幫助。
新聞熱點(diǎn)
疑難解答