niteesh
niteesh

Reputation: 329

Tornadofx - controlling tableview row object while build

My requirement is kinda similar to what Angular provides for HTML web pages.
Basically when creating a table, you iterate over a list of records to get a record and then we can remove a row based on a if condition. Something like this:

ng-repeat = "record in records"
ng-if = "record.Id != 0"

Similarly in Tornadofx, if I want to build a tableview, I do this:

tableview<Record>(recordList) {
    //I want to remove row with the 0th recordId 
    column("Id", Record::Id)
    column("First Name", Record::firstNameProperty)
    column("Last Name", Record::lastNameProperty)
}

Seeing the tutorials, I have tried the following but with no luck:

val removeZeroId = booleanBinding(idProp, idProp) {
    id != "0"
}
visibleWhen {
    //compilation
    Record::removeZeroId
}

It would be easy if I could have the row object in tableview and perform operations on it.

Frankly, I haven't clearly understood tornadofx bindings, so I might be missing something basic.

Upvotes: 0

Views: 660

Answers (1)

Edvin Syse
Edvin Syse

Reputation: 7297

You should use a SortedFilteredList to filter out the items you don't want. This keeps the UI logic clean, as you only operate on a view into your underlying data structure. This is much unlike Angular, where you have to decide about these things as you draw table rows for each record you want to display.

The SortedFilteredList can be configured with a predicate function, which is passed a record and expects a Boolean value to determine if the current record should be visible in the list.

class MyView : View() {
    val recordList = getYourListOfRecordsFromSomewhere()
    val data = SortedFilteredList(recordList)

    override val root = tableview(data) {
        column("Id", Record::idProperty)
        column("First Name", Record::firstNameProperty)
        column("Last Name", Record::lastNameProperty)
    }

    init {
        // Configure the filter predicate for the SortedFilteredList
        data.predicate = { it.id != 1 }
    }
}

Also note that you can update the filter predicate at any time. Changes to the predicate will immediately be visible in the displayed rows.

Upvotes: 2

Related Questions