【问题标题】:custom order ignores SQL ORDER BY code自定义订单忽略 SQL ORDER BY 代码
【发布时间】:2017-05-29 10:59:07
【问题描述】:

我正在尝试使用 custom_order 来呈现任意初始顺序。

# GET /sequences
def index
@sequences_grid = initialize_grid(Sequence,
  enable_export_to_csv: false,
              per_page: 10,
          custom_order: {
   'sequences.username' => "CASE WHEN username LIKE '#{current_user.username}' THEN 0 ELSE 1 END, username"
                        },
                  name: 'seq_g1')
end

这个想法是让当前用户的记录渗透到列表的顶部。

用户指南指出“键是数据库列的完全限定名称,并为在 ORDER BY 子句中使用所需的 SQL 块赋值”。

SQL 代码在数据库编辑器中运行时按预期工作,但网格未显示所需的顺序。

日志不会显示任何 wice_grid 问题,并且报告的请求是:

10:10:35 web.1  |   Sequence Load (0.3ms)  SELECT distinct username FROM "sequences"  ORDER BY username asc
10:10:35 web.1  |   Sequence Load (0.3ms)  SELECT distinct classtype FROM "sequences"  ORDER BY classtype asc
10:10:35 web.1  |   Sequence Load (0.2ms)  SELECT distinct description FROM "sequences"  ORDER BY description asc
10:10:35 web.1  |   Sequence Load (0.2ms)  SELECT distinct sequencenumber FROM "sequences"  ORDER BY sequencenumber asc
10:10:35 web.1  |   Sequence Load (0.2ms)  SELECT distinct target FROM "sequences"  ORDER BY target asc
10:10:35 web.1  |   Sequence Load (0.2ms)  SELECT distinct indicator FROM "sequences"  ORDER BY indicator asc

我怀疑 SQL 代码可能以某种方式被破坏,但在我深入挖掘之前,我认为有人可能遇到了类似的问题?

问候, 汤姆。

【问题讨论】:

    标签: wice-grid


    【解决方案1】:

    经过一番折腾,我发现 SQL 没问题,但您需要指定 order 以及 custom_order。

    用户指南提供了一个示例,因此看起来有点误导......

    @hosts_grid = initialize_grid(Host,
      custom_order: {
     'hosts.ip_address' => 'INET_ATON(hosts.ip_address)'
                    })
    

    但是这些例子提供了......

    @status_grid1 = initialize_grid(Status,
      order: 'statuses.name',
      custom_order: {
        'statuses.name' => 'length( ? )'
      }
    )
    

    所以下面做了我需要的...

    # GET /sequences
    def index
    @sequences_grid = initialize_grid(Sequence,
      enable_export_to_csv: false,
                  per_page: 10,
                     order: 'sequences.username',
              custom_order: { 'sequences.username' => "CASE WHEN sequences.username LIKE '#{current_user.username}' THEN 0 ELSE 1 END, username, updated_at" },
           order_direction: 'desc',  
                      name: 'seq_g1')
    end
    

    ...即它创建了一个列表,其中当前用户的记录按updated_at降序排列,然后是其他用户的记录。

    【讨论】:

      猜你喜欢
      • 2010-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-01
      • 2011-05-04
      • 2012-01-06
      • 1970-01-01
      • 2011-02-12
      相关资源
      最近更新 更多