-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add real-time database changes to html5 table view example
- Loading branch information
Showing
9 changed files
with
534 additions
and
8 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,21 @@ | ||
module github.com/kataras/pg_examples/live-table-reload | ||
|
||
go 1.21.3 | ||
|
||
replace github.com/kataras/pg => ../../ | ||
|
||
require ( | ||
github.com/gorilla/websocket v1.5.0 | ||
github.com/kataras/pg v1.0.6 | ||
) | ||
|
||
require ( | ||
github.com/gertd/go-pluralize v0.2.1 // indirect | ||
github.com/jackc/pgpassfile v1.0.0 // indirect | ||
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect | ||
github.com/jackc/pgx/v5 v5.4.3 // indirect | ||
github.com/jackc/puddle/v2 v2.2.1 // indirect | ||
golang.org/x/crypto v0.14.0 // indirect | ||
golang.org/x/sync v0.4.0 // indirect | ||
golang.org/x/text v0.13.0 // indirect | ||
) |
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,32 @@ | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/gertd/go-pluralize v0.2.1 h1:M3uASbVjMnTsPb0PNqg+E/24Vwigyo/tvyMTtAlLgiA= | ||
github.com/gertd/go-pluralize v0.2.1/go.mod h1:rbYaKDbsXxmRfr8uygAEKhOWsjyrrqrkHVpZvoOp8zk= | ||
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= | ||
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= | ||
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= | ||
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= | ||
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk= | ||
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= | ||
github.com/jackc/pgx/v5 v5.4.3 h1:cxFyXhxlvAifxnkKKdlxv8XqUf59tDlYjnV5YYfsJJY= | ||
github.com/jackc/pgx/v5 v5.4.3/go.mod h1:Ig06C2Vu0t5qXC60W8sqIthScaEnFvojjj9dSljmHRA= | ||
github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk= | ||
github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= | ||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= | ||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= | ||
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc= | ||
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4= | ||
golang.org/x/sync v0.4.0 h1:zxkM55ReGkDlKSM+Fu41A+zmbZuaPVbGMzvvdUPznYQ= | ||
golang.org/x/sync v0.4.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= | ||
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k= | ||
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
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,116 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>PG Real-Time Data</title> | ||
<style> | ||
body { | ||
font-family: Arial, sans-serif; | ||
background-color: #1a1a1a; | ||
color: #ffffff; | ||
margin: 0; | ||
padding: 0; | ||
} | ||
|
||
.container { | ||
max-width: 800px; | ||
margin: 0 auto; | ||
padding: 20px; | ||
} | ||
|
||
h1 { | ||
font-size: 2rem; | ||
margin-bottom: 20px; | ||
} | ||
|
||
table { | ||
width: 100%; | ||
border-collapse: collapse; | ||
margin-top: 20px; | ||
} | ||
|
||
th, | ||
td { | ||
border: 1px solid #ffffff; | ||
padding: 10px; | ||
text-align: left; | ||
} | ||
|
||
th { | ||
background-color: #333333; | ||
} | ||
</style> | ||
<script> | ||
var socket = new WebSocket("ws://localhost:8080/websocket"); | ||
socket.onopen = function (event) { | ||
socket.send(JSON.stringify({ | ||
text: "ask_view" | ||
})); | ||
}; | ||
socket.onmessage = function (event) { | ||
var data = JSON.parse(event.data); | ||
console.log(data); | ||
|
||
switch (data.type) { | ||
case "view": | ||
// Replace the entire table body. | ||
document.querySelector("tbody").innerHTML = data.data.map(function (row) { | ||
return `<tr data-id="${row.id}"><td>${row.id}</td><td>${row.username}</td><td>${row.email}</td></tr>`; | ||
}).join(""); | ||
break; | ||
case "insert": | ||
console.log(`insert ${data.data.id}`); | ||
var newRow = `<tr><td>${data.data.id}</td><td>${data.data.username}</td><td>${data.data.email}</td></tr>`; | ||
document.querySelector("tbody").insertAdjacentHTML("beforeBegin" /* or beforeEnd */, newRow); | ||
break; | ||
case "update": | ||
console.log(`update ${data.data.id}`); | ||
// Update an existing row (find by ID) | ||
var existingRow = document.querySelector(`tr[data-id="${data.data.id}"]`); | ||
if (existingRow) { | ||
existingRow.innerHTML = `<td>${data.data.id}</td><td>${data.data.username}</td><td>${data.data.email}</td>`; | ||
} | ||
break; | ||
case "delete": | ||
console.log(`delete ${data.id}`); | ||
// Remove a row (find by ID) | ||
var rowToRemove = document.querySelector(`tr[data-id="${data.data.id}"]`); | ||
if (rowToRemove) { | ||
rowToRemove.remove(); | ||
} | ||
break; | ||
default: | ||
console.log(`Don't know how to handle type '${data.type}'`); | ||
} | ||
}; | ||
</script> | ||
</style> | ||
</head> | ||
|
||
<body> | ||
<div class="container"> | ||
<h1>Futuristic Minimalist Template</h1> | ||
<table> | ||
<thead> | ||
<tr> | ||
<th>ID</th> | ||
<th>Username</th> | ||
<th>Email</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<!-- Replace with dynamic data --> | ||
<!-- Example row: | ||
<tr data-id="1"> | ||
<td>1</td> | ||
<td>JohnDoe</td> | ||
<td>[email protected]</td> | ||
</tr> --> | ||
</tbody> | ||
</table> | ||
</div> | ||
</body> | ||
|
||
</html> |
Oops, something went wrong.