更新模型更改后的表内容

  • 本文关键字:模型 更新 tornadofx
  • 更新时间 :
  • 英文 :


我正在测试tornadofx框架(主要是复制粘贴示例(,我有奇怪的问题,即在编辑后未更新表内容。我看到

的内容
val persons = FXCollections.observableArrayList<Person>()
val selectedPerson = PersonModel()

正在发生变化,但视图却没有。正如我从Tornadofx Github所做的一个例子一样,我很困惑。

这是类

class Person(id: Int, name: String) {
    var id by property(id)
    fun idProperty() = getProperty(Person::id)
    var name by property(name)
    fun nameProperty() = getProperty(Person::name)
}
class PersonModel : ItemViewModel<Person>() {
    val id = bind { item?.idProperty() }
    val name = bind { item?.nameProperty() }
}
class PersonController : Controller() {
    val persons = FXCollections.observableArrayList<Person>()
    val selectedPerson = PersonModel()
    init {
        // Add some test persons for the demo
        persons.add(Person(42, "John Doe"))
        persons.add(Person(43, "Jane Doe"))
    }
}
class MainWindow : View("FX Test") {
    private val controller: PersonController by inject()
    override val root = borderpane {
        center = tableview(controller.persons) {
            column("ID", Person::id)
            column("Name", Person::name)
            bindSelected(controller.selectedPerson)
            contextmenu {
                item("Edit", KeyCombination.keyCombination("F3")).action {
                    dialog("Client editor") {
                        field("Name") {
                            textfield(controller.selectedPerson.name)
                        }
                        buttonbar {
                            button("Save") {
                                setOnAction {
                                    controller.selectedPerson.commit()
                                    close()
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

根据文档,在控制器提交后,视图将自动更新。

您将表列绑定到getters而不是可观察的属性,因此他们无法知道数据何时更改。只需将列构建器指向属性:

column("ID", Person::idProperty)
column("Name", Person::nameProperty)

最新更新