多个 movable-view 删除赋值JSON元素后导致问题
发布于 6 年前 作者 weimo 1083 次浏览 来自 问答

使用 json 生成多个 movable-view, delete 某一个 json 元素后,导致这一个 json 元素后的 movable-view 定位出现问题。

js:

contentData = {id0: {id:"id0",x:1, y:2}} // 数据是类似这样的一个json

wxml:

<movable-area class="moveArea" style="height: {{windowHeight}}px;width: {{windowWidth}}px;background: #ff0;">
        <block wx:for="{{contentData}}" wx:for-item="i">
            <movable-view class="moveview" id="{{i.id}}" x="{{i.x}}" y="{{i.y}}">{{i.id}}</movable-view>
        </block>
</movable-area>

效果图:

删除一个 json 元素后 id33 替代了原来 id32的位置,id34 替代了 id35的位置,以此类推

在 控制台的 wxml 中看见 movable-view 的 x 和 y 属性都没变了,但是自动生成的 style 却变成继承上一个元素的 style 了。

js:

delete this.data.contentData[e.target.id];
this.setData({
    contentData: this.data.contentData
})

效果图:

1 回复

你好,这种情况是因为你没有在wx:for的节点中指定wx:key。请尝试一下指定有效的wx:key,如果还有问题,可以在下面反馈,谢谢!

回到顶部