title: go语言gin框架使用for渲染html表格数据
date: 2023-02-28 22:26:18.0
updated: 2023-05-05 17:55:35.0
url: https://liumou.site/doc/621
categories:

  • GO
  • 后端
  • Gin
    tags:
  • Go

效果

image-1683280528625

源码

go

type data struct {
    Name     string // 插件名称
    Standard string // 标准版本
    Local    string // 本地版本
    Package  string // 包名
    Result   string // 对比结果
}

func (api *User) searchHtml() {
    dataList := []data{
        {"Wps", "10489", "10489", "wps", "正确"},
        {"Wps", "10489", "10489", "wps", "正确"},
        {"Wps", "10489", "10489", "wps", "正确"},
        {"Wps", "10489", "10489", "wps", "正确"},
        {"Wps", "10489", "10489", "wps", "正确"},
        {"Wps", "10489", "10489", "wps", "正确"},
        {"Wps", "10489", "10489", "wps", "正确"},
        {"Wps", "10489", "10489", "wps", "正确"},
        {"Wps", "10489", "10489", "wps", "正确"},
        {"Wps", "10489", "10489", "wps", "正确"},
        {"Wps", "10489", "10489", "wps", "正确"},
        {"Wps", "10489", "10489", "wps", "正确"},
        {"Wps", "10489", "10489", "wps", "正确"},
        {"Wps", "10489", "10489", "wps", "正确"},
    }
    api.Route.GET("/cha", func(c *gin.Context) {
        c.HTML(http.StatusOK, "index.html", gin.H{
            "title": "查询结果",
            "data":  dataList,
        })
    })
}

html

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta name="viewport" content="width=device-width" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>{{.title}}</title>
    <link rel="stylesheet" href="/static/css/install.css" type="text/css" />
</head>
<body>
    <table>
        <tr>
            <th>插件名称</th><th>标准版本</th><th>本地版本</th><th>包名</th><th>对比结果</th>
        </tr>
        {{ range .data }}
        <tr>
            <td>{{ .Name }}</td><td>{{ .Standard }}</td><td>{{ .Local }}</td><td>{{ .Package }}</td><td>{{ .Result }}</td>
        </tr>
        {{ end }}
    </table>
</body>
</html>