如何在HTML文件中循环遍历所有产品的详细信息?

huangapple go评论82阅读模式
英文:

How to loop over all the product details in an html file?

问题

在这段代码中,我试图使用range循环遍历HTML文件中的所有产品详细信息,但是它给了我一个错误。

错误信息

在执行“body”时出错:range无法迭代{[product-names...] [product-images...] [product-links...] [product-prices...]}

controllers.go

type ProductStruct struct {
    Names   []string
    Images  []string
    Links   []string
    Prices  []string
}

func ProductsList(w http.ResponseWriter, r *http.Request) error {
    var pList ProductStruct
    for i := 0; i < len(products.AccessColumns(0)); i++ {
        pList.Names = append(pList.Names, products.AccessColumns(0)[i])
        pList.Images = append(pList.Images, products.AccessColumns(1)[i])
        pList.Links = append(pList.Links, products.AccessColumns(2)[i])
        pList.Prices = append(pList.Prices, products.AccessColumns(3)[i])
    }
    return ProductsListTmpl.Execute(w, pList)
}

product-list.html

{{range $i := .}}
<tr>
    <td class="image" data-title="No"><img src="../../static/images/{{ (index .Images $i) }}.jpg" alt="#"></td>
    <td class="product-des" data-title="Description">
        <p class="product-name"><a href="{{ (index .Links $i) }}">{{ (index .Names $i) }}</a></p>
        <p class="product-des">Maboriosam in a tonto nesciung eget  distingy magndapibus.</p>
    </td>
    <td class="price" data-title="Price"><span>${{ (index .Prices $i) }}.00 </span></td>
</tr>
{{end}}
英文:

In this code, I am trying to loop over all the product details in an HTML file using range but it is giving me an error

Error

executing &quot;body&quot; at &lt;.&gt;: range can&#39;t iterate over {[product-names...] [product-images...] [product-links...] [product-prices...]}

controllers.go

type ProductStruct struct {
	Names   []string
	Images  []string
	Links   []string
	Prices  []string
}

func ProductsList(w http.ResponseWriter, r *http.Request) error {
	var pList ProductStruct
	for i := 0; i &lt; len(products.AccessColumns(0)); i++ {
		pList.Names = append(pList.Names, products.AccessColumns(0)[i])
		pList.Images = append(pList.Images, products.AccessColumns(1)[i])
		pList.Links = append(pList.Links, products.AccessColumns(2)[i])
		pList.Prices = append(pList.Prices, products.AccessColumns(3)[i])
	}
	return ProductsListTmpl.Execute(w, pList)
}

product-list.html

{{range $i := .}}
&lt;tr&gt;
	&lt;td class=&quot;image&quot; data-title=&quot;No&quot;&gt;&lt;img src=&quot;../../static/images/{{ (index .Images $i) }}.jpg&quot; alt=&quot;#&quot;&gt;&lt;/td&gt;
	&lt;td class=&quot;product-des&quot; data-title=&quot;Description&quot;&gt;
	    &lt;p class=&quot;product-name&quot;&gt;&lt;a href=&quot;{{ (index .Links $i) }}&quot;&gt;{{ (index .Names $i) }}&lt;/a&gt;&lt;/p&gt;
		&lt;p class=&quot;product-des&quot;&gt;Maboriosam in a tonto nesciung eget  distingy magndapibus.&lt;/p&gt;
	&lt;/td&gt;
	&lt;td class=&quot;price&quot; data-title=&quot;Price&quot;&gt;&lt;span&gt;${{ (index .Prices $i) }}.00 &lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
{{end}}

答案1

得分: 0

错误发生是因为您尝试在与不相关的Names []string, Images []string, Links []string, Prices []string上进行迭代{{range $i := .}}。它们甚至可能长度不相等。

尝试重构您的解决方案,类似于以下内容(这只是一个草稿):

controllers.go

type ProductStruct struct {
    Names  string
    Images string
    Links  string
    Prices string
}

func ProductsList(w http.ResponseWriter, r *http.Request) error {
    var pList []ProductStruct
    for i := 0; i < len(products.AccessColumns(0)); i++ {
        pList = append(pList, ProductStruct{products.AccessColumns(0)[i],
            products.AccessColumns(1)[i],
            products.AccessColumns(2)[i],
            products.AccessColumns(3)[i],
        })
    }
    return ProductsListTmpl.Execute(w, pList)
}

product-list.html

{{range .}}
<tr>
    <td class="image" data-title="No"><img src="../../static/images/{{ (.Images}}.jpg" alt="#"></td>
    <td class="product-des" data-title="Description">
        <p class="product-name"><a href=".Links}}">{{.Names}}</a></p>
        <p class="product-des">Maboriosam in a tonto nesciung eget distingy magndapibus.</p>
    </td>
    <td class="price" data-title="Price"><span>${{ .Prices}}.00 </span></td>
</tr>
{{end}}
英文:

Error accrues because you trying to iterate {{range $i := .}} over not related Names []string, Images []string, Links []string, Prices []string. They can have even not equal len.

Try to refactor your solution, to have something like this(It's just a draft):

controllers.go

type ProductStruct struct {
	Names  string
	Images string
	Links  string
	Prices string
}

func ProductsList(w http.ResponseWriter, r *http.Request) error {
	var pList []ProductStruct
	for i := 0; i &lt; len(products.AccessColumns(0)); i++ {
		pList = append(pList, ProductStruct{products.AccessColumns(0)[i],
			products.AccessColumns(1)[i],
			products.AccessColumns(2)[i],
			products.AccessColumns(3)[i],
		})
	}
	return ProductsListTmpl.Execute(w, pList)
}

product-list.html

{{range .}}
&lt;tr&gt;
    &lt;td class=&quot;image&quot; data-title=&quot;No&quot;&gt;&lt;img src=&quot;../../static/images/{{ (.Images}}.jpg&quot; alt=&quot;#&quot;&gt;&lt;/td&gt;
    &lt;td class=&quot;product-des&quot; data-title=&quot;Description&quot;&gt;
        &lt;p class=&quot;product-name&quot;&gt;&lt;a href=&quot;.Links}}&quot;&gt;{{.Names}}&lt;/a&gt;&lt;/p&gt;
        &lt;p class=&quot;product-des&quot;&gt;Maboriosam in a tonto nesciung eget  distingy magndapibus.&lt;/p&gt;
    &lt;/td&gt;
    &lt;td class=&quot;price&quot; data-title=&quot;Price&quot;&gt;&lt;span&gt;${{ .Prices}}.00 &lt;/span&gt;&lt;/td&gt;
&lt;/tr&gt;
{{end}}

huangapple
  • 本文由 发表于 2022年1月22日 15:56:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/70810996.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定