如何在TableView上获取数据?

时间:2015-07-08 07:25:28

标签: json go

所有

现在我使用Go编程语言和walk开发我的任务管理应用程序(Go编程语言的“Windows应用程序库工具包”)。

该应用程序具有以下功能:①至②。

①从JSON文件中读取数据,并在TableView上显示数据(TableView是walk的一个组件) ②将TableView上的数据写入JSON文件。

我可以发展①的功能。但是我无法开发②的功能,因为我无法在TableView上获取数据。

如何在TableView上获取数据?

【版】
去:1.4.2
walk:commit baac4d4

【源代码】

package main

import (
    "reflect"
    "fmt"
    "log"
    "os"
    "encoding/json"
    "sort"

    "github.com/lxn/walk"
    . "github.com/lxn/walk/declarative"
    //"strings"
)

type Task struct {
    TaskId string
    TaskNo string
    TaskName string
    ManDay string
    Term string
    ManDayDay string
    StartDate string
    EndDate string
    Progress string
}

type TaskModel struct {
    walk.TableModelBase
    walk.SorterBase
    sortColumn int
    sortOrder walk.SortOrder
    items []Task
}

func NewTaskModel() *TaskModel {
    t := new(TaskModel)

    t.items = make([]Task, 3)

    var tasks []Task
    tasks = read("")

    for i := 0; i < len(tasks); i++ {
        var task Task
        task = tasks[i]
        t.items[i] = task
    }

    t.PublishRowsReset()

    return t
}

func (m *TaskModel) RowCount() int {
    return len(m.items)
}

func (m *TaskModel) Len() int {
    return len(m.items)
}

func (m *TaskModel) Value(row, col int) interface{} {
    item := m.items[row]

    switch col {
        case 0:
            return item.TaskId
        case 1:
            return item.TaskNo
        case 2:
            return item.TaskName
        case 3:
            return item.ManDay
        case 4:
            return item.Term
        case 5:
            return item.ManDayDay
        case 6:
            return item.StartDate
        case 7:
            return item.EndDate
        case 8:
            return item.Progress
    }

    panic("unexpected col")
}

func (m *TaskModel) Sort(col int, order walk.SortOrder) error {
    m.sortColumn, m.sortOrder = col, order

    sort.Sort(m)

    return m.SorterBase.Sort(col, order)
}

func (m *TaskModel) Less(i, j int) bool {
    a, b := m.items[i], m.items[j]

    c := func(ls bool) bool {
        if m.sortOrder == walk.SortAscending {
            return ls
        }

        return !ls
    }

    switch m.sortColumn {
        case 0:
            return c(a.TaskId < b.TaskId)
        case 1:
            return c(a.TaskNo < b.TaskNo)
        case 2:
            return c(a.TaskName < b.TaskName)
        case 3:
            return c(a.ManDay < b.ManDay)
        case 4:
            return c(a.Term < b.Term)
        case 5:
            return c(a.ManDayDay < b.ManDayDay)
        case 6:
            return c(a.StartDate < b.StartDate)
        case 7:
            return c(a.EndDate < b.EndDate)
        case 8:
            return c(a.Progress < b.Progress)
    }

    panic("error")
}

func (m *TaskModel) Swap(i, j int) {
    m.items[i], m.items[j] = m.items[j], m.items[i]
}

func read(filePath string) []Task{

    file, err := os.Open("C:/Dev/xampp/htdocs/workspace/SampleGoProject/src/main/task.json")
    //file, err := os.Open(filePath)

    if err != nil {
        log.Fatal(err)
    }

    defer file.Close()

    var tasks []Task

    decoder := json.NewDecoder(file)
    //json.NewDecoder(file)

    //fmt.Print(tasks)

    //err = decoder.Decode(&tasks)
    decoder.Decode(&tasks)
    if err != nil {
        log.Fatal(err)
    }

    //fmt.Print(tasks)

    return tasks;
}

func write(filePath string, mw *MyMainWindow) {

    var children *walk.WidgetList
    children = mw.MainWindow.Children()

    var child walk.Widget
    child = children.At(0)

    to := reflect.TypeOf(child)
    fv := reflect.ValueOf(child).Convert(to).Interface()

    //tableview := fv.Type().In(0)
    //var tableview TableView
    //tableview := child.Cursor()

    //fmt.Print(child)

    //fmt.Println(tableview)

    fmt.Println(to)
    fmt.Println(fv)



    //r := fv.InterfaceData()
    //fmt.Println(r)
    //fmt.Println(reflect.ValueOf(child).Convert(fv))

    file, err := os.Create("C:/Dev/xampp/htdocs/workspace/SampleGoProject/src/main/task2.json")
    if err != nil {
        log.Fatal(err)
    }

    defer file.Close()

    //var tasks []Task

    encoder := json.NewEncoder(file)

    err = encoder.Encode(fv)
    if err != nil {
        log.Fatal(err)
    }

}

type MyMainWindow struct {
    *walk.MainWindow
    prevFilePath string
}

func main() {

    model := NewTaskModel()

    mw := new (MyMainWindow)

    var openAction, saveAction *walk.Action

    if err:= (MainWindow{
        AssignTo: &mw.MainWindow,
        Title: "WBS",
        MenuItems: []MenuItem{
            Menu {
                Text: "&File",
                Items: []MenuItem{
                    Action{
                        AssignTo: &openAction,
                        Text: "&Open",
                        OnTriggered: mw.openAction_Triggered,
                    },
                    Action{
                        AssignTo: &saveAction,
                        Text: "&Save",
                        OnTriggered: mw.saveAction_Triggered,
                    },
                    Separator{},
                    Action{
                        Text: "&Exit",
                        OnTriggered: func() {
                            mw.Close()  
                        },
                    },
                },
            },
        },
        MinSize: Size{800, 400},
        Layout: VBox{},
        Children: []Widget{
            TableView {
                Columns: []TableViewColumn{
                    {Title: "Task Id"},
                    {Title: "Task No"},
                    {Title: "Task Name"},
                    {Title: "Man Day"},
                    {Title: "Term"},
                    {Title: "Man Day Day"},
                    {Title: "Start Date"},
                    {Title: "End Date"},
                    {Title: "Progress"},
                },
                Model: model,
            },
        },
    }.Create()); err != nil {
        //fmt.Print(err)
        //log.Fatal(err)

    }

    mw.Run()
}

func (mw *MyMainWindow) openAction_Triggered() {
    if err := mw.openFile(); err != nil {
        log.Print(err)
    }
}

func (mw *MyMainWindow) openFile() error {
    dlg := new(walk.FileDialog)

    dlg.FilePath = mw.prevFilePath
    dlg.Filter = "JSON Files|*.json"
    dlg.Title = "Select a json file"

    if ok, err := dlg.ShowOpen(mw); err != nil {
        return err
    } else if !ok {
        return nil
    }

    mw.prevFilePath = dlg.FilePath

    read(mw.prevFilePath)

    return nil
}

func (mw *MyMainWindow) saveAction_Triggered() {
    if err := mw.saveFile(); err != nil {
        log.Print(err)
    }
}

func (mw *MyMainWindow) saveFile() error {
    dlg := new(walk.FileDialog)

    dlg.FilePath = mw.prevFilePath
    dlg.Filter = "JSON Files|*.json"
    dlg.Title = "Input a json file name"

    if ok, err := dlg.ShowSave(mw); err != nil {
        return err
    } else if !ok {
        return nil
    }

    mw.prevFilePath = dlg.FilePath

    write(mw.prevFilePath, mw)

    return nil
}

enter image description here

0 个答案:

没有答案