如何使用我的结构从切片显示表格 [英] How to show a table from a slice with my struct

查看:28
本文介绍了如何使用我的结构从切片显示表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示一个表格,每一行都包含我的结构数据.

这是我的结构:

  type My_Struct struct {FIRST_FIELD字符串SECOND_FIELD字符串THIED_FIELD字符串} 

这是我的html代码:

 <表id ="t01">< tr><第一场</th>< th>第二字段</th><第三>第三场</tr>< tr>< td> FIRST_OBJ_HERE_SHOULD_BE_THE_FIRST_FIELD</td>< td> FIRST_OBJ_HERE_SHOULD_BE_THE_SECOND_FIELD</td>< td> FIRST_OBJ_HERE_SHOULD_BE_THE_THIRD_FIELD</td></tr>< tr>< td> SECOND_OBJ_HERE_SHOULD_BE_THE_FIRST_FIELD</td>< td> SECOND_OBJ_HERE_SHOULD_BE_THE_SECOND_FIELD</td>< td> SECOND_OBJ_HERE_SHOULD_BE_THE_THIRD_FIELD</td></tr></table> 

如您所见,我想将包含结构的切片(每个包含3个文件)传递给此html代码,并且希望将整个切片设置在此表中-每行包含一个struct数据./p>

我该如何实现?

解决方案

似乎您想要进入模板包.

这是一个如何使用它的示例:定义一个处理程序,该处理程序将带有一些已定义字段的结构实例传递给使用Go模板的视图:

  type MyStruct struct {SomeField字符串}func MyStructHandler(w http.ResponseWriter,r * http.Request){ms:= MyStruct {SomeField:"Hello Friends",}t:= template.Must(template.ParseFiles("./showmystruct.html"))t.Execute(w,ms)} 

在视图(showmystruct.html)中使用Go Template语法访问结构字段:

 <!DOCTYPE html>< title>显示我的结构</title>< h1> {{.SomeField}}</h1> 

更新

如果您特别希望传递列表并对其进行遍历,则 {{range}} 关键字非常有用.另外,有一种非常常见的模式(至少在我的世界中),您可以将 PageData {} 结构传递给视图.

这是一个扩展的示例,添加了一个结构列表和一个 PageData 结构(以便我们可以在模板中访问其字段):

  type MyStruct struct {SomeField字符串}输入PageData struct {标题字符串数据[] MyStruct}func MyStructHandler(w http.ResponseWriter,r * http.Request){数据:= PageData {标题:我的超棒结构页面",数据:[] MyStruct {MyStruct {SomeField:"Hello Friends",},MyStruct {SomeField:再见朋友",},}t:= template.Must(template.ParseFiles("./showmystruct.html"))t.Execute(w,数据)} 

以及修改后的模板(showmystruct.html):

 <!DOCTYPE html>< title> {{.Title}}</title>< ul>{{range .Data}}< li> {{.SomeField}}</li>{{ 结尾 }}</ul> 

I want to show a table that each row contains my struct data.

Here is my struct:

type My_Struct struct {
FIRST_FIELD       string
SECOND_FIELD      string
THIED_FIELD       string
}

Here is my html code:

<table id="t01">
<tr>
    <th>FIRST FIELD</th>
    <th>SECOND FIELD</th>
    <th>THIRD FIELD</th>
</tr>
<tr>
    <td>FIRST_OBJ_HERE_SHOULD_BE_THE_FIRST_FIELD</td>
    <td>FIRST_OBJ_HERE_SHOULD_BE_THE_SECOND_FIELD</td>
    <td>FIRST_OBJ_HERE_SHOULD_BE_THE_THIRD_FIELD</td>
</tr>

<tr>
    <td>SECOND_OBJ_HERE_SHOULD_BE_THE_FIRST_FIELD</td>
    <td>SECOND_OBJ_HERE_SHOULD_BE_THE_SECOND_FIELD</td>
    <td>SECOND_OBJ_HERE_SHOULD_BE_THE_THIRD_FIELD</td>
</tr>

</table>

As you see, I want to pass a slice with my struct (each one contains 3 files) to this html code, and I want the the whole slice will be set in this table - each row contains one struct data.

How can I achieve this?

解决方案

It seems like you want the Go Template package.

Here's an example of how you may use it: Define a handler that passes an instance of a struct with some defined field(s) to a view that uses Go Templates:

type MyStruct struct {
        SomeField string
}

func MyStructHandler(w http.ResponseWriter, r *http.Request) {
        ms := MyStruct{
                SomeField: "Hello Friends",
        }

        t := template.Must(template.ParseFiles("./showmystruct.html"))
t.Execute(w, ms)
}

Access the struct fields using the Go Template syntax in your view (showmystruct.html):

<!DOCTYPE html>
<title>Show My Struct</title>
<h1>{{ .SomeField }}</h1>

Update

If you are interested particularly in passing a list, and iterating over that, then the {{ range }} keyword is useful. Also, there's a pretty common pattern (at least in my world) where you pass a PageData{} struct to the view.

Here's an expanded example, adding a list of structs, and a PageData struct (so we can access its fields in the template):

type MyStruct struct {
    SomeField string
}

type PageData struct {
    Title string
    Data []MyStruct
}

func MyStructHandler(w http.ResponseWriter, r *http.Request) {
        data := PageData{
            Title: "My Super Awesome Page of Structs",
            Data: []MyStruct{
                MyStruct{
                    SomeField: "Hello Friends",
                },
                MyStruct{
                    SomeField: "Goodbye Friends",
                },
            }

        t := template.Must(template.ParseFiles("./showmystruct.html"))
        t.Execute(w, data)

}

And the modified template (showmystruct.html):

<!DOCTYPE html>
<title>{{ .Title }}</title>
<ul>
  {{ range .Data }}
    <li>{{ .SomeField }}</li>
  {{ end }}
</ul>

这篇关于如何使用我的结构从切片显示表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆