-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #68 from ihasidul/delete-table-row
Delete Row - Demonstrates row deletion in a table
- Loading branch information
Showing
5 changed files
with
61 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from fasthtml.common import * | ||
|
||
app, rt = fast_app() | ||
|
||
# This represents the data we are rendering | ||
# The data could original from a database, or any other datastore | ||
@dataclass | ||
class Contact: | ||
# Data points | ||
id: int | ||
name: str | ||
email: str | ||
status: str | ||
|
||
def __ft__(self): | ||
# __ft__ method is used by FastHTML to render an item in the UI | ||
# By defining this, a `Contact` will show up as a table row automatically | ||
return Tr( | ||
*map(Td, (self.name, self.email, self.status)), | ||
Td(Button('Delete', | ||
hx_delete=delete.to(id=self.id).lstrip('/'), | ||
# Give a confirmation prompt before deleting | ||
hx_confirm="Are you sure?", | ||
# Target the closest row (The one you clicked on) | ||
hx_target="closest tr", | ||
# Swap out the row with nothing (removes the row) | ||
hx_swap="outerHTML"))) | ||
|
||
# Sample data | ||
# Often this would come from a database | ||
contacts = [{'id':1, 'name': "Bob Deer", 'email': "[email protected]", 'status': "Active" }, | ||
{'id':2, 'name': "Jon Doe", 'email': "[email protected]", 'status': "Inactive"}, | ||
{'id':3, 'name': "Jane Smith",'email': "[email protected]",'status': "Active" }] | ||
|
||
@rt | ||
def index(sess): | ||
# Save a copy of contacts in your session | ||
# This is the demo doesn't conflict with other users | ||
sess['contacts'] = contacts | ||
# Create initial table | ||
return Table( | ||
Thead(Tr(*map(Th, ["Name", "Email", "Status", ""]))), | ||
# A `Contact` object is rendered as a row automatically using the `__ft__` method | ||
Tbody(*(Contact(**x) for x in sess['contacts']))) | ||
|
||
|
||
@app.delete | ||
def delete(id: int, sess): | ||
sess['contacts'] = [c for c in sess['contacts'] if c['id'] != id] | ||
# Return nothing to the HTMX request to swap in nothing (removes the row) | ||
|
||
|
||
serve() |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions
6
examples/dynamic_user_interface_(htmx)/delete_row/metadata.ini
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[REQUIRED] | ||
ImageAltText=Delete row | ||
ComponentName=Delete Row | ||
ComponentDescription=Delete a row from table via HTMX | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters