简体   繁体   中英

Unable to display slice of structs in html

I am working on a project which requires me to retrieve data and display it on a html.

The data is stored and pass to the html as slice of structs.

The data consists of different fields (eg. name, temperature, humidity ...) The data retrieval part is working (I have confirm by printing it out and displaying the Go slice on my html). However, what I want to achieve is displaying the data properly in a table according to their fields. I have attached my code and there isn't any output. I couldn't find the errors.

Please help me with this. Thank you

*PS All the names/labels are spelled correctly.

这是输出的屏幕截图

This is the code:

<div style="overflow:auto;">
        {{if .}}

        <table class="table table-striped">
                <thead>
                  <tr style="text-align: center;">
                    <th scope="col">Time</th>
                    <th scope="col">Name</th>
                    <th scope="col">Temperature</th>
                    <th scope="col">Humidity</th>
                    <th scope="col">Ambient</th>
                    <th scope="col">Red</th>
                    <th scope="col">Green</th>
                    <th scope="col">Blue</th>
                  </tr>
                </thead>
                <tbody>



        {{range .}}

        <tr style="text-align: center;">

          <td>{{.time_added}}</td>
          <td>{{.name}}</td>
          <td>{{.temperature }}</td>
          <td>{{.humidity }}</td>
          <td>{{.ambient }}</td>
          <td>{{.red }}</td>
          <td>{{.green}}</td>
          <td>{{.blue}}</td>
        </tr>
        {{end}}
    </tbody>

  </table>
    {{else}}
        <h1 style="font-size: 3vh;margin-top:10vh;">No data available</h1>
    {{end}}



  </div>

Most likely, the issue is that you are referring to the fields of your struct by lower-case names. In Go, lower-case is private, so there's no way for the template engine to see them.

Here's a simple, working example:

package main

import (
    `html/template`
    `os`
)

var T = template.Must(template.New(``).Parse(`
        {{if .}}
        <table class="table table-striped">
                <thead>
                  <tr style="text-align: center;">
                    <th scope="col">Time</th>
                    <th scope="col">Name</th>
                  </tr>
                </thead>
                <tbody>

        {{range .}}
                <tr style="text-align: center;">
                  <td>{{.Time_Added}}</td>
                  <td>{{.Name}}</td>
                </tr>
                {{end}}
            </tbody>
          </table>
    {{else}}
        <h1 style="font-size: 3vh;margin-top:10vh;">No data available</h1>
    {{end}}
`))

type S struct {
    Time_Added string
    Name string
}

func main() {
    data := []*S{
        &S{"today","fred"}, &S{"yesterday","joe"},
    }
    if err := T.Execute(os.Stdout, data); nil!=err {
        panic(err)
    }
}

If you change to .time_added or .name , you will get an error. So be sure to check for an error from T.Execute .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM