使用beego的view中的range巢狀使用if
一. 使用select下拉是用if判斷然後給預設選中
<select name="Pid">
{{$pid := .department.Pid}}
{{range $value := .list}}
<option lay-skin="primary" {{if eq $value.id $pid}} selected="selected" {{end}}
class="layui-input" value="{{$value.id}}">{{$value.name}}</option>
{{end}}
</select>
錯誤資訊
2020/04/30 12:39:21.736 [E] [server.go:2697] template: department/edit.html:46:60: executing "department/edit.html" at <eq $value.id $pid>: error calling eq: invalid type for comparison
使用compare替換eq即可解決
<select name="Pid">
{{$pid := .department.Pid}}
{{range $value := .list}}
<option lay-skin="primary" {{if compare $value.id $pid}} selected="selected" {{end}}
class="layui-input" value="{{$value.id}}">{{$value.name}}</option>
{{end}}
</select>
本作品採用《CC 協議》,轉載必須註明作者和本文連結