当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 微信小程序 wx:for遍历循环使用实例解析

微信小程序 wx:for遍历循环使用实例解析

2019年09月14日  | 移动技术网IT编程  | 我要评论

这篇文章主要介绍了微信小程序 wx:for遍历循环使用实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

效果图如下:

实现代码如下:

type.js:

// pages/type/type.js
page({

 /**
  * 页面的初始数据
  */
 data: {
  types: ""
 },
 edittype: function (e) {
   var typeid = e.currenttarget.dataset['id'];
   console.log("edit:"+typeid);


  wx.navigateto({
   url: '../type_edit/type_edit?typeid=' + typeid
  })
 },
 deltype:function(e){

  var typeid = e.currenttarget.dataset['id'];

  console.log("delete:"+typeid)


  wx.showmodal({
   title: '提示',
   content: '确认要删除该支出类型?',
   success: function (res) {
    if (res.confirm) {
     console.log('用户点击确定')

     wx.request({

      url: getapp().globaldata.urlpath + "spendingtype/delete",
      method: "post",
      data: {
       typeid: typeid
      },
      header: {
       "content-type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
       console.log(res.data.code);
       if (res.statuscode == 200) {

        //访问正常
        if (res.data.code == "000000") {
         wx.showtoast({
          title: "删除成功,返回支出类型列表",
          icon: 'success',
          duration: 3000,
          success: function () {

           wx.navigateto({
            url: '../type/type'
           })
          }
         })

        }
       } else {

        wx.showloading({
         title: '系统异常',
         fail
        })

        settimeout(function () {
         wx.hideloading()
        }, 2000)
       }

      }
     })


    } else if (res.cancel) {
     console.log('用户点击取消')
    }
   }
  })

 },
 /**
  * 生命周期函数--监听页面加载
  */
 onload: function (options) {
  wx.setnavigationbartitle({

   title: "支出类型列表"

  })
  var usercode = wx.getstoragesync('userid');
  var self = this
  wx.request({
   url: getapp().globaldata.urlpath + "spendingtype/types",//json数据地址 
   data: {
    usercode: usercode
   },
   headers: {
    "content-type": "application/x-www-form-urlencoded"
   },
   success: function (res) {
    self.setdata({

     types: res.data.data

    });//等同于

   }
  })
 },

 /**
  * 生命周期函数--监听页面初次渲染完成
  */
 onready: function () {

 },

 /**
  * 生命周期函数--监听页面显示
  */
 onshow: function () {

 },

 /**
  * 生命周期函数--监听页面隐藏
  */
 onhide: function () {

 },

 /**
  * 生命周期函数--监听页面卸载
  */
 onunload: function () {

 },

 /**
  * 页面相关事件处理函数--监听用户下拉动作
  */
 onpulldownrefresh: function () {

 },

 /**
  * 页面上拉触底事件的处理函数
  */
 onreachbottom: function () {

 },

 /**
  * 用户点击右上角分享
  */
 onshareappmessage: function () {

 }
})

type.js没什么好说的,如果要说,只能说这个onload这里的onload就相当于js中的onload方法,当进入该视图时,默认全局加载一次。

type.wxml:

<view>
 <view>
  <navigator url="/pages/type_add/type_add" hover-class="navigator-hover">添加支出类型信息</navigator>
 </view>
 <view>
  <text>\n</text>
 </view>
 <view>
  <view>
   <text>列表数据</text>
   <text>\n</text>
  </view>
<view class="table">
    <view class="tr thead">
     <view class="td">类型名</view>
     <view class="td">创建时间</view>
     <view class="td ">修改时间</view>
     <view class="td">备注</view>
     <view class="td ">操作</view>
    </view>
    <block>
     <view class="tr" wx:for="{{types}}" wx:for-item="item">
      <view class="td">{{item.typename}}</view>
      <view class="td">{{item.createdate}}</view>
      <view class="td">{{item.modifydate}}</view>
      <view class="td">{{item.remark}}</view>
      <view class="td">
       <text bindtap='edittype' data-id="{{item.typeid}}">编辑</text>
       <text>\n</text>
       <text>\n</text>
       <text bindtap='deltype' data-id="{{item.typeid}}">删除</text>
      </view>
     </view>
    </block>
   </view>
 </view>
</view>

遍历循环主要使用的是wx:for。如果要类比的话,我觉得jstl跟这个神似。先来看看jstl,代码如下:

<c:foreach var="u" items="${user}">
    <tr>
        <td>${u.cid}</td>
        <td>${u.cname}</td>
        <td>${u.age }</td>
    </tr>
</c:foreach>

var相当于我可以任意定义一个简要字母来调用item(item相当于type.js中的data或self.setdata存储的数据)。

如果你还不明白的话,可以联系到$.each,代码如下:

$.each(classroom_list, function(i, c) {

          rows = rows + "<tr>";
          rows = rows + "<td>" + c.id + "</td>";
          rows = rows + "<td>" + c.nickname + "</td>";
          rows = rows + "<td><a href='student_submit_info.html?userid="+c.id+"'>查看详情</a></td>";
          rows = rows + "</tr>"
});

type.wxss:

.table {
 border: 1px solid #ccc;
 font-size: 28rpx;
 background: #fff;
 border-right: none;
}
 
.tr{
 display: flex;
 justify-content: space-between;
}
 
.td {
 text-align: center;
 border: 1px solid #ccc;
 display: inline-block;
 border-left: none;
 border-bottom: none;
 padding: 10rpx 1%;
 width: 12%;
}
 
.thead .td{
 border-top: none;
 height: 140rpx;
 line-height: 50rpx;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网