Pass struct array to HTML-Template to populate a drop-down

Hello Guys,

i’m very new to this language and i have to use it for my project. I have to pass my Struct Array to my HTML-Template to Populate a drop-down. I can’t pass even a single string to populate… seems like data is empty… The Goal is to pass the whole Struct-Array. In my code i’m trying to do it with one item of them.

The drop-down gets new rows with this code but with no Data in it.

Pls help me guys…

Here is my Code of the Index.html:

<script type="text/javascript">
$(document).ready(function() {
    $("#getKammer").on('click', function() {
        $.ajax({
            url: "http://localhost:8080/getKammer",
            method: "GET",
            success: function(data) {
                var $dropdown = $("#Kammer");
                $dropdown.append($("<option />").val(data).text(this.name));
                $("#getKammer").prop('disabled', true);
            },

        });
    });
});
</script>

And here is my Go-Code:

//Klimakammer struct
type klimakammer struct {
	name        string
	hersteller  string
	ip          string
	sollTemp    string
	istTemp     string
	sollFcht    string
	istFcht     string
	kammerstart bool
	kammerstop  bool
}

//Ct01 Klimakammern erstellen
var Ct01 = klimakammer{"ct01", "weiss", "10.0.62.22", "", "", "", "", false, true}

//Kammern - Fill Klimakammer Array
var Kammern = []klimakammer{
	Ct01,
}

var tmpl = template.Must(template.New("tmpl").ParseFiles("./static/Index.html"))

func main() {

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		if err := tmpl.ExecuteTemplate(w, "Index.html", nil); err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
		}
	})

	http.ListenAndServe(":8080", nil)

	http.HandleFunc("/getKammer", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintln(w, Ct01.name)
	})

}

if you want to make the field visible, make them exported (capital letter).

i did but still nothing shows up

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