加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

golang 动态生成表头及数据

发布时间:2020-12-16 18:38:41 所属栏目:大数据 来源:网络整理
导读:golang的struct不支持界面展示的嵌套循环,所以采用map数据结构来存储数据。直接看代码和效果。 用的beego框架 服务端代码: ` func(this*TableController)Get(){this.Data["Thead"]=[]string{"#","名称","年龄"}//表头数据结构this.Data["Tbody"]=[]string{

golang的struct不支持界面展示的嵌套循环,所以采用map数据结构来存储数据。直接看代码和效果。 用的beego框架 服务端代码: `

func(this*TableController)Get(){
this.Data["Thead"]=[]string{"#","名称","年龄"}//表头数据结构
this.Data["Tbody"]=[]string{"Id","Name","Age"}//对应字段值
personMap:=make(map[string]string)
personMap["Id"]="1"
personMap["Name"]="lisi"
personMap["Age"]="25"
personMap1:=make(map[string]string)
personMap1["Id"]="2"
personMap1["Name"]="zhangsan"
personMap1["Age"]="28"
person:=make(map[int64]map[string]string)
person[0]=personMap
person[1]=personMap1
fmt.Println(person)
this.Data["Person"]=person
this.TplNames="table.tpl"
}

` 前端测试代码:

<html>
<head>
	<title>table</title>
</head>
<body>
	<tableboder="1pxsolidblack">
		{{range$k,$v:=.Thead}}
		<th>{{$v}}</th>
		{{end}}
		{{range$k,$person:=.Person}}
		<tr>
			{{range$.Tbody}}
				<td>{{index$person.}}</td>
			{{end}}
		</tr>
		{{end}}
	</table>
</body>
</html>


最后效果 :

# 名称 年龄

1 lisi 25

2 zhangsan 28

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读