Mikael Ståldal
07/07/2023, 10:04 AM<option value="a1">A1</option>
<option value="a3">A3</option>
<option value="a6">A6</option>
(without the containing <select>
element.) This is for JVM server-side, I am generating HTML snippets to be requested with AJAX and inserted into the DOM on the client.
I can do this to get it with a wrapping <select>
element:
val models = listOf(IdName("a1", "A1"), IdName("a3", "A3"), IdName("a6", "A6"))
val html: String = createHTML()
.select {
for (model in models) {
option {
attributes["value"] = model.id
+model.name
}
}
}
but how do I get rid of the wrapping element?Cies
07/07/2023, 1:42 PMselect
element before sending the snippet out.Cies
07/07/2023, 1:44 PMMikael Ståldal
07/07/2023, 2:38 PMCies
07/07/2023, 2:59 PMoption
then you can make it in a way that --unlike the non-custom option
element-- it can occur everywhere in the HTML (and not only in a select
)Cies
07/07/2023, 3:00 PMMikael Ståldal
07/07/2023, 3:02 PMoption
element without a `select`:
val html: String = createHTML().option {
value = "value"
+"name"
}
Cies
07/07/2023, 3:12 PMMikael Ståldal
07/07/2023, 3:12 PMString
and cannot add more elements.Mikael Ståldal
07/07/2023, 3:36 PMMikael Ståldal
07/07/2023, 3:44 PMmodels.joinToString(separator = "") { model ->
createHTML().option {
value = model.id
+model.name
}
}
But it is not very nice.Mikael Ståldal
07/08/2023, 12:58 PM@HtmlTagMarker
inline fun <T, C : TagConsumer<T>> C.fragment(crossinline block: TagConsumer<T>.() -> Unit): T {
try {
this.block()
} catch (err: Throwable) {
this.onTagError(HTMLTag("", this, emptyMap(), null, inlineTag = false, emptyTag = false), err)
}
return this.finalize()
}
Mikael Ståldal
07/08/2023, 1:00 PMcreateHTML().fragment {
models.forEach { model ->
option {
value = choice.id
+choice.name
}
}
}
Mikael Ståldal
07/09/2023, 11:58 AMTagConsumer.finalize()
multiple times. Back to the drawing board.Mikael Ståldal
07/09/2023, 1:54 PM@HtmlTagMarker
inline fun <T, C : TagConsumer<T>> C.fragment(crossinline block: DIV.() -> Unit): T {
DIV(emptyMap(), this).block()
return this.finalize()
}
But not for all elements, not for e.g. <tr>
and <option>
.