Populate record in select with group

Untitled

Out put will be like this. and the record from database.

Is this a <select> in HTML? Do you build the HTML using Go templates? How is your question related to Go?

Yes that is . I want to put the record from database in a group

How do you use Go for this?

This is the code. How can i display in template

 func Fill(res http.ResponseWriter, req *http.Request) {
userName:= getUserName(req)
if userName != "" {
	
	UserEmail := userName

	car :=car{}
	records :=[]car{}

	selDB, err :=config.DB.Query("SELECT Car FROM tblcars")
	if err != nil {
		panic(err.Error())
	}
	for selDB.Next() {
		
		var car string
		err = selDB.Scan(&car)
		if err != nil {
			panic(err.Error())
		}	
		car.car = car
		
 		records = append(records, car)				
    }
		data:=Page{
			Title:  "Dashboard",
			UserEmail: UserEmail,
			car: records,
	
		}
		fmt.Println(data)	
		templating.Display(res, "index",data)
} else {
	http.Redirect(res, req, "/", 302)
	return
}

}

	 <select>
	  <optgroup label="Swedish Cars">
		<option value="volvo">Volvo</option>
		<option value="saab">Saab</option>
	  </optgroup>
	  <optgroup label="German Cars">
		<option value="mercedes">Mercedes</option>
		<option value="audi">Audi</option>
	  </optgroup>
	</select>

Which library is templating?

“html/template”

How to do it in json

can i do it this way. How can i make like this from database?

	cars{
	  "name": "Swedish Cars",
	  "car": {
		"Nameofcar" : "Volvo",
		"Nameofcar" : "Saab"
	  },
	  "name": "German Cars",
	  "car": {
		"Nameofcar" : "Mercedes",
		"Nameofcar" : "Audi"
	  }
	}

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.