【问题标题】:sort the table by column name Exposed Kotlin按列名对表进行排序 Exposed Kotlin
【发布时间】:2021-03-05 14:24:45
【问题描述】:

下午好,我想对所有表格进行通用排序。这个想法是该方法将接收列的名称作为输入,并且通过反射,我将接收到同名字段的链接。

 val id = "id"
        var a = JobSeekerTable::class
        a.memberProperties.forEach { e ->
            if (e.name == id) {
                transaction {
                    JobSeeker.all().sortedBy { e.getter }
                }

            }
        }

很遗憾,这不起作用。有一个选项,通过表的字段字段

JobSeekerTable.fields.forEach {v->
            transaction {
                JobSeeker.all().sortedBy { v }
            }
        }

但也没有成功:( 如果有任何方法可以通过名称引用必填字段。不使用 if 之类的东西?

【问题讨论】:

    标签: kotlin kotlin-exposed


    【解决方案1】:

    首先,您可能正在寻找orderBy,而不是sortedBy。前者是对SQL查询结果进行排序,后者是对一个集合进行排序。

    其次,你要传递一个列的实例:

    val id = "id"
    JobSeekerTable.selectAll().orderBy(JobSeekerTable.columns.find {
        it.name == id // Here I used the name you provided, although probably it should be named something like columnName
    } !!  to SortOrder.ASC)
    

    在 Kotlin 中使用“尖叫”运算符 (!!) 是一种不好的做法。因此,如果您的所有表都有 ID 列,例如,您可以使用“elvis”运算符。

    JobSeekerTable.selectAll().orderBy((JobSeekerTable.columns.find {
        it.name == id
    } ?: JobSeekerTable.id) to SortOrder.ASC)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-15
      • 1970-01-01
      • 1970-01-01
      • 2021-07-06
      • 1970-01-01
      • 2021-02-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多