Views
6 months ago

tornadofx-guide

Advanced Data Controls

Advanced Data Controls Now comes the ListCellFragment itself. class PersonListFragment : ListCellFragment() { val person = PersonModel().bindTo(this) } override val root = form { fieldset { field("Name") { label(person.name) } field("Birthday") { label(person.birthday) } label(stringBinding(person.age) { "$value years old" }) { alignment = Pos.CENTER_RIGHT style { fontSize = 22.px fontWeight = FontWeight.BOLD } } } } Because this Fragment will be reused to represent different list items, the easiest approach is to bind the ui elements to the ViewModel's properties. The name and birthday properties are bound directly to the labels inside the fields. The age string in the last label needs to be constructed using a stringBinding to make sure it updates when the item changes. While this might seem like slightly more work than the cellFormat example, this approach makes it possible to leverage everything the Fragment class has to offer. It also forces you to define the cell node graph outside of the builder hierarchy, which improves refactoring possibilities and enables code reuse. Additional helpers and editing support The ListCellFragment also have some other helper properties. They include the cellProperty which will update whenever the underlying cell changes and the editingProperty , which will tell you if this the underlying list cell is in editing mode. There are also editing helper functions called startEdit , commitEdit , cancelEdit plus an onEdit callback. The ListCellFragment makes it trivial to utilize the existing editing capabilites of the ListView . A complete example can be seen in the TodoMVC demo application. 204

Advanced Data Controls 205

GUIDE
GUIDE
Guide
Guide
GUIDE
GUIDE
GUIDE
GUIDE
guide
GUIDE
Guide
GUIDE
Guide