根据数据类型动态隐藏ALV列 [英] Dynamically Hide ALV Columns by Data Type

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

问题描述

背景:我正在使用 cl_salv_table 类生成和修改ALV。该ALV显示一个类型为 zpm_et_qual_notif_s 的表,其中每个偶数行都是一个类型为 CHAR 的定界符字段,长度为<$。 c $ c> 1 ,名称为 DELIM1 DELIM2 ...等。由于没有理由在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_structdescr cl_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天全站免登陆