TableView use model.PublishRowsReset(),Some lines do not take effect
ParkourLiu opened this issue · 4 comments
A running example and code example are given below, I change two lines of data, but only the last line takes effect. The first line takes effect only if you mouse over it, action.settext () does not work either. You need to move the mouse over it to take effect
package main
import (
"fmt"
"github.com/lxn/walk"
. "github.com/lxn/walk/declarative"
"sort"
"time"
)
var model = NewCondomModel()
var at *walk.Action
func main() {
if _, err := (MainWindow{
Title: "666",
Size: Size{600, 500},
Layout: Grid{},
MenuItems: []MenuItem{
Action{
AssignTo: &at,
Text: "testAction",
},
},
Children: []Widget{
HSplitter{
Children: []Widget{
VSplitter{
StretchFactor: 4,
Children: []Widget{
TableView{
StretchFactor: 9,
Columns: []TableViewColumn{
{Title: "Terrace", Width: 200},
{Title: "IP"},
},
Model: model,
},
Composite{
StretchFactor: 1,
Layout: Grid{Columns: 1, Spacing: 10},
Children: []Widget{
PushButton{Text: "test", OnClicked: func() {
go func() {
aa := []*Condom{
&Condom{
IP: "1",
Terrace: time.Now().Format("2006-01-02 15:04:05"),
checked: false,
},
&Condom{
IP: "2",
Terrace: time.Now().Format("2006-01-02 15:04:05"),
checked: false,
},
}
model.items = aa
model.PublishRowsReset()
err := at.SetText(time.Now().Format("2006-01-02 15:04:05"))
fmt.Println(err)
}()
}},
},
},
},
},
},
},
},
}.Run()); err != nil {
var tmp walk.Form
walk.MsgBox(tmp, "error", err.Error(), walk.MsgBoxIconInformation)
}
}
//--------------------
type Condom struct {
Terrace string
IP string
checked bool
}
type CondomModel struct {
walk.TableModelBase
walk.SorterBase
sortColumn int
sortOrder walk.SortOrder
items []*Condom
}
func (m *CondomModel) RowCount() int {
return len(m.items)
}
func (m *CondomModel) Value(row, col int) interface{} {
item := m.items[row]
switch col {
case 0:
return item.Terrace
case 1:
return item.IP
}
panic("unexpected col")
}
func (m *CondomModel) Checked(row int) bool {
return m.items[row].checked
}
func (m *CondomModel) SetChecked(row int, checked bool) error {
m.items[row].checked = checked
return nil
}
func (m *CondomModel) Sort(col int, order walk.SortOrder) error {
m.sortColumn, m.sortOrder = col, order
sort.Stable(m)
return m.SorterBase.Sort(col, order)
}
func (m *CondomModel) Len() int {
return len(m.items)
}
func (m *CondomModel) 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.Terrace < b.Terrace)
case 1:
return c(a.IP < b.IP)
}
panic("unreachable")
}
func (m *CondomModel) Swap(i, j int) {
m.items[i], m.items[j] = m.items[j], m.items[i]
}
func NewCondomModel() *CondomModel {
m := new(CondomModel)
return m
}
In the above code, I change two lines of data, but only the last line takes effect. The first line takes effect only if you mouse over it
how to solve it?