【问题标题】:Tornadofx - controlling tableview row object while buildTornadofx - 在构建时控制 tableview 行对象
【发布时间】:2017-05-21 16:59:31
【问题描述】:

我的要求有点类似于 Angular 为 HTML 网页提供的要求。
基本上在创建表时,您遍历记录列表以获取记录,然后我们可以根据 if 条件删除一行。像这样:

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

同样在 Tornadofx 中,如果我想构建一个 tableview,我会这样做:

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)
}

看到教程,我尝试了以下但没有运气:

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

如果我可以在 tableview 中拥有行对象并对其执行操作,那将很容易。

坦率地说,我还没有清楚地理解 tornadofx 绑定,所以我可能会遗漏一些基本的东西。

【问题讨论】:

    标签: javafx kotlin tornadofx


    【解决方案1】:

    您应该使用SortedFilteredList 过滤掉您不想要的项目。这使 UI 逻辑保持干净,因为您只对底层数据结构的视图进行操作。这与 Angular 不同,您必须在其中做出决定 在为要显示的每条记录绘制表格行时了解这些内容。

    SortedFilteredList 可以使用predicate 函数进行配置,该函数传递一条记录并期望一个布尔值来确定当前记录是否应在列表中可见。

    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 }
        }
    }
    

    还请注意,您可以随时更新过滤谓词。的变化 谓词将立即在显示的行中可见。

    【讨论】:

    • 谢谢,这很简单。抱歉,我在实用程序课上错过了这个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-29
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多