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> <table boder="1px solid black"> {{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
有疑问加站长微信联系(非本文作者)