无法在html中显示结构的切片
我正在从事一个项目,该项目要求我检索数据并将其显示在html上 数据被存储并作为结构的一部分传递给html 数据由不同的字段组成(例如名称、温度、湿度…) 数据检索部分正在工作(我已经通过打印出来并在我的html上显示Go片段来确认)。 但是,我想要实现的是根据字段在表中正确显示数据。我已经附加了我的代码,没有任何输出。我找不到错误 请帮我做这个。多谢各位 *所有名称/标签拼写正确 代码如下:无法在html中显示结构的切片,html,go,web,web-development-server,Html,Go,Web,Web Development Server,我正在从事一个项目,该项目要求我检索数据并将其显示在html上 数据被存储并作为结构的一部分传递给html 数据由不同的字段组成(例如名称、温度、湿度…) 数据检索部分正在工作(我已经通过打印出来并在我的html上显示Go片段来确认)。 但是,我想要实现的是根据字段在表中正确显示数据。我已经附加了我的代码,没有任何输出。我找不到错误 请帮我做这个。多谢各位 *所有名称/标签拼写正确 代码如下: <div style="overflow:auto;"> {{if .}
<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>
{{if.}}
时间
名称
温度
湿度
环境的
红色
绿色
蓝色
{{range.}}
{{.time_added}
{{.name}
{{.temperature}}
{{湿度}
{{.ambient}}
{{.red}
{{.green}}
{{.blue}
{{end}
{{else}
没有可用的数据
{{end}
最可能的问题是,您使用小写名称引用结构的字段。在Go中,小写字母是私有的,因此模板引擎无法查看它们
下面是一个简单的工作示例:
包干管
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)
}
}
导入(
`html/模板`
`操作系统`
)
var T=template.Must(template.New(``)).Parse(`
{{if.}}
时间
名称
{{range.}}
{{.Time_Added}
{{.Name}
{{end}
{{else}
没有可用的数据
{{end}
`))
S型结构{
添加字符串的时间
名称字符串
}
func main(){
数据:=[]*S{
&S{“今天”、“弗雷德”}和S{“昨天”、“乔”},
}
if err:=T.Execute(os.Stdout,data);nil!=err{
恐慌(错误)
}
}
如果更改为
.time\u added
或.name
,则会出现错误。因此,请务必检查T中的错误。执行最有可能的问题是,您使用小写名称引用结构的字段。在Go中,小写字母是私有的,因此模板引擎无法查看它们
下面是一个简单的工作示例:
包干管
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)
}
}
导入(
`html/模板`
`操作系统`
)
var T=template.Must(template.New(``)).Parse(`
{{if.}}
时间
名称
{{range.}}
{{.Time_Added}
{{.Name}
{{end}
{{else}
没有可用的数据
{{end}
`))
S型结构{
添加字符串的时间
名称字符串
}
func main(){
数据:=[]*S{
&S{“今天”、“弗雷德”}和S{“昨天”、“乔”},
}
if err:=T.Execute(os.Stdout,data);nil!=err{
恐慌(错误)
}
}
如果更改为.time\u added
或.name
,则会出现错误。因此,请务必检查T中的错误。执行嗨,您可以共享生成模板的代码吗?
中的内容。而且,非常重要的是,
正在生成-如果它们是空的,它们不会出现在浏览器的显示中,但它们可能在那里。因此,也许可以在每一行中添加一个Hello
,以查看是否有一个Hello
的表。嗨,您能否共享生成模板的代码:
中的内容。而且,非常重要的是,
正在生成-如果它们是空的,它们不会出现在浏览器的显示中,但它们可能在那里。因此,也许可以在每一行中添加一个Hello
,以查看是否有一个Hello
表。