结构/内部表的动态语言输出

时间:2017-08-23 14:31:51

标签: dynamic abap

我有一个带选择选项的选择屏幕,我想输入一些有关材料的信息,例如:材料编号等。

用户还可以输入输出所在的语言。

如果用户选择英语,程序将显示一个内部表格,其中包含材料编号,语言,材料名称。如果用户输入西班牙语,我希望输出为西班牙语。

为了定义动态结构/表格,我需要做些什么来显示依赖于所选语言的相应列?

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

它高度依赖于您要向用户显示的数据结构,但通常您需要动态结构,而是需要填充数据动态,即取决于当前的用户语言。

例如,素材文本存储在MAKT text table中,其中文本与通常检索它们的语言键一起存储:

  

选择
          一〜MATNR
          一〜WERKS
          b~maktx
来自ekpo AS a                   INNER JOIN makt AS b
                  ON b~matnr = a~matnr
                   AND b~spras = sy-langu
                  INTO相应的表格int_out
                  WHERE
                  a~matnr IN s_matnr和
                  a〜werks IN s_werks。

SAP中的其他描述通常也存储在文本表中。 有关 sy-langu 和其他系统字段is here的更多信息。

更新:如果您真的想要一个包含所有语言的动态结构,请参阅以下示例:

DATA: lang TYPE SPRAS.
* language selection
SELECT-OPTIONS: s_lang FOR lang.

SELECT a~matnr, a~werks, b~maktx, b~spras UP TO 5000 ROWS
FROM ekpo AS a
JOIN makt AS b
ON b~matnr = a~matnr
INTO TABLE @DATA(int_out)
WHERE a~werks LIKE '3%'
  AND  a~matnr LIKE '1%'
  AND b~spras IN @s_lang.

*finding unique languages
DATA lt_langs TYPE TABLE OF spras.
lt_langs = VALUE #( ( '' ) ).
LOOP AT int_out ASSIGNING FIELD-SYMBOL(<fs_out>)
    GROUP BY ( lang = to_upper( val = <fs_out>-spras ) ) ASCENDING
    WITHOUT MEMBERS
        ASSIGNING FIELD-SYMBOL(<ls_lang>).
        APPEND <ls_lang>-lang TO lt_langs.
ENDLOOP.

DATA :
ls_component    TYPE cl_abap_structdescr=>component,
gt_components   TYPE cl_abap_structdescr=>component_table.

*adding MATNR column
ls_component-name = 'MATNR'.
ls_component-type ?= cl_abap_datadescr=>describe_by_name( 'matnr' ).
APPEND ls_component TO gt_components.

*Creating dynamic structure with column for every lang
LOOP AT lt_langs ASSIGNING FIELD-SYMBOL(<fs_lang>).
CONDENSE <fs_lang>.
IF <fs_lang> IS NOT INITIAL.
    ls_component-name = 'makt_' && <fs_lang>.
    ls_component-type ?= cl_abap_datadescr=>describe_by_name( 'maktx' ).
    APPEND ls_component TO gt_components.
ENDIF.
ENDLOOP.

* constructing dynamic structure
DATA: gr_struct_typ   TYPE REF TO  cl_abap_datadescr.
      gr_struct_typ  ?= cl_abap_structdescr=>create( p_components = gt_components ).
* constructing table from structure
DATA: gr_dyntable_typ TYPE REF TO  cl_abap_tabledescr.
      gr_dyntable_typ = cl_abap_tabledescr=>create( p_line_type = gr_struct_typ ).

DATA: gt_dyn_table     TYPE REF TO data,
      gw_dyn_line      TYPE REF TO data.

FIELD-SYMBOLS: <gfs_line>,<gfs_line1>,<fs1>,
               <gfs_dyn_table> TYPE STANDARD TABLE.

CREATE DATA: gt_dyn_table TYPE HANDLE gr_dyntable_typ,
             gt_dyn_table TYPE HANDLE gr_dyntable_typ,
             gw_dyn_line  TYPE HANDLE gr_struct_typ.

ASSIGN gt_dyn_table->* TO <gfs_dyn_table>.
ASSIGN gw_dyn_line->* TO <gfs_line>.

LOOP AT int_out ASSIGNING <fs_out>.
* checking for duplicated
READ TABLE <gfs_dyn_table> ASSIGNING <gfs_line1> WITH KEY ('MATNR') = <fs_out>-matnr.
IF sy-subrc = 0.
    CONTINUE.
ENDIF.
* assigning material number
LOOP AT gt_components ASSIGNING FIELD-SYMBOL(<fs_component>).
    IF <fs_component>-name = 'MATNR'.
        ASSIGN COMPONENT <fs_component>-name OF STRUCTURE <gfs_line> TO <fs1>.
        IF <fs1> IS ASSIGNED.
        <fs1> = <fs_out>-matnr.
        UNASSIGN <fs1>.
        ENDIF.
    ENDIF.
* assigning languge-dependent names
    READ TABLE int_out WITH KEY matnr = <fs_out>-matnr
                                spras = <fs_component>-name+5
                                ASSIGNING FIELD-SYMBOL(<fs_spras>).
    IF sy-subrc = 0.
        ASSIGN COMPONENT <fs_component>-name OF STRUCTURE <gfs_line> TO <fs1>.
        IF <fs1> IS ASSIGNED.
        <fs1> = <fs_spras>-maktx.
        UNASSIGN <fs1>.
        ENDIF.
    ENDIF.

ENDLOOP.
APPEND <gfs_line> TO <gfs_dyn_table>.
CLEAR: <gfs_line>.

ENDLOOP.

DATA: l_lang TYPE spras VALUE 'E'.
* showing values in proper language depending on user input
LOOP AT <gfs_dyn_table> ASSIGNING <gfs_line>.
    ASSIGN COMPONENT 'makt_' && l_lang OF STRUCTURE <gfs_line> TO <fs1>.
    IF <fs1> IS ASSIGNED.
      WRITE / <fs1>.
      UNASSIGN <fs1>.
    ENDIF.
ENDLOOP.