DEMO CRUD App – Fyne Golang GUI Tutorial 56

Fyne CRUD app – Fyne Golang GUI Tutorial 56

Want to create full crud app in Fyne & golang watch this tutorial.

 

Source Code

package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"os"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/widget"
)

func main() {
	// reading data from file
	res, _ := ioutil.ReadFile("myfilename.txt")
	// creating struct for my data
	type Student struct {
		Name  string
		Phone string
	}
	// slice/array to store all data
	var mydata []Student
	// parse json data
	json.Unmarshal(res, &mydata)
	a := app.New()
	w := a.NewWindow("Student Record List")
	w.Resize(fyne.NewSize(400, 400))
	list := widget.NewList(
		func() int { return len(mydata) },
		func() fyne.CanvasObject {
			return widget.NewLabel("My list item widget")
		},
		func(lii widget.ListItemID, co fyne.CanvasObject) {
			co.(*widget.Label).SetText(mydata[lii].Name)
		},
	)
	label1 := widget.NewLabel("...")
	label2 := widget.NewLabel("...")
	list.OnSelected = func(id widget.ListItemID) {
		label1.Text = mydata[id].Name
		label1.TextStyle = fyne.TextStyle{Bold: true}
		label2.Text = mydata[id].Phone
		// label2.TextStyle = fyne.TextStyle{Bold: true}
		label1.Refresh()
		label2.Refresh()
	}
	e_name := widget.NewEntry()
	e_name.SetPlaceHolder("Enter name ...")
	e_phone := widget.NewEntry()
	e_phone.SetPlaceHolder("Enter phone ...")
	e_submit := widget.NewButton("Submit", func() {
		if e_name.Text != "" && e_phone.Text != "" {
			obj1 := &Student{
				Name:  e_name.Text,
				Phone: e_phone.Text,
			}
			mydata = append(mydata, *obj1)
			b, _ := json.MarshalIndent(mydata, "", " ")
			os.WriteFile("myfilename.txt", b, 0644)
			e_name.Text = ""
			e_phone.Text = ""
			e_name.Refresh()
			e_phone.Refresh()
		} else {
			label1.Text = "Incorrect data"
			label2.Text = ""
			label2.Refresh()
			label1.Refresh()
		}
		list.Refresh()
	})
	e_del := widget.NewButton("DELETE", func() {
		var mydata2 []Student
		for _, a := range mydata {
			if a.Name != label1.Text {
				mydata2 = append(mydata2, a)
				fmt.Println(a)
			}
		}
		mydata = mydata2
		b, _ := json.MarshalIndent(mydata2, "", " ")
		os.WriteFile("myfilename.txt", b, 0644)
		e_name.Text = ""
		e_phone.Text = ""
		e_name.Refresh()
		e_phone.Refresh()
		list.Refresh()
	})
	e_update := widget.NewButton("Update", func() {
		if e_name.Text != "" && e_phone.Text != "" {
			var mydata2 []Student
			for _, a := range mydata {
				if a.Name != label1.Text {
					mydata2 = append(mydata2, a)
					fmt.Println(a)
				} else {
					obj1 := &Student{
						Name:  e_name.Text,
						Phone: e_phone.Text,
					}
					mydata2 = append(mydata2, *obj1)
					fmt.Println(a)
				}
			}
			mydata = mydata2
			b, _ := json.MarshalIndent(mydata2, "", " ")
			os.WriteFile("myfilename.txt", b, 0644)
			e_name.Text = ""
			e_phone.Text = ""
			e_name.Refresh()
			e_phone.Refresh()
			list.Refresh()
		} else {
			label1.Text = "Incorrect data"
			label2.Text = ""
			label2.Refresh()
			label1.Refresh()
		}
		list.Refresh()
	})
	c := container.NewVBox(label1, label2,
		e_name, e_phone, e_submit, e_del, e_update)
	w.SetContent(container.NewHSplit(
		list,
		c,
	))
	w.ShowAndRun()
	fmt.Println(mydata)
}

 

Download Code

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: