Home > Net >  How to change dynamic variable in Revel Template
How to change dynamic variable in Revel Template

Time:09-27

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

<select  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>

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

<select  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>

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

CodePudding user response:

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}}

This works in me. Hope its help you.

  • Related