小程序追加数据的实现方法

index.wxml

<block wx:for="{{demoData}}" wx:for-item="item" wx:for-index="idx">
  <view class="demos">
    <view><text>测试的时间:</text><text>{{item.demoTime}}</text></view>
    <view><text>测试的内容:</text><text>{{item.demoCon}}</text></view>
  </view>
</block>

<view class="btn" catchtap="anniu">追加的按钮</view>

index.js

Page({
  data: {
    demoData: [
      {
        demoTime: "2017-2-6",
        demoCon: '我是测试代码001'
      },
      {
        demoTime: "2017-8-6",
        demoCon: '我是测试代码002'
      }
    ]
  },
  onLoad: function () {
    this.setData({
      nowDate: util.formatTime(new Date())
    });
  },
  anniu:function(){
    var zhuijia = {demoTime: "2017-7-7", demoCon: '我是追加的测试代码'};
    this.data.demoData.unshift(zhuijia);
    this.setData({
      demoData: this.data.demoData
    });
  }
})

index.wxss

.demos{
  width: 100%;
  height: 100px;
  line-height: 50px;
  background-color: #188eee;
  color: #fff;
  margin: 10px 0px;
}
.btn{
  height: 50px;
  width: 80%;
  margin-left: 10%;
  line-height: 50px;
  background-color:orangered;
  color: #fff;
  text-align: center;
}

 

posted @ 2017-07-10 17:05  生如逆旅,一苇以航  阅读(2459)  评论(0编辑  收藏  举报