Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jerrybroughton opened this issue Aug 15, 2017 · 2 comments
Open

Comments

@jerrybroughton
Copy link

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
Copy link
Contributor

@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
Copy link
Member

josephspurrier commented Sep 29, 2017

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants