【问题标题】:Multiple Data Sending form Go file to Html [duplicate]多个数据发送表单Go文件到Html [重复]
【发布时间】:2020-06-29 17:37:06
【问题描述】:

有两种结构:

    Offers   := "10% Discunt"
    SelectUsers := "ALL"
    OpenDate    := "29-06-2020"
    closedDate   := "29-07-2020"

}

type ScanInfo struct{
    IdScan    := "1212"
    Longitude := "98.323202"
    Latitude:= "34.432201"

}

Go方法:


http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        
tmpl := template.Must(template.ParseFiles("home.html"))
        var scans []ScanInfo
        var infos []info

        tmpl.Execute(w, struct{ ScanData []ScanInfo }{scans})
    })

我在html表中获取单一类型数据的方法是:

 <tbody>
    {{ range .ScanData }}
       <tr>
          <th>{{.IdScan}}</th>
          <th>{{.Longitude}}</th>
          <th>{{.Latitude}}</th>
        </tr>
      {{ end }}
 </tbody>

现在我想一次将结构的 (info & ScanInfo's) 数据发送到 HTML 表单 (home.html) 中。 无论如何,请建议我发送此数据,以及如何将这些值输入 HTML 页面。

【问题讨论】:

标签: html go


【解决方案1】:

为这些情况创建映射而不是结构更容易:

 tmpl.Execute(w, map[string]interface{}{"ScanData":scans,"InfoData":infos})

在您的模板中,您可以使用两个顶级变量 {{.ScanData}}{{.InfoData}}

【讨论】:

    猜你喜欢
    • 2015-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-25
    • 2023-04-07
    • 2020-12-27
    相关资源
    最近更新 更多