kotlin - 使用 kotlinx.html 通过迭代构建 HTML
问题描述
我正在尝试通过迭代一个集合来使用 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
推荐阅读
- java - 用于 SOAP 消息的 Spring Cloud Gateway RouteLocator
- .net - 在数据库更新期间更改环境
- python - 升级 pip 卸载了我的 pip,现在我无法在 Mac OS X 上重新安装它
- docker - 使用 Web 服务和 Docker 通过 HTTPs 服务多个站点
- angular5 - 使用代理时,Angular 5 或 6 订阅不起作用
- python-3.x - 即使模块可用,python 模块未找到错误
- python - 如何在python中访问嵌套字典
- algorithm - 根据与输入字符串匹配的单词对字符串数组进行排序
- oracle - docker weblogic 12.2.1 中不显示新服务器
- ruby - 控制器的 CanCanCan 别名方法