gpt4 book ai didi

javascript - 嵌套 dom-repeat 中数组映射的更改通知

转载 作者:行者123 更新时间:2023-11-29 15:15:52 37 4
gpt4 key购买 nike

我正在尝试向数组映射中添加一些内容,但值绑定(bind)没有接受更改:-(

我有这样的东西:

       properties: {
deals: {
type: Object,
notify: true,
value: function () {
return {
items: {
"MONDAY": [],
"TUESDAY": [],
"WEDNESDAY": [],
"THURSDAY": [],
"FRIDAY": [],
"SATURDAY": [],
"SUNDAY": []
}
}
},
},
weekDays: {
type: Array,
value: function () {
return [
{"text": "Montag", "key": "MONDAY"},
{"text": "Dienstag", "key": "TUESDAY"},
{"text": "Mittwoch", "key": "WEDNESDAY"},
{"text": "Donnerstag", "key": "THURSDAY"},
{"text": "Freitag", "key": "FRIDAY"},
{"text": "Samstag", "key": "SATURDAY"},
{"text": "Sonntag", "key": "SUNDAY"}
]
},
},
},

然后我嵌套了 dom-repeat 来渲染元素。当我使用类似

的函数时这有效
        _itemsForDay: function (day,map) {
const data = map[day.key];
return data;
}

但是当我添加元素时,它并没有被重新绘制,我已经尝试了很多 notifyPath 变体,但是它没有渲染新元素:-(

这是我尝试组合成一个的:

  add: function (e) {
day = e.model.day.key;
this.push("deals.items." + day,
{
description: "Bla",
price1: "23,45€",
price2: "43,21€",
imageId: 1
}
);
this.notifyPath("weekDays");
this.notifyPath("weekDays.*");
this.notifyPath("weekDays.splice");
this.notifyPath("deals.items.*");
this.notifyPath("deals.items.*");
this.notifyPath("deals.items.splice");
this.notifyPath("deals.items."+day);
this.notifyPath("deals.items."+day+".*");
this.notifyPath("deals.items."+day+".splice");
this.notifyPath("weekDays.*");
console.log("Length: "+this.deals.items[day].length)
},

如何确保 map 键的内容被重新渲染(也可以重新渲染整个 map ,我不关心)。

模板看起来像这样:

       <tr>
<template is="dom-repeat" items="{{weekDays}}" as="day" >
<td>
<template is="dom-repeat" items="{{_itemsForDay(day,deals.items)}}" >
<div>
DESC: [[item.description]]
IMG: [[item.imageId]]
[[deals.price1Label]]: [[item.price1]]
[[deals.price2Label]]: [[item.price2]]
</div>
</template>
<paper-button on-click="add" data-day="[[day.key]]">
+
</paper-button>
</td>
</template>
</tr>

有趣的是,当我删除外部循环并使用固定的工作日时,它起作用了:

              <td>
<template is="dom-repeat" items="{{deals.items.TUESDAY}}" >
<div>
DESC: [[item.description]]
IMG: [[item.imageId]]
[[deals.price1Label]]: [[item.price1]]
[[deals.price2Label]]: [[item.price2]]
</div>
</template>
<paper-button on-click="add" data-day="TUESDAY">
+
</paper-button>
</td>

这会拾取更改并呈现新项目,但我必须多次复制内部部分,这远非理想。

最佳答案

slack channel 中的 ZeeWolf 有一个不错的想法,它奏效了:

创建新属性:

                dayLists: {
type: Array,
computed: '_computeDayLists(deals.items.*)'
}

添加计算方法:

        _computeDayLists: function() {
return this.weekDays.map(day => {
const dayKey = day.key;
const items = this.deals.items[dayKey];
console.log("Found for "+dayKey+" #"+items.length+" items");
return {
key: dayKey,
items: items
}
});
},

更新模板:

           <template is="dom-repeat" items="{{dayLists}}" >
<td>
<template is="dom-repeat" items="{{item.items}}" mutable-data>
<div>
DESC: [[item.description]]
IMG: [[item.imageId]]
[[deals.price1Label]]: [[item.price1]]
[[deals.price2Label]]: [[item.price2]]
</div>
</template>
<paper-button on-click="add">
+
</paper-button>
</td>
</template>
        add: function (e) {
const day = e.model.item.key;
this.push("deals.items." + day,
{
description: "Bla",
price1: "23,45€",
price2: "43,21€",
imageId: 1,
new: true
}
);
console.log("Length: " + this.deals.items[day].length)
},

解释

内部循环不知何故没有获取更改,因此此方法使用计算列表,该列表依赖于 map 项内容(.* 启用此功能)。当其中的某些内容发生变化时,这会重新绘制列表。重要的是没有它的可变数据,那是行不通的!

并确保您拥有最新版本的聚合物。我有一个旧版本,由于错误配置的 bower maven 步骤而没有更新,并且在我更新依赖项之前它没有工作。

关于javascript - 嵌套 dom-repeat 中数组映射的更改通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49147510/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com