使用gorilla/schema解包<select>字段

eli*_*rar 3 html go gorilla

我正在使用gorilla/schema来解压缩r.PostForm到一个结构中.

我的问题是,我试图找出一种"明智的"方式来获取<select>元素的选定值,使我能够轻松地使用html/template来重新选择字段(即从会话中重新填充表单时) )注意到没有一种简单的方法来测试相等性,只是通过传递结构的实例RenderTemplate.

为了说明我的所作所为:

type Listing struct {
    Id           string        `schema:"-"`
    Title        string        `schema:"title"`
    Company      string        `schema:"company"`
    Location     string        `schema:"location"`
        ...
    Term         string        `schema:"term"`
}
Run Code Online (Sandbox Code Playgroud)
if r.Method == "POST" {

// error handling code removed for brevity, but trust me, it exists!

    err = r.ParseForm()
    err = decoder.Decode(listing, r.PostForm)
    err = listing.Validate() // checks field lengths as I'm using a schema-less datastore
Run Code Online (Sandbox Code Playgroud)
<label for="term">Term</label>
      <select data-placeholder="Term...?" id="term" name="term" required>
        <option name="term" value="full-time">Full Time</option>
        <option name="term" value="part-time">Part Time</option>
        <option name="term" value="contract">Contract</option>
        <option name="term" value="freelance">Freelance</option>
      </select>
Run Code Online (Sandbox Code Playgroud)

...当我将列表实例传递给模板时,我希望能够做什么:

renderTemplate(w, "create_listing.tmpl", M{
        "listing":              listing,
    })
Run Code Online (Sandbox Code Playgroud)
 <label for="term">Term</label>
          <select data-placeholder="Term...?" id="term" name="term" required>
            <option name="term" value="full-time" {{ if .term == "full-time" }}selected{{end}}>Full Time</option>
            <option name="term" value="part-time"{{ if .term == "part-time" }}selected{{end}}>Part Time</option>
            <option name="term" value="contract" {{ if .term == "contract" }}selected{{end}}>Contract</option>
            <option name="term" value="freelance" {{ if .term == "freelance" }}selected{{end}}>Freelance</option>
          </select>
Run Code Online (Sandbox Code Playgroud)

显然这不起作用.我认为这template.FuncMap是一个可能的解决方案,但我不知道当我想将整个列表实例传递给模板时(例如,而不是逐个字段),我如何使用它.如果可能的话,我也希望最小化我的struct中不必要的字段.我可以为每个值设置布尔字段(即Fulltime bool,但是如果用户返回并编辑内容,我需要代码将其他字段更改为"false".

有没有办法以这种方式实现这一点,以便与...的局限性相吻合template/html?

thw*_*hwd 5

您可以编写一个视图来构建和表示一个select元素:

{{define "select"}}
    <select name="{{.Name}}>
        {{range $a, $b := .Options}}
             <option value="{{print $a}}" {{if $a == .Selected}}>{{print $b}}</option>
        {{end}}
    </select>
{{end}}
Run Code Online (Sandbox Code Playgroud)

以及相应的数据结构:

type SelectBlock struct {
    Name     string
    Selected string
    Options  map[string]string
}
Run Code Online (Sandbox Code Playgroud)

然后实例化它:

termSelect := SelectBlock{
    Name:     "term",
    Selected: "",
    Options:  map[string]string{
        "full-time": "Full Time",
        "part-time": "Part Time",
        "contract":  "Contract",
        "freelance": "Freelance",
    },
}
Run Code Online (Sandbox Code Playgroud)

并设置Selected字段:

termSelect.Selected = "full-time"
Run Code Online (Sandbox Code Playgroud)

并在表单视图中输出视图片段:

{{template "select" $termSelect}}
Run Code Online (Sandbox Code Playgroud)

$termSelect你的实例在哪里SelectBlock?