【问题标题】:Update table contents after model change模型更改后更新表格内容
【发布时间】:2018-02-09 20:26:59
【问题描述】:

我正在测试 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()
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

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

【问题讨论】:

    标签: tornadofx


    【解决方案1】:

    您将表列绑定到 getter 而不是可观察属性,因此它们无法知道数据何时更改。只需将列生成器指向属性即可:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-11
      • 2018-06-16
      • 2021-08-20
      • 2012-06-12
      • 1970-01-01
      • 2019-05-17
      • 1970-01-01
      • 2020-05-04
      相关资源
      最近更新 更多