dongsechuan0535 2019-07-07 15:24
浏览 53

转到html模板表

I want to make the table in HTML in go package "temlate" and i want to add rows in loop, but i didn't find how doing this

my code:

package main
import (
     "net/http"
     "html/template"
)
type Devicevalue_view struct {
    Devicetype string
    Iddevice   string
    Devicename string
    Oidname    string
    Value      string
  }
func page_1(w http.ResponseWriter, r *http.Request){
    for i:=1; i<10; i++{    
        data := Devicevalue_view{
            Devicetype: "devicetype",
            Iddevice: "iddevice",
            Devicename: "devicename",
            Oidname: "oidname",
            Value: "value",
        }   
        tmpl, _ := template.ParseFiles("./index.html")
        tmpl.Execute(w, data)
    }   
}
func main() {
    http.HandleFunc("/table", page_1) 
    http.ListenAndServe(":3000", nil)
}

i'm getting this :


Devices
Type    Name    Param   Time    Value
devicetype  iddevice    devicename  oidname value
Devices
Type    Name    Param   Time    Value
devicetype  iddevice    devicename  oidname value
...

but i want somthing like this

Devices
Type    Name    Param   Time    Value
devicetype  iddevice    devicename  oidname value
devicetype  iddevice    devicename  oidname value
...

I don't undestand how connect all cells in one table

index.html: https://drive.google.com/file/d/1HzEL0i3VhiafPzlV8iC0kU8WaSQwoYZY/view?usp=sharing

  • 写回答

1条回答 默认 最新

  • doumei8258 2019-07-07 18:01
    关注

    Because you are executing template inside for loop. Also you can pass a single struct. To pass array you have to pass it as a member of the struct.

    package main
    
    import (
        "html/template"
        "net/http"
    )
    
    type Data struct {
        Items []Devicevalue_view
    }
    
    type Devicevalue_view struct {
        Devicetype string
        Iddevice   string
        Devicename string
        Oidname    string
        Value      string
    }
    
    func page_1(w http.ResponseWriter, r *http.Request) {
        data := Data{}
        for i := 1; i < 10; i++ {
            view := Devicevalue_view{
                Devicetype: "devicetype",
                Iddevice:   "iddevice",
                Devicename: "devicename",
                Oidname:    "oidname",
                Value:      "value",
            }
    
            data.Items = append(data.Items, view)
        }
    
        tmpl, _ := template.ParseFiles("./index.html")
        tmpl.Execute(w, data)
    }
    func main() {
        http.HandleFunc("/table", page_1)
        http.ListenAndServe(":3000", nil)
    }
    

    Also you have to iterate through data and generate row dynamically.

    <!DOCTYPE html>
    <html lang="en">
    <body>
    <table>
        <tr>
            <th>Type</th>
            <th>Name</th>
            <th>Param</th>
            <th>Time</th>
            <th>Value</th>
        </tr>
        {{ range .Items}}
            <tr>
                <td>{{ .Devicetype }}</td>
                <td>{{ .Iddevice }}</td>
                <td>{{ .Devicename }}</td>
                <td>{{ .Oidname }}</td>
                <td>{{ .Value }}</td>
            </tr>
        {{ end}}
    </table>
    </body>
    </html>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)