Skip to content

Commit f59f06f

Browse files
committed
Add htmx_delete_row.sc example
1 parent a9dd231 commit f59f06f

File tree

2 files changed

+63
-10
lines changed

2 files changed

+63
-10
lines changed

examples/scala-cli/htmx/htmx_click_to_load.sc

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,6 @@ import ba.sake.hepek.htmx.*
99
import ba.sake.querson.QueryStringRW
1010
import ba.sake.sharaf.*, routing.*
1111

12-
/*
13-
<tr id="replaceMe">
14-
<td colspan="3">
15-
<button class='btn' hx-get="/contacts/?page=2"
16-
hx-target="#replaceMe"
17-
hx-swap="outerHTML">
18-
Load More Agents... <img class="htmx-indicator" src="/img/bars.svg">
19-
</button>
20-
</td>
21-
</tr>*/
2212
object views {
2313
import scalatags.Text.all.*
2414
import ba.sake.hepek.html.HtmlPage
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//> using scala "3.3.1"
2+
//> using dep ba.sake::sharaf:0.0.22
3+
4+
import java.util.UUID
5+
import io.undertow.Undertow
6+
import scalatags.Text.all.*
7+
import ba.sake.hepek.html.HtmlPage
8+
import ba.sake.hepek.htmx.*
9+
import ba.sake.querson.QueryStringRW
10+
import ba.sake.sharaf.*, routing.*
11+
12+
object views {
13+
import scalatags.Text.all.*
14+
import ba.sake.hepek.html.HtmlPage
15+
import ba.sake.hepek.htmx.*
16+
17+
trait BasePage extends HtmlPage with HtmxDependencies
18+
19+
class ContactsViewPage(contacts: Seq[Contact]) extends BasePage:
20+
override def bodyContent = div(
21+
h1("Delete Row example"),
22+
table()(
23+
thead(tr(th("Name"), th("Email"), th(""))),
24+
tbody(hx.confirm := "Are you sure?", hx.target := "closest tr", hx.swap := "outerHTML swap:1s")(
25+
contactsRows(contacts)
26+
)
27+
)
28+
)
29+
30+
override def stylesInline = List("""
31+
tr.htmx-swapping td {
32+
opacity: 0;
33+
transition: opacity 1s ease-out;
34+
}
35+
""")
36+
37+
def contactsRows(contacts: Seq[Contact]): Frag = contacts.map { contact =>
38+
tr(td(contact.name), td(contact.email), td(button(hx.delete := s"/contacts/${contact.id}")("Delete")))
39+
}
40+
41+
}
42+
case class Contact(id: String, name: String, email: String)
43+
44+
var allContacts = Seq(
45+
Contact("1", "Angie MacDowell", "[email protected]"),
46+
Contact("2", "Fuqua Tarkenton", "[email protected]"),
47+
Contact("3", "Kim Yee", "[email protected]")
48+
)
49+
50+
val routes = Routes:
51+
case GET() -> Path() =>
52+
Response.withBody(views.ContactsViewPage(allContacts))
53+
case DELETE() -> Path("contacts", id) =>
54+
allContacts = allContacts.filterNot(_.id == id)
55+
Response.withBody("") // empty string, remove that <tr>
56+
57+
Undertow.builder
58+
.addHttpListener(8181, "localhost")
59+
.setHandler(SharafHandler(routes))
60+
.build
61+
.start()
62+
63+
println(s"Server started at http://localhost:8181")

0 commit comments

Comments
 (0)