如何在 Revel 模板中更改动态变量

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

How to change dynamic variable in Revel Template

问题

在我的revel项目中,我有一个选择选项,需要在点击提交按钮后被选中。

如果我像这样做,它可以工作,因为.OptionType是一个字符串,所以我需要将它与字符串进行比较:

<select class="chosen form-control" id="selectType">
	<option disabled value='' style="display:none"> --------- </option>
	<option {{ if eq .OptionType "1" }}selected{{end}} value="1">Option A</option>
	<option {{ if eq .OptionType "2" }}selected{{end}} value="2">Option B</option>
	<option {{ if eq .OptionType "3" }}selected{{end}} value="3">Option C</option>
</select>

但是,如果我尝试像.ID这样比较变量,它会抛出一个错误:

<select class="chosen form-control" id="selectValue">
	<option {{ if eq .SelectedValue "all" }} selected {{end}} value='all'>
		All Value
	</option>
	{{range .Circles}}
		<option {{ if eq .SelectedValue .ID }} selected {{end}} value='{{.ID}}'>
			{{.Name}}
		</option>
	{{end}}
</select>

有没有办法在同一行中将.ID转换为if语句?

错误信息:

在执行"BackendReport/Finance.html"时,无法在models.Value类型中评估字段SelectedValue

英文:

In my revel project, i have a select option that need to get selected after clicking submit button.

If i do it like this it works because .OptionType is a string so i need to compare it with string

&lt;select class=&quot;chosen form-control&quot; id=&quot;selectType&quot;&gt;
	&lt;option disabled value=&#39;&#39; style=&quot;display:none&quot;&gt; --------- &lt;/option&gt;
	&lt;option  {{ if eq .OptionType &quot;1&quot; }}selected{{end}} value=&quot;1&quot;&gt;Option A&lt;/option&gt;
	&lt;option {{ if eq .OptionType &quot;2&quot; }}selected{{end}} value=&quot;2&quot;&gt;Option B&lt;/option&gt;
	&lt;option  {{ if eq .OptionType &quot;3&quot; }}selected{{end}} value=&quot;3&quot;&gt;Option C&lt;/option&gt;
&lt;/select&gt;

But if i try to compare variable like .ID it throws an error

&lt;select class=&quot;chosen form-control&quot; id=&quot;selectValue&quot;&gt;
	&lt;option {{ if eq .SelectedValue &quot;all&quot; }} selected {{end}} value=&#39;all&#39;&gt;
		All Value
	&lt;/option&gt;
	{{range .Circles}}
		&lt;option {{ if eq .SelectedValue .ID }} selected {{end}} value=&#39;{{.ID}}&#39;&gt;
			{{.Name}}
		&lt;/option&gt;
	{{end}}
&lt;/select&gt;

Is there any way that i could convert .ID in the same line with if statement ?

Error Message:

executing "BackendReport/Finance.html" at <.SelectedValue>: can't evaluate field SelectedValue in type models.Value

答案1

得分: 1

template.go

revel.TemplateFuncs["toString"] = func(val interface{}) string {
    return fmt.Sprint(val)
}

.html

{{$current_value := .SelectedValue -}}
    {{range .Circles}}
        <option  {{ if eq $current_value (toString .ID)  }}selected{{end}} value='{{.ID}}'>
            {{.Name}}
        </option>
    {{end}}

这在我这里可以工作。希望对你有帮助。

英文:

template.go

revel.TemplateFuncs[&quot;toString&quot;] = func(val interface{}) string {
    return fmt.Sprint(val)
}

.html

{{$current_value := .SelectedValue -}}
	{{range .Circles}}
		&lt;option  {{ if eq $current_value (toString .ID)  }}selected{{end}} value=&#39;{{.ID}}&#39;&gt;
			{{.Name}}
		&lt;/option&gt;
	{{end}}

This works in me. Hope its help you.

huangapple
  • 本文由 发表于 2022年9月27日 11:39:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/73862087.html
匿名

发表评论

匿名网友

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

确定