gpt4 book ai didi

kotlin - 使用kotlinx.html通过迭代构建HTML

转载 作者:行者123 更新时间:2023-12-02 13:44:29 24 4
gpt4 key购买 nike

我正在尝试通过遍历集合来使用kotlinx.html构建HTML列表,对于该集合中的每个元素,我想在UL标签内构建LI标签。

这是我正在尝试的:

fun showProducts(products: List<Product>) {
document.getElementById(content.id)
?.append {
ol {
products.forEach {
this.li {
+it.name
}
}
}
}
}

但是我在浏览器控制台中收到一个错误:

Uncaught (in promise) TypeError: closure$products.iterator is not a function



我如何遍历集合并为传递给该函数的每种产品在UL标签内添加LI标签?

最佳答案

也许可以代替简单地使用products.forEach来使用for (p in products)。因此,代码将是:

fun showProducts(products: List<Product>) {
document.getElementById(content.id)
?.append {
ol {
for (p in products) {
li {
+p.name
}
}
}
}
}
看到这个链接:
https://try.kotlinlang.org/#/Kotlin%20Koans/Builders/Html%20builders/Task.kt

关于kotlin - 使用kotlinx.html通过迭代构建HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60044380/

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