Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
415 views
in Technique[技术] by (71.8m points)

微信小程序:如何用setData修改数组?

学习微信小程序的小白一枚,在操作array更新时,遇到问题:
html如下:

<view class="todo-item" wx:for="{{todos}}" bindtouchend="showDeleteBtn" id="{{index}}">
            ...
      <icon type="clear" color="red" hidden="{{item.isDelete? false: true}}"></icon>
</view>

JS如下:

Page({
  data: {
    todos: [{id: 0, text:'abc', isDelete: false}]
  },
  //事件处理函数
  ...
  showDeleteBtn: function(event){
    var index = event.currentTarget.id;
    var deletedTodo = "todos["+index+"].text";
    this.setData({
       deletedTodo: true
    });
  }
})

根据文档,如果修改array里面某个具体值,是可以采用'array[0].text':'changed data'这种格式的:
图片描述

可为什么在我的例子里面,isDelete属性值就是不会被修改呢?
到底程序错在那里?
求大神解答,万分感谢!!!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

这样试试:

showDeleteBtn: function(event)
{
    var index = event.currentTarget.id;
    this.data.todos[index].isDelete = true;
    this.setData(
    {
       todos: this.data.todos
    });
)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.7k users

...