Update Data CRUD APP – Fyne Golang GUI Tutorial 62

Update Data CRUD APP – Fyne Golang GUI Tutorial 62



Source Code

<div>package main</div>
<div>// import fyne</div>
<div>import (</div>
<div>    "encoding/json"</div>
<div>    "io/ioutil"</div>
<div>    "fyne.io/fyne/v2"</div>
<div>    "fyne.io/fyne/v2/app"</div>
<div>    "fyne.io/fyne/v2/container"</div>
<div>    "fyne.io/fyne/v2/widget"</div>
<div>func main() {</div>
<div>    // student struct, you can use any name</div>
<div>    type Student struct {</div>
<div>        Name  string // name N is capital</div>
<div>        Phone string</div>
<div>    }</div>
<div>    // Now creat a slice/ array to store data</div>
<div>    var myStudentData []Student</div>
<div>    // welcome again</div>
<div>    //lets read data from file and display it in list</div>
<div>    data_from_file, _ := ioutil.ReadFile("myFile_name.txt")</div>
<div>    // file name with extension .txt or .json</div>
<div>    // unmarshall/parse data received from file and save/push in slice</div>
<div>    // 2 argument 1. data source, 2. data slice to store data</div>
<div>    json.Unmarshal(data_from_file, &amp;myStudentData)</div>
<div>    // now we can use this data in our list</div>
<div>    // lets create our list</div>
<div>    // new app</div>
<div>    a := app.New()</div>
<div>    // new title and window</div>
<div>    w := a.NewWindow("CRUD APP")</div>
<div>    // resize window</div>
<div>    w.Resize(fyne.NewSize(400, 400))</div>
<div>    // New label to dispaly name and phone number details</div>
<div>    l_name := widget.NewLabel("...")</div>
<div>    l_name.TextStyle = fyne.TextStyle{Bold: true}</div>
<div>    // for phone number</div>
<div>    l_phone := widget.NewLabel("...")</div>
<div>    // entry widget for name</div>
<div>    e_name := widget.NewEntry()</div>
<div>    e_name.SetPlaceHolder("Enter name here...")</div>
<div>    // entry widget for phone</div>
<div>    e_phone := widget.NewEntry()</div>
<div>    e_phone.SetPlaceHolder("Enter phone here...")</div>
<div>    // submit button</div>
<div>    submit_btn := widget.NewButton("Submit", func() {</div>
<div>        // logic part- store our data in json format</div>
<div>        // let create a struct for our data</div>
<div>        // Get data from entry widget and push to slice</div>
<div>        myData1 := &amp;Student{</div>
<div>            Name:  e_name.Text, // data from name entry widget</div>
<div>            Phone: e_phone.Text,</div>
<div>        }</div>
<div>        // append / push data to our slice</div>
<div>        myStudentData = append(myStudentData, *myData1)</div>
<div>        // * star is very important</div>
<div>        // convert / parse data to json format</div>
<div>        // 3 arguments</div>
<div>        // 1st is our slice data</div>
<div>        // ignore 2nd</div>
<div>        // 3rd is identation, we are using space to indent our data</div>
<div>        final_data, _ := json.MarshalIndent(myStudentData, "", " ")</div>
<div>        // writing data to file</div>
<div>        // it take 3 things</div>
<div>        //file name .txt or .json or anything you want to use</div>
<div>        // data source, final_data in our case</div>
<div>        // writing/reading/execute permission</div>
<div>        ioutil.WriteFile("myFile_name.txt", final_data, 0644)</div>
<div>        /// we are done :)</div>
<div>        // lets test</div>
<div>        // empty input field after data insertion</div>
<div>        e_name.Text = ""</div>
<div>        e_phone.Text = ""</div>
<div>        // refresh name &amp; phone entry object</div>
<div>        e_name.Refresh()</div>
<div>        e_phone.Refresh()</div>
<div>    })</div>
<div>    /// Delete Button</div>
<div>    del_button := widget.NewButton("Del", func() {</div>
<div>        // Create a new Temporary slice</div>
<div>        var TempData []Student // student is the struct we create yesterday</div>
<div>        // now loop through the main slice "myStudentData"</div>
<div>        // and push all the data to TempData slice except the select one</div>
<div>        // here i is the indexs and "e" is the element of slice</div>
<div>        // I don't need index here</div>
<div>        // I will push all the data to tempdata slice</div>
<div>        for _, e := range myStudentData {</div>
<div>            // l_name is the label we create to show details</div>
<div>            // important not equal to is used. Don't append if equal to e.Name</div>
<div>            if l_name.Text != e.Name {</div>
<div>                TempData = append(TempData, e)</div>
<div>            }</div>
<div>        }</div>
<div>        // Now append all the data back to main slice myStudentData</div>
<div>        myStudentData = TempData</div>
<div>        // conver to json and marshall indent</div>
<div>        // 3 element</div>
<div>        // our slice is the data source (1st argument)</div>
<div>        // 2nd is prefix.. we don't need prefix</div>
<div>        // 3rd is the indent. we need a single space</div>
<div>        result, _ := json.MarshalIndent(myStudentData, "", " ")</div>
<div>        // write data to file</div>
<div>        // first argument is file name</div>
<div>        // data source which is result here.</div>
<div>        // last one is permission</div>
<div>        ioutil.WriteFile("myFile_name.txt", result, 0644)</div>
<div>    })</div>
<div>    // list widget</div>
<div>    list := widget.NewList(</div>
<div>        // first argument is item count</div>
<div>        // len() function to get myStudentData slice len</div>
<div>        func() int { return len(myStudentData) },</div>
<div>        // 2nd argument is for widget choice. I want to use label</div>
<div>        func() fyne.CanvasObject { return widget.NewLabel("") },</div>
<div>        // 3rd argument is to update widget with our new data</div>
<div>        func(lii widget.ListItemID, co fyne.CanvasObject) {</div>
<div>            co.(*widget.Label).SetText(myStudentData[lii].Name)</div>
<div>        },</div>
<div>    )</div>
<div>    // update on clicked/ selected</div>
<div>    list.OnSelected = func(id widget.ListItemID) {</div>
<div>        l_name.Text = myStudentData[id].Name</div>
<div>        l_name.Refresh()</div>
<div>        // for phone number</div>
<div>        l_phone.Text = myStudentData[id].Phone</div>
<div>        l_phone.Refresh()</div>
<div>    }</div>
<div>    // paste update button code here</div>
<div>    // Update Button</div>
<div>    update_button := widget.NewButton("Update", func() {</div>
<div>        // Temp slice</div>
<div>        var TempData []Student</div>
<div>        // Data I want to update</div>
<div>        update := &amp;Student{</div>
<div>            Name:  e_name.Text,  // entry name widget</div>
<div>            Phone: e_phone.Text, // entry widget : phone</div>
<div>        }</div>
<div>        // looping through our slice and update the data meeting our criteria</div>
<div>        // _ is to ignore index</div>
<div>        // e is the element/data in the myStudentData slice</div>
<div>        for _, e := range myStudentData {</div>
<div>            // checking data criteria</div>
<div>            if l_name.Text == e.Name {</div>
<div>                // if criteria mathed, append updated data else</div>
<div>                TempData = append(TempData, *update)</div>
<div>            } else {</div>
<div>                // else append old data in TempData slice</div>
<div>                TempData = append(TempData, e)</div>
<div>            }</div>
<div>        }</div>
<div>        // first</div>
<div>        myStudentData = TempData</div>
<div>        // convert data to json &amp; write to file</div>
<div>        // first argument is data source // 2nd is prefix // third is indent</div>
<div>        result, _ := json.MarshalIndent(myStudentData, "", " ")</div>
<div>        // write data to file</div>
<div>        // 1st argument is file name</div>
<div>        // 2nd is our data(result) from marshalindent</div>
<div>        // 3rd is the file permission.</div>
<div>        ioutil.WriteFile("myFile_name.txt", result, 0644)</div>
<div>        // refresh &amp; empty entry box and refresh list</div>
<div>        e_name.Text = ""</div>
<div>        e_phone.Text = ""</div>
<div>        e_name.Refresh()</div>
<div>        e_phone.Refresh()</div>
<div>        // refresh list also</div>
<div>        list.Refresh() // if not working cut code and paste after list widget</div>
<div>    })</div>
<div>    // update widget tree and add update button also</div>
<div>    // show and run</div>
<div>    w.SetContent(</div>
<div>        // lets create Hsplite container</div>
<div>        container.NewHSplit(</div>
<div>            // first argument is list of data</div>
<div>            list,</div>
<div>            // 2nd is</div>
<div>            // vbox container</div>
<div>            // show both label</div>
<div>            container.NewVBox(</div>
<div>                l_name, l_phone, e_name,</div>
<div>                e_phone, submit_btn, del_button,</div>
<div>                update_button,</div>
<div>            ),</div>
<div>        ),</div>
<div>    )</div>
<div>    w.ShowAndRun()</div>


Fyne Golang GUI Course

Tony BB

TonyBB is a Coach , marketer, hypnotist and a founder of RSKVF Production who specializes in providing simple, affordable, and easy to use solutions for Life.

Click Here to Leave a Comment Below 0 comments

Leave a Reply: