Web App Issue Getting Data

Newbie here . . .inherited some Go code to work on a proof-of-concept idea, have hit a {hopefully small} stumbling block.

We used the default libraries to stand up a simple Go web app that initially just displays a/form to get the user to enter a string.

I have the handler handling the GETs and POSTs all working for this.

On the POST, i do a ParseForm and successfully extract this value.

Channels and goroutines are then used to pass the string on to backend Go processing, which populates different in-memory data structures containing various stats {eventually a db}.

Once i get the “I’m done” message from the channel, the web app is meant to transition to a default ‘dashboard’ and display some summary data. One of the dashboard panels is a dynamically built table. This table is to be populated with the data from 1 of the above in-memory data structures…

…and this is where i’m stuck.

On the HTML dashboard page, I have all the javascript functions already working that build a dynamic table. For now, I’m using a hard-coded dataset, eg 2-dimensional array, representing what the Go backend will create, inside the JS file so that I can work on building the table while the Go backend code is worked on.

That’s all done and working like we like.

The piece i don’t know how to implement is how to get the data from the running handler into the form.

How do i format the template and/or the call to the template.execute(…) fcn to provide a ‘complicated’ data structure which when the new page is displayed, will pass the data structure on to the above working javascript function to build the dynamic table.

Any helpful suggestions, links, etc would be appreciated.
Thank you.

Hey!

I recommend simply sending the data through the ExecuteTemplate function like so:

request.ParseForm()

input := request.Form.Get("data")

var output []TableData = process_data(input)

template.ExecuteTemplate(responseWriterName, "dashboard.html", output) 

Then, use the template loops to send the data you have in there to your table:
( REMEMBER TO USE CAPITAL FIRST LETTER FOR YOUR STRUCT’S DATA. )
type TableData struct {
Id string
Firstname string
Lastname string
Height string
Weight string
}

<html>

<table>
  <tr>
    <th>ID</th>
    <th>NAME</th>
    <th>SURNAME</th>
    <th>HEIGHT</th>
    <th>WEIGHT</th>
  </tr>
  
{{range $row := .}}
 <tr>
    <td>$row.Id</td>
    <td>$row.Firstname</td>
    <td>$row.Lastname</td>
    <td>$row.Height</td>
    <td>$row.Weight</td>
  </tr>

{{ end }}

</table>
</html>

Create a http endpoint which will serve the data in your table in JSON form, and get your javascript to issue an AJAX request to get the data from this endpoint and use it to populate your table.

Hi, hope you have the solution of your queries. If not, you can view this sample project. It’s a simple blog application with mysql database. It has the templates, forms and database connectivity in single project. Hope this help.


for mysql

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.