Skip to content

Commit 40c9b9c

Browse files
authored
Clickable tables (#987)
1 parent 749a71f commit 40c9b9c

File tree

8 files changed

+106
-25
lines changed

8 files changed

+106
-25
lines changed

pgml-dashboard/src/components/tables/large/row/mod.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,28 @@ use sailfish::TemplateOnce;
66
#[template(path = "tables/large/row/template.html")]
77
pub struct Row {
88
columns: Vec<Component>,
9+
action: String,
10+
data: Vec<(String, String)>,
911
}
1012

1113
impl Row {
1214
pub fn new(columns: &[Component]) -> Row {
1315
Row {
1416
columns: columns.to_vec(),
17+
action: "click->tables-large-table#selectRow".to_string(),
18+
data: vec![],
1519
}
1620
}
21+
22+
pub fn action(mut self, action: &str) -> Self {
23+
self.action.push_str(&format!(" {}", action));
24+
self
25+
}
26+
27+
pub fn data(mut self, name: &str, value: &str) -> Self {
28+
self.data.push((name.to_owned(), value.to_owned()));
29+
self
30+
}
1731
}
1832

1933
component!(Row);

pgml-dashboard/src/components/tables/large/row/row.scss

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ table.table.table-lg {
66
}
77
}
88

9-
&:hover {
9+
&:hover, &.active {
1010
div.table-cell-content {
1111
background: #{$gray-800};
1212
}
@@ -34,4 +34,12 @@ table.table.table-lg {
3434
}
3535
}
3636
}
37+
38+
&.selectable {
39+
tbody {
40+
tr:hover {
41+
cursor: pointer;
42+
}
43+
}
44+
}
3745
}

pgml-dashboard/src/components/tables/large/row/template.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
<tr>
1+
<tr
2+
data-action="<%= action %>"
3+
data-tables-large-table-target="row"
4+
<% for (name, value) in data { %>
5+
data-<%= name %>="<%= value %>"
6+
<% } %>
7+
>
28
<% for column in columns { %>
39
<td>
410
<div class="table-cell-content">

pgml-dashboard/src/components/tables/large/table/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,22 @@ use sailfish::TemplateOnce;
77
pub struct Table {
88
rows: Vec<Row>,
99
headers: Vec<String>,
10+
classes: String,
1011
}
1112

1213
impl Table {
1314
pub fn new(headers: &[impl ToString], rows: &[Row]) -> Table {
1415
Table {
1516
headers: headers.iter().map(|h| h.to_string()).collect(),
1617
rows: rows.to_vec(),
18+
classes: "table table-lg".to_string(),
1719
}
1820
}
21+
22+
pub fn selectable(mut self) -> Self {
23+
self.classes.push_str(" selectable");
24+
self
25+
}
1926
}
2027

2128
component!(Table);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Controller } from '@hotwired/stimulus'
2+
3+
export default class extends Controller {
4+
static targets = ['row']
5+
6+
selectRow(event) {
7+
this.rowTargets.forEach(row => row.classList.remove('active'))
8+
event.currentTarget.classList.add('active')
9+
}
10+
}

pgml-dashboard/src/components/tables/large/table/template.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<table class="table table-lg">
1+
<table class="<%= classes %>" data-controller="tables-large-table">
22
<thead>
33
<tr>
44
<% for header in headers { %>
@@ -11,4 +11,4 @@
1111
<%+ row %>
1212
<% } %>
1313
</tbody>
14-
</div>
14+
</table>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Controller } from '@hotwired/stimulus'
2+
3+
export default class extends Controller {
4+
selectRow(event) {
5+
console.log('dataset: ', event.currentTarget.dataset)
6+
}
7+
}

pgml-dashboard/templates/content/playground.html

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
<% use crate::components::*; %>
1+
<% use crate::components::*;
2+
use crate::components::tables::large::*;
3+
use crate::components::navigation::tabs::*;
4+
%>
25

3-
<div class="min-height: 100vh;">
6+
<div class="min-height: 100vh;" data-controller="playground">
47
<h3 class="h3">Playground</h3>
58

69
<div class="mb-3">
@@ -37,26 +40,52 @@ <h3 class="h3">Playground</h3>
3740
</div>
3841
</div>
3942
<div class="mb-3">
40-
<%+ navigation::tabs::Tabs::new(&vec![
41-
navigation::tabs::Tab::new(
42-
"Test tab",
43-
"Test content".into()
44-
),
45-
navigation::tabs::Tab::new(
46-
"Second tab",
47-
"Second tab content".into()
48-
),
49-
navigation::tabs::Tab::new(
50-
"Third active tab",
51-
"Hello third tab".into(),
52-
),
53-
]).active_tab("Third active tab") %>
43+
<%+ Tabs::new(
44+
&[
45+
Tab::new(
46+
"Test tab",
47+
Table::new(
48+
&["Date", "Time", "Status"],
49+
&[
50+
Row::new(&[
51+
"01/01/2022".into(),
52+
"20:00:43.956373 +00:00:00 UTC".into(),
53+
"Ready".into()
54+
])
55+
.action("click->playground#selectRow")
56+
.data("row-id", "1"),
57+
58+
Row::new(&[
59+
"01/01/2022".into(),
60+
"20:00:43.956373 +00:00:00 UTC".into(), "Ready".into()
61+
])
62+
.action("click->playground#selectRow")
63+
.data("row-id", "2"),
64+
65+
Row::new(&[
66+
"01/01/2022".into(),
67+
"20:00:43.956373 +00:00:00 UTC".into(), "Ready".into()
68+
])
69+
.action("click->playground#selectRow")
70+
.data("row-id", "3"),
71+
72+
])
73+
.selectable()
74+
.into()
75+
),
76+
Tab::new(
77+
"Second tab",
78+
"Second tab content".into()
79+
),
80+
Tab::new(
81+
"Third active tab",
82+
"Hello third tab".into(),
83+
),
84+
]
85+
)
86+
.active_tab("Test tab") %>
5487
</div>
5588
<div class="mb-3">
56-
<%+ tables::large::Table::new(&["Date", "Time", "Status"], &vec![
57-
tables::large::Row::new(&["01/01/2022".into(), "20:00:43.956373 +00:00:00 UTC".into(), "Ready".into()]),
58-
tables::large::Row::new(&["01/01/2022".into(), "20:00:43.956373 +00:00:00 UTC".into(), "Ready".into()]),
59-
tables::large::Row::new(&["01/01/2022".into(), "20:00:43.956373 +00:00:00 UTC".into(), "Ready".into()]),
60-
]) %>
89+
6190
</div>
6291
</div>

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy