golang 动态生成表头及数据

golang 动态生成表头及数据,第1张

概述golang的struct不支持界面展示的嵌套循环,所以采用map数据结构来存储数据。直接看代码和效果。 用的beego框架 服务端代码: ` func (this *TableController) Get() {    this.Data["Thead"] = []string{"#", "名称", "年龄"} //表头数据结构    this.Data["Tbody"] = []stri

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]=personMapperson[1]=personMap1fmt.Println(person)this.Data["Person"]=personthis.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

总结

以上是内存溢出为你收集整理的golang 动态生成表头及数据全部内容,希望文章能够帮你解决golang 动态生成表头及数据所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1286546.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-09
下一篇 2022-06-09

发表评论

登录后才能评论

评论列表(0条)

保存