Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
Jigang_Zhang张吉刚
Active Contributor
0 Kudos
5,461
There're some legacy customized reports which been batch-created quickly and find out no layout Save button in the toolbar.


 

All those reports are using OO ALV refer to class cl_salv_table and its GUI status is copy refer to standard Application Toolbar SALV_TABLE_STANDARD which already contains function code (&AVE) for "Save Layout" button.


 

Here is a quick-fix example:
DATA: gr_table TYPE REF TO cl_salv_table,
DATA: lf_variant TYPE slis_vari,
lr_layout TYPE REF TO cl_salv_layout,
ls_key TYPE salv_s_layout_key.

CALL METHOD gr_table->get_layout
RECEIVING
value = lr_layout.

lr_layout->set_save_restriction( if_salv_c_layout=>restrict_none ).
lf_variant = p_vari. "variant at selection screen

*... set the Layout Key
ls_key-report = sy-repid.
lr_layout->set_key( ls_key ). *<-----have to set this!!!

*... set usage of default Layouts
IF lf_variant IS NOT INITIAL. "layout variant
lr_layout->set_initial_layout( lf_variant ).
ELSE. "no layout variant! default!!IMPORTANT!
lr_layout->set_default( abap_true ).
ENDIF.

And it's quite a common requirement to provide layout variant at the selection screen along with saving layout button. Below is code as a template to deal with this (forget where it has been copied~):
PARAMETERS: p_vari LIKE disvariant-variant .

* Data for ALV variant
DATA gv_repname LIKE sy-repid.
DATA gv_x_variant LIKE disvariant.
DATA lv_value LIKE bapicurr-bapicurr.
DATA gv_exit(1) TYPE c.
DATA: gt_cat TYPE lvc_t_fcat,
gs_cat LIKE LINE OF gt_cat.
DATA gv_save(1) TYPE c.
DATA gv_variant LIKE disvariant.
FIELD-SYMBOLS: <fs_table> TYPE STANDARD TABLE,
<fs_wa>,
<fs_field>.
DATA : color_tab TYPE STANDARD TABLE OF lvc_s_scol,
wa_color LIKE LINE OF color_tab.
DATA: gt_usr TYPE TABLE OF usdef,
gs_usr LIKE LINE OF gt_usr.


AT SELECTION-SCREEN.
*- Validating selection screen fields
PERFORM f_at_selection_screen.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_vari.
*- For variant
PERFORM f_f4_for_variant.

INITIALIZATION.
* Initialize ALV Layout variant
PERFORM f_initialize_variant.

*---------------------------------------------------------------------*
* FORM F_INITIALIZE_VARIANT *
*---------------------------------------------------------------------*
* INITALIZE VARIANT *
*---------------------------------------------------------------------*
FORM f_initialize_variant .
gv_repname = sy-repid.

CLEAR gv_variant.
gv_save = 'X'.
gv_variant-report = gv_repname.
gv_x_variant = gv_variant.

CALL FUNCTION 'REUSE_ALV_VARIANT_DEFAULT_GET'
EXPORTING
i_save = gv_save
CHANGING
cs_variant = gv_x_variant
EXCEPTIONS
not_found = 2.

IF sy-subrc = 0.
p_vari = gv_x_variant-variant.
ENDIF.

ENDFORM. " f_initialize_variant
*---------------------------------------------------------------------*
* FORM F_f4_for_varint *
*---------------------------------------------------------------------*
* F4 help for VARIANT *
*---------------------------------------------------------------------*
FORM f_f4_for_variant .
CALL FUNCTION 'REUSE_ALV_VARIANT_F4'
EXPORTING
is_variant = gv_variant
i_save = gv_save
IMPORTING
e_exit = gv_exit
es_variant = gv_x_variant
EXCEPTIONS
not_found = 2.

IF sy-subrc = 2.
MESSAGE ID sy-msgid TYPE 'S' NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ELSE.
IF gv_exit = space.
p_vari = gv_x_variant-variant.
ENDIF.
ENDIF.

ENDFORM. " f_f4_for_variant
*---------------------------------------------------------------------*
* FORM F_at_selection_screen *
*---------------------------------------------------------------------*
* VARIANT setting *
*---------------------------------------------------------------------*
FORM f_at_selection_screen .

* ALV Layout variant
IF NOT p_vari IS INITIAL.
MOVE gv_variant TO gv_x_variant.
MOVE p_vari TO gv_x_variant-variant.

CALL FUNCTION 'REUSE_ALV_VARIANT_EXISTENCE'
EXPORTING
i_save = gv_save
CHANGING
cs_variant = gv_x_variant.
gv_variant = gv_x_variant.
ELSE.
PERFORM f_initialize_variant.
ENDIF.
ENDFORM.
Labels in this area