眺めて覚えるGo言語 その5 TableView&Button

#GUIを扱うと欲しくなるのがData Grid Viewですね。
データを一覧表で見せるやつ。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/274715/e954c7a3-b466-be41-6db5-5d1532f2dd3e.png)
#まずデータ

```data.json
[{"ID":"1","Name":"関波子","Namae":"セキナミコ","Sex":"女","Btype":"AB","Date":"1980/07/31","Tel":"03-3060-4716","Mob":"090-7787-3784","Email":"sk@eaccess.net"},
{"ID":"2","Name":"小倉準司","Namae":"コクラジュンジ","Sex":"男","Btype":"A","Date":"1973/10/08","Tel":"0166-36-3522","Mob":"","Email":"junzi-kokura@eaccess.net"},
{"ID":"3","Name":"西村有紀子","Namae":"ニシムラユキコ","Sex":"女","Btype":"O","Date":"1972/12/05","Tel":"0745-67-2723","Mob":"090-5165-2074","Email":"okikuy1972@livedoor.com"},
{"ID":"4","Name":"谷僧三郎","Namae":"タニソウザブロウ","Sex":"男","Btype":"B","Date":"1989/01/17","Tel":"077-323-7124","Mob":"090-3781-1181","Email":"suzbrutn@dsn.ad.jp"},
{"ID":"5","Name":"山口和久","Namae":"ヤマグチカズヒサ","Sex":"男","Btype":"A","Date":"1973/02/21","Tel":"0183-21-6828","Mob":"070-4790-1232","Email":"kazuhisa73@geocities.com"},
{"ID":"6","Name":"米田一生","Namae":"コメタイッセイ","Sex":"男","Btype":"O","Date":"1984/07/25","Tel":"043-883-8543","Mob":"","Email":"issei07@dsn.ad.jp"},
{"ID":"7","Name":"村松希美江","Namae":"ムラマツキミエ","Sex":"女","Btype":"O","Date":"1980/11/21","Tel":"075-727-1789","Mob":"090-4849-6939","Email":"kimie.muramatu@odn.ne.jp"},
{"ID":"8","Name":"小野寺眞八","Namae":"オノデラシンヤ","Sex":"男","Btype":"O","Date":"1986/04/28","Tel":"0748-01-7199","Mob":"090-1457-0772","Email":"ondr.sny@gmo-media.jp"},
{"ID":"9","Name":"志村陽一郎","Namae":"シムラヨウイチロウ","Sex":"男","Btype":"O","Date":"1975/07/08","Tel":"0136-88-6603","Mob":"","Email":"smr-yutru@dion.ne.jp"},
{"ID":"10","Name":"鎌田敏美","Namae":"カマタトシミ","Sex":"女","Btype":"B","Date":"1993/06/09","Tel":"0554-03-0156","Mob":"090-5690-8749","Email":"kamata0609@example.com"},
{"ID":"11","Name":"宮本力弥","Namae":"ミヤモトリキヤ","Sex":"男","Btype":"O","Date":"1975/08/10","Tel":"07617-9-0761","Mob":"070-6565-9470","Email":"rky75@example.ac.jp"},
{"ID":"12","Name":"中山英博","Namae":"ナカヤマヒデヒロ","Sex":"男","Btype":"A","Date":"1978/04/01","Tel":"03-3043-4017","Mob":"090-3086-6504","Email":"nkym0401@dion.ne.jp"},
{"ID":"13","Name":"丸山平作","Namae":"マルヤマヘイサク","Sex":"男","Btype":"AB","Date":"1994/05/26","Tel":"0178-60-8503","Mob":"070-3567-0574","Email":"hisk0526@ybb.ne.jp"},
{"ID":"14","Name":"金子君吉","Namae":"カネコキミキチ","Sex":"男","Btype":"O","Date":"1984/05/15","Tel":"0256-13-9329","Mob":"090-7210-7725","Email":"kaneko_kimikiti@aitai.ne.jp"},
{"ID":"15","Name":"阿部康伸","Namae":"アベヤスノブ","Sex":"男","Btype":"B","Date":"1973/04/09","Tel":"0167-59-5860","Mob":"090-6699-6888","Email":"abe-yasunobu@users.gr.jp"},
{"ID":"16","Name":"東健也","Namae":"アズマケンヤ","Sex":"男","Btype":"B","Date":"1977/12/25","Tel":"0766-24-2308","Mob":"","Email":"kenya.azuma@tiki.ne.jp"},
{"ID":"17","Name":"高田紀子","Namae":"タカタトシコ","Sex":"女","Btype":"B","Date":"1994/11/06","Tel":"078-547-5651","Mob":"070-8120-1066","Email":"okisotokisot@example.com"},
{"ID":"18","Name":"金城茂義","Namae":"カネギシゲヨシ","Sex":"男","Btype":"O","Date":"1987/03/14","Tel":"076-969-3546","Mob":"070-8191-6845","Email":"sigeyosi2003@plala.or.jp"},
{"ID":"19","Name":"神谷覚","Namae":"カミタニサトル","Sex":"男","Btype":"O","Date":"1976/02/04","Tel":"075-435-9736","Mob":"090-9981-6325","Email":"inatimak0204@hotmail.com"},
{"ID":"20","Name":"北川梨華","Namae":"キタカワリカ","Sex":"女","Btype":"A","Date":"1980/12/25","Tel":"03-3658-2663","Mob":"090-1407-6777","Email":"akir1710033@yahoo.co.jp"},
{"ID":"21","Name":"福岡邦江","Namae":"フクオカクニエ","Sex":"女","Btype":"O","Date":"1975/09/26","Tel":"0547-17-6053","Mob":"090-9435-2443","Email":"hukuoka-kunie@comeon.to"},
{"ID":"22","Name":"安井治次","Namae":"ヤスイハルジ","Sex":"男","Btype":"B","Date":"1977/02/09","Tel":"0157-27-9757","Mob":"090-3155-4472","Email":"izurah090@example.org"},
{"ID":"23","Name":"原田紀世実","Namae":"ハラタキヨミ","Sex":"女","Btype":"A","Date":"1994/10/31","Tel":"07435-8-5006","Mob":"","Email":"hrt.kym@hi-ho.ne.jp"},
{"ID":"24","Name":"今井為一郎","Namae":"イマイタメイチロウ","Sex":"男","Btype":"A","Date":"1974/10/05","Tel":"0575-15-1441","Mob":"070-6309-2203","Email":"imi.tmitru@plala.or.jp"},
{"ID":"25","Name":"天野周","Namae":"アマノシュウキチ","Sex":"男","Btype":"O","Date":"1992/08/19","Tel":"0426-73-1142","Mob":"090-7152-4834","Email":"amn9718@aitai.ne.jp"}]
```

#データ1行の構造(struct)

``` data.go
type Adr struct {
	ID    int64
	Name  string
	Namae string
	Sex   string
	Btype string
	Date  string
	Tel   string
	Mob   string
	Email string
}
```

###読み込みルーチン

```read.json.go
	bytes, err := ioutil.ReadFile("data.json")
	if err != nil {
		log.Fatal(err)
	}
	var adrs []interface{}
	if err := json.Unmarshal(bytes, &adrs); err != nil {
		log.Fatal(err)
	}
```
Jsonを**adrs []interface**の型に格納する。
-**モデルの作成**

``` model.go
type AdrsModel struct {
	walk.SortedReflectTableModelBase
	items []*Adr
}

func (m *AdrsModel) Items() interface{} {
	return m.items
}
```
##モデルに代入

```make_model.go
m := &AdrsModel{items: make([]*Adr, len(adrs))}
	for i := range m.items {
		xx := adrs[i].(map[string]interface{})
		id, _ := strconv.ParseInt(xx["ID"].(string), 10, 64)
		m.items[i] = &Adr{
			ID:    id,
			Name:  xx["Name"].(string),
			Namae: xx["Namae"].(string),
			Sex:   xx["Sex"].(string),
			Btype: xx["Btype"].(string),
			Date:  xx["Date"].(string),
			Tel:   xx["Tel"].(string),
			Mob:   xx["Mob"].(string),
			Email: xx["Email"].(string),
		}
	}
```
#プログラム全体見渡すと下記のようになる。

```main.go
package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"strconv"
)

import (
	"github.com/lxn/walk"
	. "github.com/lxn/walk/declarative"
)

type Adr struct {
	ID    int64
	Name  string
	Namae string
	Sex   string
	Btype string
	Date  string
	Tel   string
	Mob   string
	Email string
}

func getJ() (*AdrsModel, []TableViewColumn) {
	bytes, err := ioutil.ReadFile("data.json")
	if err != nil {
		log.Fatal(err)
	}
	var adrs []interface{}
	if err := json.Unmarshal(bytes, &adrs); err != nil {
		log.Fatal(err)
	}
	lb := []string{"ID", "Name", "Namae", "Sex", "Btype", "Date", "Tel", "Mob", "Email"}
	lbs := []TableViewColumn{}
	for _, x := range lb {
		l := TableViewColumn{Name: x}
		lbs = append(lbs, l)
	}
	m := &AdrsModel{items: make([]*Adr, len(adrs))}
	for i := range m.items {
		xx := adrs[i].(map[string]interface{})
		id, _ := strconv.ParseInt(xx["ID"].(string), 10, 64)
		m.items[i] = &Adr{
			ID:    id,
			Name:  xx["Name"].(string),
			Namae: xx["Namae"].(string),
			Sex:   xx["Sex"].(string),
			Btype: xx["Btype"].(string),
			Date:  xx["Date"].(string),
			Tel:   xx["Tel"].(string),
			Mob:   xx["Mob"].(string),
			Email: xx["Email"].(string),
		}
	}

	return m, lbs
}

type AdrsModel struct {
	walk.SortedReflectTableModelBase
	items []*Adr
}

func (m *AdrsModel) Items() interface{} {
	return m.items
}
func main() {
	if err := RunMainWindow(); err != nil {
		log.Fatal(err)
	}
}
func click() {
	fmt.Println("clcik")
}
func RunMainWindow() error {
	model, lbs := getJ()
	if _, err := (MainWindow{
		Name:    "mainWindow", 
		Title:   "Walk Settings Example",
		MinSize: Size{800, 600},
		Layout:  VBox{MarginsZero: true},
		Children: []Widget{

			TableView{
				Name:             "tableView", 
				AlternatingRowBG: true,
				ColumnsOrderable: true,
				Columns:          lbs,
				Model:            model,
			},
			Composite{
				Layout: Grid {Columns : 6}, // GridはCompositeの中のみ可。
				Children: []Widget {
					PushButton{
						Text:      "Hello",
						ColumnSpan : 1,
						OnClicked: func(){
							r:=walk.MsgBox(nil, "Hello", "World", walk.MsgBoxOKCancel)
							if r == walk.DlgCmdOK {
								fmt.Println("OK!")
							}else {
								fmt.Println("Cancel!")
							}
							},
					},
					PushButton{
						Text:      "World",
						ColumnSpan : 5,
						OnClicked: func(){fmt.Println("world")},
					},
				},
			},

		},
	}.Run()); err != nil {
		return err
	}

	return nil
}

func NewAdrModel() *AdrsModel {
	bytes, err := ioutil.ReadFile("data.json")
	if err != nil {
		log.Fatal(err)
	}

	var adrs []interface{}
	if err := json.Unmarshal(bytes, &adrs); err != nil {
		log.Fatal(err)
	}

	m := &AdrsModel{items: make([]*Adr, len(adrs))}
	for i, _ := range m.items {
		xx := adrs[i].(map[string]interface{})
		id, _ := strconv.ParseInt(xx["ID"].(string), 10, 64)
		m.items[i] = &Adr{
			ID:    id,
			Name:  xx["Name"].(string),
			Namae: xx["Namae"].(string),
			Sex:   xx["Sex"].(string),
			Btype: xx["Btype"].(string),
			Date:  xx["Date"].(string),
			Tel:   xx["Tel"].(string),
			Mob:   xx["Mob"].(string),
			Email: xx["Email"].(string),
		}
	}

	return m
}
```

#余談buttonとメッセージダイアログ

```
Composite{
				Layout: Grid {Columns : 6}, // GridはCompositeの中のみ可。
				Children: []Widget {
					PushButton{
						Text:      "Hello",
						ColumnSpan : 1,
						OnClicked: func(){
							r:=walk.MsgBox(nil, "Hello", "World", walk.MsgBoxOKCancel)
							if r == walk.DlgCmdOK {
								fmt.Println("OK!")
							}else {
								fmt.Println("Cancel!")
							}
							},
					},
					PushButton{
						Text:      "World",
						ColumnSpan : 5,
						OnClicked: func(){fmt.Println("world")},
					},
				},
			},

```