andlabs / ui

Platform-native GUI library for Go.
Other
8.33k stars 651 forks source link

how to change cell value in table? #321

Open killernova opened 5 years ago

killernova commented 5 years ago

I have a table, for example, have 5 columns. The last column is a button column (actually I want a select box column, but it seems not provided yet), when I click the button, the value in the first column of the same row will be changed. Is there any API to achieve this without rerender the entire UI object?

andlabs commented 5 years ago

Yes, that would be ui.TableModel.RowChanged() after setting the value of column 1.

killernova commented 5 years ago

I still don't get it, how to set cell value? How to insert a new row? How to set the data of new row? I wonder if you could write a demo for us, maybe based on the following demo.

The demo has two meets: First, there is a button named "Add New Row Button' on the top left of the window, and a new row would be added at the first line of the table with some specific data when the button was clicked. Second, the fourth column of the table should be a button and the first column value should be modified when the button was clicked.

package view

import (
    "fmt"
    "github.com/andlabs/ui"
)

type modelHandler struct {
    fileName string
    state string
    url string
    buttonText string
}

func newModelHandler() *modelHandler {
    m := new(modelHandler)
    m.fileName = "None"
    m.buttonText = "Small"
    return m
}

func (mh *modelHandler) ColumnTypes(m *ui.TableModel) []ui.TableValue {
    return []ui.TableValue{
        ui.TableString(""), // column 0 text
        ui.TableString(""), // column 1 text
        ui.TableString(""), // column 2 text
        ui.TableString(""), // column 2 text
    }
}

func (mh *modelHandler) NumRows(m *ui.TableModel) int {
    return 6
}

func (mh *modelHandler) CellValue(m *ui.TableModel, row, column int) ui.TableValue {
    switch column {
    case 0:
        return ui.TableString("")
    case 1:
        fallthrough
    case 2:
        return ui.TableString("")
    case 3:
        return ui.TableString("Normal")
    }
    panic("unreachable")
}

func (mh *modelHandler) SetCellValue(m *ui.TableModel, row, column int, value ui.TableValue) {
    fmt.Println(row, column)
    if column == 3 {
        mh.fileName = "ABC"
    }
}

func SetupUI() {
    mainwin := ui.NewWindow("OSS Client", 1000, 1000, true)
    mainwin.OnClosing(func(*ui.Window) bool {
        ui.Quit()
        return true
    })
    ui.OnShouldQuit(func() bool {
        mainwin.Destroy()
        return true
    })

    vbox := ui.NewVerticalBox()
    vbox.SetPadded(true)

    hbox := ui.NewHorizontalBox()
    hbox.SetPadded(true)
    vbox.Append(hbox, false)
    hbox.Append(ui.NewButton("Add New Row Button"), false)

    mainwin.SetChild(vbox)

    mh := newModelHandler()
    model := ui.NewTableModel(mh)

    table := ui.NewTable(&ui.TableParams{
        Model: model,
        RowBackgroundColorModelColumn: -1,
    })
    vbox.Append(table, false)
    mainwin.SetChild(vbox)
    mainwin.SetMargined(true)

    table.AppendTextColumn("File name",
        0, ui.TableModelColumnNeverEditable, nil)

    table.AppendTextColumn("State", 1, ui.TableModelColumnNeverEditable, nil)
    table.AppendTextColumn("URL",
        2, ui.TableModelColumnNeverEditable, nil)

    table.AppendButtonColumn("Image size",
        3, ui.TableModelColumnAlwaysEditable)

    mainwin.Show()
}
andlabs commented 5 years ago

Oh, I see. Package ui doesn't actually store data; you have to store the data yourself, in the model handler implementation. To add a row, set it up so that NumRows returns a different number, and then call the model's RowInserted method. When the button is clicked, you'll set CellValue up to return a different value for that row's column 1, and then call the model's RowChanged method. I can improve the table example, though; that'll come sometime later.

killernova commented 5 years ago

Looking forward to your demo ~

killernova commented 5 years ago

By the way, I can handle this operation now, thanks for your reply.