0

First sorry for my bad english, I'll try to do my best.

Im trying to create a webapp that shows the name of servers, their IPs, ping, ports, etc...

Here is a part of my code:

MAIN.GO :

type Widget struct {
Ipport  string
Number  string
State   string
Colport string
Blink   string
}

type resulthtml struct {
Name       string
Affordi    string
Afflieu    string
Affip      string
Affresping string
Affresport string
Affport    string
Affactif   string
Colping    string
Lisports   []Widget
Blink      string
}

var prout []resulthtml

// 80% of the missing code is here like ping function, etc, etc

func main() {

//.........some code here.............

    http.Handle("/favicon.ico", http.NotFoundHandler())
    http.HandleFunc("/", affichagePing)
    http.ListenAndServe(":2692", nil)
}


func affichagePing(w http.ResponseWriter, r *http.Request) {

    tmpl := template.Must(template.ParseFiles("test.html"))
    tmpl.Execute(w, prout)
}

And TEST.HTML :

        <div id="cadre_affich1">
        <table id="Affichageglobal">
        <thead>
                <tr>    
                    <th class="AffichageLigneT" style="width: 3%;"></th>
                    <th class="AffichageLigneT" style="width: 15%;">Name</th>
                    <th class="AffichageLigneT" style="width: 8%;">Type</th>
                    <th class="AffichageLigneT" style="width: 6%;">Place</th>
                    <th class="AffichageLigneT" style="width: 14%;">IP</th>
                    <th class="AffichageLigneT" style="width: 10%;">Ping</th>
                    <th class="AffichageLigneT" style="width: 41%;">Ports</th>
                    <th class="AffichageLigneT" style="width: 3%;"></th>
                </tr>
            </thead>
            <tbody>
                {{range .}}
                <tr>
                    <td class="AffichageLigneRefr"></td>
                    <td class="AffichageLigneNom"><span class="AffichageStatut" style="background-color:{{.Colping}}"></span><span>{{.Name}}</span></td>
                    <td class="AffichageLigneType">{{.Affordi}}</td>
                    <td class="AffichageLigneLieu">{{.Afflieu}}</td>
                    <td class="AffichageLigneIP">{{.Affip}}</td>                        
                    <td class="AffichageLigneResPing"><span class={{.Blink}} style="color:{{.Colping}}">{{.Affresping}}</span></td>                     
                    <td class="AffichageLignePorts">{{range $v := .Lisports}}<span class={{$v.Blink}}>{{$v.Number}}</span>{{end}}</td>
                    <td class="AffichageLigneEdit"></td>
                </tr>
                {{end}}
            </tbody>
        </table>
    </div>

I have no problem displaying what I want. Now I want to refresh a specific row without reloading the whole page (its ping, name, etc...) when I click on the first cell of it. I know I have to do it through Javascript that will send informations to a Go Handler but well, I spent hours and tried so many things...

I can't get it to work.

Thank you very much.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Madpeky
  • 1
  • 1
  • Add a new endpoint to your Go server, one that will serve only the row that you want to refresh, implement a new handler that will handle the requests to that endpoint and have the handler write the row in json format. With that in place add a jascript on-click event listener to the the HTML table and have it send an XMLHttpRequest to the new Go endpoint, when the response comes back have the js listener use the returned json to update the clicked row. – mkopriva Nov 03 '18 at 14:11
  • 1
    See similar examples: [Creating load more button in Golang with templates](https://stackoverflow.com/questions/41136000/creating-load-more-button-in-golang-with-templates/41138344#41138344); [Interactive web pages in Go](https://stackoverflow.com/questions/41197376/interactive-web-pages-in-go/41198615#41198615); [Dynamically refresh a part of the template when a variable is updated golang](https://stackoverflow.com/questions/37118281/dynamically-refresh-a-part-of-the-template-when-a-variable-is-updated-golang/37119014#37119014). – icza Nov 03 '18 at 14:21
  • Ok thanks to you both. – Madpeky Nov 03 '18 at 21:13
  • Got it to work in one way, I sent data from Javascript to Goland and Im able to print it (all cell of a specific row). But in main.Go when i try to iterate through the data with a 'range' it says 'impossible cause of rune type'. Is it the default format ? Do i have to convert the rune data to string ? – Madpeky Nov 03 '18 at 21:14

0 Answers0