blob: 72ff0d38d454cd74662b1fc8763879954b7d9963 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
---
layout: demo_layout.njk
---
## Click To Load
This example shows how to implement click-to-load the next page in a table of data. The crux of the demo is
the final row:
```html
<tr id="replaceMe">
<td colspan="3">
<button class='btn' hx-get="/contacts/?page=2"
hx-target="#replaceMe"
hx-swap="outerHTML">
Load More Agents... <img class="htmx-indicator" src="/img/bars.svg">
</button>
</td>
</tr>
```
This row contains a button that will replace the entire row with the next page of
results (which will contain a button to load the *next* page of results). And so on.
{% include demo_ui.html.liquid %}
<script>
//=========================================================================
// Fake Server Side Code
//=========================================================================
// data
var dataStore = function(){
var contactId = 9;
function generateContact() {
contactId++;
var idHash = "";
var possible = "ABCDEFG0123456789";
for( var i=0; i < 15; i++ ) idHash += possible.charAt(Math.floor(Math.random() * possible.length));
return { name: "Agent Smith", email: "void" + contactId + "@null.org", id: idHash }
}
return {
contactsForPage : function(page) {
var vals = [];
for( var i=0; i < 10; i++ ){
vals.push(generateContact());
}
return vals;
}
}
}()
// routes
init("/demo", function(request, params){
var contacts = dataStore.contactsForPage(1)
return tableTemplate(contacts)
});
onGet(/\/contacts.*/, function(request, params){
var page = parseInt(params['page']);
var contacts = dataStore.contactsForPage(page)
return rowsTemplate(page, contacts);
});
// templates
function tableTemplate(contacts) {
return `<table><thead><tr><th>Name</th><th>Email</th><th>ID</th></tr></thead><tbody>
${rowsTemplate(1, contacts)}
</tbody></table>`
}
function rowsTemplate(page, contacts) {
var txt = "";
for (var i = 0; i < contacts.length; i++) {
var c = contacts[i];
txt += "<tr><td>" + c.name + "</td><td>" + c.email + "</td><td>" + c.id + "</td></tr>\n";
}
txt += loadMoreRow(page);
return txt;
}
function loadMoreRow(page) {
return `<tr id="replaceMe">
<td colspan="3">
<center>
<button class='btn' hx-get="/contacts/?page=${page + 1}"
hx-target="#replaceMe"
hx-swap="outerHTML">
Load More Agents... <img class="htmx-indicator" src="/img/bars.svg">
</button>
</center>
</td>
</tr>`;
}
</script>
|