blue-jay / blueprint

Blueprint for your next web application in Go.
https://blue-jay.github.io/
MIT License
481 stars 77 forks source link

Question: Handling html select with single or multiple select #68

Open jerrybroughton opened 7 years ago

jerrybroughton commented 7 years ago

Is there an external library or something I am missing that allows taking a struct from a database representing a list of <options in an HTML template and setting the one selected in the case of a single select dropdown, or multiple selected and handling that in an html template. What do you usually do in your framework to support this.

For example, in a simple example, I may have a table called color that I have columns of ID and color_name, with 1-red, 2-blue, etc. I want to have a <select with the options from this lookup table but my actual data is lets say a table that has my favorite color and other preferences. I would want to on CREATE.. in the template allow them to choose from that color list and on UPDATE show what I selected before in the list and allow it to be changed. I might have MANY of these lookup boxes.

in WebApps, this is very common, and so I was wondering if this was baked in someplace and I missed it or if you have someway you usually do this.

TuralAsgar commented 7 years ago

@jerrybroughton It is a fair question. I also once got stuck on this issue and couldn't find a direct solution. Cause I have to use range template function. But when you enter range function the scope changes so the code I write doesn't work. I have looked at different templating languages on Go. But it must be ways to leverage that in standard templating.

josephspurrier commented 6 years ago

You should be able to get the multi-select to work like this. On the page load, the red and blue options will be selected. Is that what you're looking for?

// Create displays the create form.
func Create(w http.ResponseWriter, r *http.Request) {
    c := flight.Context(w, r)

    v := c.View.New("note/create")
    c.Repopulate(v.Vars, "name")
    v.Vars["mselect"] = []string{"red", "blue"}
    v.Render(w, r)
}
<select multiple class="form-control" name="mselect">
    <option {{OPTION "mselect" "red" .item.Select .}}>Red</option>
    <option {{OPTION "mselect" "green" .item.Select .}}>Green</option>
    <option {{OPTION "mselect" "blue" .item.Select .}}>Blue</option>
</select>

The documentation for these is right before this heading.