简体   繁体   English

为什么不能对 CL_SALV_TABLE 中的 integer 进行小计?

[英]Why isn't it possible to do a subtotal over an integer in CL_SALV_TABLE?

Let's assume we have the following table in ABAP:假设我们在 ABAP 中有下表:

PERNR PERNR WORKHOURS工作时间 Other_Fields Other_Fields
00012 00012 12,00 12,00 - -
00110 00110 5,00 5,00 - -
00120 00120 22,00 22,00 - -

PERNR is on its DB-table saved as a NUMC with leading 0's. PERNR 在其 DB 表上保存为带有前导 0 的 NUMC。 Due to some must-have-features, the leading 0's must be deleted before displaying a SALV-Table with CL_SALV_TABLE (as it is not accessible later on since).由于一些必备功能,在显示带有 CL_SALV_TABLE 的 SALV 表之前必须删除前导 0(因为它以后无法访问)。 So essentially we have the following example data:所以基本上我们有以下示例数据:

PERNR PERNR WORKHOURS工作时间 Other_Fields Other_Fields
12 12 12,00 12,00 - -
110 110 5,00 5,00 - -
120 120 22,00 22,00 - -

Now I want to do the following:现在我想做以下事情:

  1. Do a total over WORKHOURS总共超过WORKHOURS
  2. Do a subtotal over PERNR对 PERNR 进行小计

But since PERNR is a NUMC, the result is the following:但由于 PERNR 是 NUMC,所以结果如下:

PERNR PERNR WORKHOURS工作时间 Other_Fields Other_Fields
110 110 5,00 5,00 - -
12 12 12,00 12,00 - -
120 120 22,00 22,00 - -

AFAIK, NUMC is getting compared from left to right and then this order makes sense. AFAIK,NUMC 从左到右进行比较,然后这个顺序是有意义的。 Sadly, I cannot change this, when I create my own dataelement for this or my own domain (At least I didn't saw an option to...).可悲的是,当我为此或我自己的域创建自己的数据元素时,我无法更改这一点(至少我没有看到...的选项)。

When I cast PERNR as an INTEGER or DEC however, the sorting works and the order is as expected.但是,当我将 PERNR 转换为 INTEGER 或 DEC 时,排序工作正常,并且顺序符合预期。 Additionaly there is no need to delete leading 0's since these datatypes don't display or have leading 0's.另外,无需删除前导 0,因为这些数据类型不显示或没有前导 0。 BUT when I try to do my Steps 1. and 2., I get the following Error when trying Step 2. : "Subtotals cannot be calculated on aggregatable columns"但是当我尝试执行步骤 1 和 2 时,尝试步骤 2 时出现以下错误:“无法在可聚合列上计算小计”

...Why? ...为什么? I don't see a reason to not aggregate these values.我认为没有理由不汇总这些值。 I haven't found any documentation so far as to why this is not allowed (or possible) either.到目前为止,我还没有找到任何文档说明为什么不允许(或可能)这样做。 I found numerous websites that explain, how to do it by coding it, but when I try to do a subtotal via coding I get the same error, so I'm pretty stuck.我发现许多网站解释了如何通过编码来完成,但是当我尝试通过编码进行小计时,我得到了同样的错误,所以我很困惑。

Is it somehow possible to still do a subtotal over the column PERNR?是否仍有可能对 PERNR 列进行小计?

EDIT: @Suncatcher Sry, I used a tabletype in my datadictionary and didn't wanted to make the Post too large.编辑:@Suncatcher Sry,我在我的数据字典中使用了一个表格类型,不想让帖子太大。 I'll try to define my coding here as short as possible.我将尝试尽可能短地定义我的编码。 Structuretype and Tabletype that has this as stucture defined:将其定义为结构的结构类型和表格类型:

@EndUserText.label : 'Structure for test salv'
@AbapCatalog.enhancementCategory : #NOT_EXTENSIBLE
define type ZS_SALV_STRUCTURE {
pernr        : abap.numc(8);
workhours    : abap.dec(4,2);
other_fields : abap.string(256);
}

Tabletype is called ZT_SALV_STRUCTURE in this example.在此示例中,表类型称为ZT_SALV_STRUCTURE The Report is shortened as follows (Example where I delete leading 0's from NUMC):报告缩短如下(我从 NUMC 中删除前导 0 的示例):

REPORT zsalv_test.
DATA: gv_okcode TYPE sy-ucomm,
      gx_salv   TYPE REF TO cl_salv_table,
      gt_data   TYPE zt_salv_structure.

START-OF-SELECTION.
  "Example 1: Delete leading 0's
  "Create example data to substitute the CDS-View
  gt_data = VALUE #( ( pernr = '012' workhours = 12 other_fields = '-' )
                     ( pernr = '110' workhours = 5  other_fields = '-' )
                     ( pernr = '120' workhours = 22 other_fields = '-' ) ).
  "Delete leading 0's
  LOOP AT gt_data ASSIGNING FIELD-SYMBOL(<ls_data>).
    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
      EXPORTING
        input  = <ls_data>-pernr
      IMPORTING
        output = <ls_data>-pernr.
  ENDLOOP.
  "Example 1 END

  "The Dynpro has gv_okcode for the OKCODE and is filled with a single custom container named 'CONTAINER'
  CALL SCREEN 0100.

*&---------------------------------------------------------------------*
*& Module STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
  IF gx_salv IS INITIAL.
    cl_salv_table=>factory(
      EXPORTING
        r_container    = NEW cl_gui_custom_container( container_name = 'CONTAINER' )
        container_name = 'CONTAINER'
      IMPORTING
        r_salv_table   = gx_salv
      CHANGING
        t_table        = gt_data
    ).
    gx_salv->get_functions( )->set_all( abap_true ).
    gx_salv->get_columns( )->set_optimize( abap_true ).
    DATA(lt_columns) = gx_salv->get_columns( )->get( ).
    LOOP AT lt_columns ASSIGNING FIELD-SYMBOL(<ls_column>).
      CASE <ls_column>-columnname.
        WHEN 'PERNR'.
          <ls_column>-r_column->set_long_text( 'PERNR' ).
        WHEN 'WORKHOURS'.
          <ls_column>-r_column->set_long_text( 'WORKHOURS' ).
        WHEN 'OTHER_FIELDS'.
          <ls_column>-r_column->set_long_text( 'OTHER_FIELDS' ).
      ENDCASE.
    ENDLOOP.
    gx_salv->display( ).
  ELSE.
    gx_salv->refresh( ).
  ENDIF.

ENDMODULE.
*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
MODULE user_command_0100 INPUT.

ENDMODULE.

When I use INTEGER or DEC for PERNR, I change the Structuretype as follows and don't delete the leading 0's: INTEGER:当我将 INTEGER 或 DEC 用于 PERNR 时,我将 Structuretype 更改如下,并且不删除前导 0:INTEGER:

pernr        : abap.int4;

DEC:十二月:

pernr        : abap.dec(8,0);

And I "cast" my data in my Consumption-View via the CAST -Expression INTEGER:我通过CAST -Expression INTEGER 在我的消费视图中“投射”我的数据:

cast(PersonnelNumber as abap.int4) as pernr;

DEC:十二月:

cast(PersonnelNumber as abap.dec(8,0) as pernr;

As for how I am sorting and do a (sub-)total: I use the function provided by the CL_SALV_TABLE class on the dynpro while running the report (Small buttons on top of the SALV).至于我如何排序并进行(子)总计:我在运行报告时使用 dynpro 上的 CL_SALV_TABLE class 提供的 function (SALV 顶部的小按钮)。

Subtotal it's only on numeric data type.小计仅适用于数字数据类型。 NUMC it's a char like field that allow only numbers but it's a char. NUMC 它是一个类似字符的字段,只允许数字,但它是一个字符。

If you have this table... |如果你有这张桌子... | PERNR | PERNR | Month |月 | Hours |小时 | |-------|-------|-------| |-------|-------|--------| | | 0010 | 0010 | Dec. |十二月 | 160 | 160 | | | 0010 | 0010 | Nov. |十一月 | 100 | 100 | | | 0020 | 0020 | Dec. |十二月 | 80 | 80 |

You must have this Subtotals options Group by PERNR |您必须拥有此分类汇总选项 Group by PERNR | PERNR | PERNR | Month |月 | Hours |小时 | |-------|-------|-------| |-------|-------|--------| | | 0010 | 0010 | | | 260 | 260 | | | 0020 | 0020 | | | 80 | 80 |

Group by Month |按月分组 | PERNR | PERNR | Month |月 | Hours |小时 | |-------|-------|-------| |-------|-------|--------| | | | | Dec. |十二月 | 240 | 240 | | | | | Nov. |十一月 | 100 | 100 |

If you wanna show PERNR with zero leadings, you can force ALPHA as conversion exit in the fieldcat.如果您想以零前导显示 PERNR,您可以强制 ALPHA 作为 fieldcat 中的转换出口。

Best regards.此致。

Sebastian塞巴斯蒂安

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM