英文:
How to write incrementally to html template in go
问题
我正在构建一个名为Mavgo Flight的基于Web的数据浏览器。我希望从sqlite中获取的大型表能够连续打印,而不是默认的只有在所有数据可用时才打印的行为。我尝试了按行运行模板的方法,但失败了。
func renderHTMLTable(w http.ResponseWriter, result *sqlx.Rows) {
cols, err := result.Columns()
if err != nil {
log.Println(err, "renderHTMLTable")
return
}
tmpl, err := template.ParseFiles("./templates/2d.html")
if err != nil {
log.Println("template failed", err)
return
}
data := HTMLTable{}
data.Cols = cols
for result.Next() {
cols, err := result.SliceScan()
if err != nil {
log.Println(w, err)
break
}
s := make([]string, len(cols))
for i, v := range cols {
s[i] = fmt.Sprint(v)
}
tmpl.Execute(w, s)
}
}
以上是你提供的代码。
英文:
I'm building a web based data browser called Mavgo Flight.
I want large tables from sqlite to print continuously instead of the default behavior of only printing when all the data is available. I tried running the template per row of data which fails.
func renderHTMLTable(w http.ResponseWriter, result *sqlx.Rows) {
cols, err := result.Columns()
if err != nil {
log.Println(err, "renderHTMLTable")
return
}
tmpl, err := template.ParseFiles("./templates/2d.html")
if err != nil {
log.Println("template failed", err)
return
}
data := HTMLTable{}
data.Cols = cols
for result.Next() {
cols, err := result.SliceScan()
if err != nil {
log.Println(w, err)
break
}
s := make([]string, len(cols))
for i, v := range cols {
s[i] = fmt.Sprint(v)
}
tmpl.Execute(w, s)
}
}
答案1
得分: 0
我放弃了聪明才智,完全按照Cerise的建议去做了
逐行编写的函数:
func renderHTMLTable(w http.ResponseWriter, result *sqlx.Rows) {
cols, err := result.Columns()
if err != nil {
log.Println(err, "renderHTMLTable")
return
}
head, err := template.ParseFiles("./templates/head.html")
if err != nil {
log.Println("template failed", err)
return
}
row, err := template.ParseFiles("./templates/row.html")
if err != nil {
log.Println("template failed", err)
return
}
foot := ` </tbody>
</table>
</div>
</body>
</html>`
head.Execute(w, cols)
s := make([]string, len(cols))
for result.Next() {
values, err := result.SliceScan()
if err != nil {
log.Println(w, err)
break
}
for i, v := range values {
s[i] = fmt.Sprint(v)
}
row.Execute(w, s)
}
fmt.Fprint(w, foot)
}
英文:
I gave up on being clever did exactly what Cerise suggested
The function that writes rows incrementally:
func renderHTMLTable(w http.ResponseWriter, result *sqlx.Rows) {
cols, err := result.Columns()
if err != nil {
log.Println(err, "renderHTMLTable")
return
}
head, err := template.ParseFiles("./templates/head.html")
if err != nil {
log.Println("template failed", err)
return
}
row, err := template.ParseFiles("./templates/row.html")
if err != nil {
log.Println("template failed", err)
return
}
foot := ` </tbody>
</table>
</div>
</body>
</html>`
head.Execute(w, cols)
s := make([]string, len(cols))
for result.Next() {
values, err := result.SliceScan()
if err != nil {
log.Println(w, err)
break
}
for i, v := range values {
s[i] = fmt.Sprint(v)
}
row.Execute(w, s)
}
fmt.Fprint(w, foot)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论