模型更改后更新 table 内容
Update table contents after model change
我正在测试 tornadofx 框架(主要是复制粘贴示例),我有一个奇怪的问题,即 table 内容在编辑后没有更新。我看到
的内容
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()
}
}
}
}
}
}
}
}
}
根据文档,控制器提交后,视图会自动更新。
您将 table 列绑定到 getter 而不是可观察的属性,因此它们无法知道数据何时更改。只需将列构建器指向属性即可:
column("ID", Person::idProperty)
column("Name", Person::nameProperty)
我正在测试 tornadofx 框架(主要是复制粘贴示例),我有一个奇怪的问题,即 table 内容在编辑后没有更新。我看到
的内容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()
}
}
}
}
}
}
}
}
}
根据文档,控制器提交后,视图会自动更新。
您将 table 列绑定到 getter 而不是可观察的属性,因此它们无法知道数据何时更改。只需将列构建器指向属性即可:
column("ID", Person::idProperty)
column("Name", Person::nameProperty)