新手基本问题:
我想同步/绑定两个表。
为了使示例简单,我使用了两个单独的表视图。这需要使用片段和范围来完成,我认为这会使问题复杂化,因为我陷入了一个基本问题。
行为:单击表 1 的同步按钮时,我希望表 1 选择的数据覆盖相应的表 2 数据。反之亦然
人物模型:
class Person(firstName: String = "", lastName: String = "") {
val firstNameProperty = SimpleStringProperty(firstName)
var firstName by firstNameProperty
val lastNameProperty = SimpleStringProperty(lastName)
var lastName by lastNameProperty
}
class PersonModel : ItemViewModel<Person>() {
val firstName = bind { item?.firstNameProperty }
val lastName = bind { item?.lastNameProperty }
}
人员控制器(虚拟数据):
class PersonController : Controller(){
val persons = FXCollections.observableArrayList<Person>()
val newPersons = FXCollections.observableArrayList<Person>()
init {
persons += Person("Dead", "Stark")
persons += Person("Tyrion", "Lannister")
persons += Person("Arya", "Stark")
persons += Person("Daenerys", "Targaryen")
newPersons += Person("Ned", "Stark")
newPersons += Person("Tyrion", "Janitor")
newPersons += Person("Arya", "Stark")
newPersons += Person("Taenerys", "Dargaryen")
}
}
人员列表视图:
class PersonList : View() {
val ctrl: PersonController by inject()
val model : PersonModel by inject()
var personTable : TableView<Person> by singleAssign()
override val root = VBox()
init {
with(root) {
tableview(ctrl.persons) {
personTable = this
column("First Name", Person::firstNameProperty)
column("Last Name", Person::lastNameProperty)
columnResizePolicy = SmartResize.POLICY
}
hbox {
button("Sync") {
setOnAction {
personTable.bindSelected(model)
//model.itemProperty.bind(personTable.selectionModel.selectedItemProperty())
}
}
}
}
}
另一个人列表视图:
class AnotherPersonList : View() {
val model : PersonModel by inject()
val ctrl: PersonController by inject()
override val root = VBox()
var newPersonTable : TableView<Person> by singleAssign()
init {
with(root) {
tableview(ctrl.newPersons) {
newPersonTable = this
column("First Name", Person::firstNameProperty)
column("Last Name", Person::lastNameProperty)
columnResizePolicy = SmartResize.POLICY
}
hbox {
button("Sync") {
setOnAction {
newPersonTable.bindSelected(model)
}
}
}
}
}
}