Skip to content
Calculated Form Field

Calculated Form Field

A form built with crud.NewBinding whose third field is calculated from the other two.

Calculated Form Field

package main

import (
	"fmt"
	"go.wdy.de/nago/application"
	"go.wdy.de/nago/presentation/core"
	"go.wdy.de/nago/presentation/ui"
	"go.wdy.de/nago/presentation/ui/crud"
	"go.wdy.de/nago/web/vuejs"
)

type Invoice struct {
	ID string
	A  int
	B  int
}

func (i Invoice) Identity() string {
	return i.ID
}

func main() {
	application.Configure(func(cfg *application.Configurator) {
		cfg.SetApplicationID("de.worldiety.tutorial")
		cfg.Serve(vuejs.Dist())

		cfg.RootView(".", func(wnd core.Window) core.View {
			bnd := crud.NewBinding[Invoice](wnd)
			bnd.Add(
				crud.Int(crud.IntOptions{Label: "A"}, crud.Ptr(func(model *Invoice) *int {
					return &model.A
				})),
				crud.Int(crud.IntOptions{Label: "B"}, crud.Ptr(func(model *Invoice) *int {
					return &model.B
				})),
				crud.Text(crud.TextOptions{Label: "A+B"}, crud.Ptr(func(model *Invoice) *string {
					tmp := fmt.Sprintf("%.2f", float64(model.A+model.B))
					return &tmp
				})),
			)

			state := core.AutoState[Invoice](wnd)
			return ui.VStack(
				crud.Form(bnd, state)).
				Frame(ui.Frame{}.MatchScreen())
		})
	}).Run()
}

View on GitHub ↗ · run it with go run go.wdy.de/nago/example/cmd/tutorial-38-crudcalc@latest