微信小程序 wx:for循环和数组内部元素的增加

wx:for循环实现自动换行:
wxss文件内

flex-wrap: wrap;

wx:for 循环的内容

<view class="menu_item" wx:for="{{menus}}" wx:for-index="idx" wx:for-item="menus" wx:key="menus">
      <image class="menu_icon" src="/icons/blue.png"></image>
      <text class="menu_name">{{menus.name}}</text>
      <switch type="switch" class="switch" checked="true"/>
    </view>

如果没有wx:key会有黄色警示

随循环图形移动按钮的实现:

<view class="part_2">
    <view class="menu_item" wx:for="{{menus}}" wx:for-index="idx" wx:for-item="menus" wx:key="menus">
      <image class="menu_icon" src="/icons/blue.png"></image>
      <text class="menu_name">{{menus.name}}</text>
      <switch type="switch" class="switch" checked="true"/>
    </view>//同上面的循环
    <view class="menu_item">//下部不是循环,但是属于父类view,仍然会自动换行
    <image class="next" src="/icons/add.png" bindtap="addicons"></image>
      <text class="menu_name">增加餐桌</text>
    </view>
  </view>

wxss文件

.part_2 {
  position: absolute;
  top: 206px;
  height: 300px;
  width: 330px;
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
}

.menu_item {
  height: 150px;
  width: 110px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  font-size: 12px;
  box-sizing: border-box;
  padding-bottom: 10px;
  padding-top: 10px;
}

.menu_name{
font-family: '幼圆';
font-size: large;
position: relative;
top: 5px;
color: #A9A9A9;
}

.menu_icon{
  width: 70px;
  height: 60px;
}

.switch{
  position: relative;
  top: 10px;

}

.next{
  width: 60px;
  height: 60px;
  justify-content: center;
}

js文件:实现点击来增加数组元素实现图片文字的循环增加

var i=0;
var array=[];
Page({
  data: {
    number:0,
    motto: 'Hello World',
    userInfo: {},
    menus:[ 
    ],
  },
  addicons:function(e){
    console.log("正在执行增添餐桌操作");
    i++;
    var obj={};
    obj.id=i;
    obj.name=i+"号桌"
    array.push(obj);
    console.log(array);
    this.setData({
      "menus": array
    })
  }
})
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值