按数据类型动态隐藏 ALV 列 [英] Dynamically hide ALV columns by data type

查看:50
本文介绍了按数据类型动态隐藏 ALV 列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:我正在使用 cl_salv_table 类来生成和修改 ALV.此 ALV 显示一个类型为 zpm_et_qual_notif_s 的表,其中每个偶数行都是一个类型为 CHAR 长度为 1 的分隔符字段,名称为 DELIM1DELIM2...等.由于没有理由在 ALV 中显示分隔符列,我想删除它们.

Background: I'm using the cl_salv_table class to generate and modify an ALV. This ALV displays a table of type zpm_et_qual_notif_s, where every even number row is a delimiter field of type CHAR length 1 with names DELIM1, DELIM2...etc. Since there's no reason to have delimiter columns displayed in the ALV, I'd like to remove them.

注意:我在标题中保留了通用的ABAP 字典/内部结构",因为我是否从 ABAP 字典结构或我从它定义的内部表中确定列数对我来说无关紧要.

Note: I left "ABAP Dictionary/Internal Structure" generic in the title because whether or not I determine the column count from the ABAP Dictionary structure or the internal table I define from it matters not to me.

对我来说最简单的解决方案是拥有这 15 个语句,因为目前有 15 个分隔符字段:

The easy solution for me would be to have these 15 statements, as there are currently 15 delimiter fields:

lv_alv->get_columns( )->get_column( 'DELIM1' )->set_visible( if_salv_c_bool_sap=>false ).
lv_alv->get_columns( )->get_column( 'DELIM2' )->set_visible( if_salv_c_bool_sap=>false ).
lv_alv->get_columns( )->get_column( 'DELIM3' )->set_visible( if_salv_c_bool_sap=>false ).
lv_alv->get_columns( )->get_column( 'DELIM4' )->set_visible( if_salv_c_bool_sap=>false ).
lv_alv->get_columns( )->get_column( 'DELIM5' )->set_visible( if_salv_c_bool_sap=>false ).
...

这样做的问题是,如果将新字段添加到表中,我的程序也必须更新.出于这个原因,而且这种方法需要许多几乎重复的行,我发现这是一个草率的解决方案.

The problem with this is that if new fields are added to the table, my program would also have to be updated. For this reason, and that this approach requires many almost duplicate lines, I find this to be a sloppy solution.

我认为更简洁的解决方案是以如下方式动态设置所有分隔符列的可见性:

What I believe to be a cleaner solution is to dynamically set the visibility of all of the delimiter columns in a manner like so:

" Dynamically hide delimiter columns
DATA lv_idx TYPE syst_index VALUE 1.
WHILE lv_idx < 16. " Number of delimiters
    lv_alv->get_columns( )->get_column( |DELIM{ lv_idx }| )->set_visible( if_salv_c_bool_sap=>false ).
    lv_idx = lv_idx + 1.
ENDWHILE.

这很好,因为它是一个简单的解决方案并且引入的开销最小.但是,我仍然遇到必须对分隔符列的数量进行硬编码的问题.一个理想的解决方案会让我这样做:

This is nice because it's a simple solution and introduces minimal overhead. However, I still have the problem of having to hard code the number of delimiter columns. An ideal solution would let me do this:

" Dynamically hide delimiter columns
DATA lv_idx TYPE syst_index VALUE 1.
WHILE lv_idx < ( columns( 'ZPM_ET_QUAL_NOTIF_S' ) / 2 ). " Number of delimiters
    lv_alv->get_columns( )->get_column( |DELIM{ lv_idx }| )->set_visible( if_salv_c_bool_sap=>false ).
    lv_idx = lv_idx + 1.
ENDWHILE.

...但当然,那不是一回事.

...but of course, that isn't a thing.

如何动态获取内部表或它所基于的 ABAP 字典结构的列数?当然有一些动态解决方案.试图自己解决这个问题导致我玩弄 cl_abap_structdescrcl_abap_tabledescr,但没有任何实质性的结果.如果我的整个方法很糟糕,我很乐意更改它以遵循良好的做法.

How may I dynamically get the column count of my internal table, or of the ABAP Dictionary structure that it's based upon? Surely there's some dynamic solution to this. Trying to solve this problem myself led me to toying around with cl_abap_structdescr and cl_abap_tabledescr, but nothing substantial came of it. If my entire approach is bad, I'm comfortable with changing it in order to follow good practice.

推荐答案

首先,我会用 set_technical 标记这些列,以防止它们被完全显示.set_visible( abap_false ) 仅在当前/初始显示中隐藏它们,但用户可以选择显示这些可能会造成混淆的列.

First of all, I would tag these columns with set_technical to prevent them from being displayed entirely. set_visible( abap_false ) only hides them from the current/initial display, but the user can select to show these columns which might be confusing.

然后,我可能会尝试通过它们的数据元素而不是它们的位置来区分这些列.(伪)代码,未经测试:

Then, I would probably try to distinguish these columns by their data element rather than their position. (Pseudo-)code, untested:

DATA(columns) = my_alv->get_columns( ).
DATA(column_list) = columns->get( ).
LOOP AT column_list ASSIGNING FIELD-SYMBOL(<column>).
  IF <column>-r_column->get_ddic_rollname( ) = 'Z_IRRELEVANT_DELIMITER'.
    <column>-r_column->set_technical( ).
  ENDIF.
ENDLOOP.

这篇关于按数据类型动态隐藏 ALV 列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆