Search on this Website

Thursday, April 26, 2007

Customize ALV grid layout at run time

Create a Two layout variant which you can choose at run time

type-pools: slis.
data: ln_fieldcat type slis_fieldcat_alv, "Fieldcatalog
tab_fieldcat type slis_t_fieldcat_alv, "table
ln_sortinfo type slis_sortinfo_alv, "Sort criteria
tab_sortinfo type slis_t_sortinfo_alv, "table
ln_layoutinfo type slis_layout_alv, "List layout
ln_events type slis_alv_event, "Events
tab_events type slis_t_event, "table
ln_gridinfo type lvc_s_glay,


ln_listheader type slis_listheader, "Table for
tab_listheader type slis_t_listheader, "list header
spec_layout type disvariant,
def_layout type disvariant,
gs_variant type disvariant, "finally chosen layout
g_znsi_alv_exit type c,
tabname_header type slis_tabname,"Variable for main data table
tabname_item type slis_tabname,"Variable for item table(opt.)
ln_keyinfo type slis_keyinfo_alv, "Links for Header-Detail rep
ls_repid like sy-repid, "Name of calling report
ucommform type slis_formname, "PAI form
pfstatform type slis_formname, "Form for setting PF Status
topofpageform type slis_alv_event-form. "Form for TOP-OF-PAGE

field-symbols: type standard table,
type standard table.


selection-screen begin of block alv0 with frame
title alvbxtxt.
*selection-screen: begin of line.
*selection-screen: comment 1(31) alvvrtxt for field p_vari.
parameters: p_vari like disvariant-variant . "List layout variant
*selection-screen: end of line.
parameters: p_save type c default 'A' no-display, "Save possible?
pselinfo type c default 'X' no-display. "Print SelectionInfo
parameter p_outmod type c default 'R' no-display. "Web download
selection-screen end of block alv0.

************************************************************************
* AT SELECTION-SCREEN *
************************************************************************


at selection-screen on value-request for p_vari.
* popup F4 help to select a layout
move ls_repid to spec_layout-report.

call function 'REUSE_ALV_VARIANT_F4'
exporting
is_variant = spec_layout
i_save = p_save
importing
e_exit = g_znsi_alv_exit
es_variant = spec_layout
exceptions
not_found = 1
program_error = 2
others = 3
.
if sy-subrc <> 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
else.
if g_znsi_alv_exit ne 'X'.
* set name of layout on selection screen
p_vari = spec_layout-variant.
endif.
endif.

at selection-screen on p_vari.
* If layout is specified, test if specified layout exist
if p_vari ne space.
clear spec_layout.
move p_vari to spec_layout-variant.
move ls_repid to spec_layout-report.

call function 'REUSE_ALV_VARIANT_EXISTENCE'
exporting
i_save = p_save
changing
cs_variant = spec_layout
exceptions
wrong_input = 1
not_found = 2
program_error = 3
others = 4.
if sy-subrc <> 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
else.
move-corresponding spec_layout to gs_variant.
endif.
endif.

*----------------------------------------------------------------------*
* S U B R O U T I N E S *
*----------------------------------------------------------------------*

*---------------------------------------------------------------------*
* FORM ADD_CHG_FIELDCAT *
*---------------------------------------------------------------------*
* Adds a field to the field catalog table or changes the attributes *
* of an existing field catalog entry *
*---------------------------------------------------------------------*
* --> FLDNAME Fieldname *
* --> ITABNAME Name of internal table *
* --> TXT_FIELD Text fieldname for current entry *
* --> ROW_POS Row position *
* --> COL_POS Column position *
* --> CURR_UNIT Currency unit fieldname for field *
* --> QTY_UNIT Quantity unit fieldname for field *
* --> COLOR_CD Output color specification *
* --> CHECKBOX Output field as checkbox *
* --> ICON Output field as icon *
*---------------------------------------------------------------------*
form add_chg_fieldcat using fldname like ln_fieldcat-fieldname
itabname like tabname_header
txt_field like ln_fieldcat-text_fieldname
row_pos like ln_fieldcat-row_pos
col_pos like ln_fieldcat-col_pos
curr_unit like ln_fieldcat-cfieldname
qty_unit like ln_fieldcat-qfieldname
color_cd like ln_fieldcat-emphasize
checkbox like ln_fieldcat-checkbox
icon like ln_fieldcat-icon.

clear ln_fieldcat.
ln_fieldcat-row_pos = row_pos.
ln_fieldcat-col_pos = col_pos.
ln_fieldcat-fieldname = fldname.
if itabname ne space.
ln_fieldcat-tabname = itabname.
else.
ln_fieldcat-tabname = tabname_header.
endif.
ln_fieldcat-text_fieldname = txt_field.
if curr_unit ne space.
ln_fieldcat-cfieldname = curr_unit.
if itabname ne space.
ln_fieldcat-ctabname = itabname.
else.
ln_fieldcat-ctabname = tabname_header.
endif.
endif.
if qty_unit ne space.
ln_fieldcat-qfieldname = qty_unit.
if itabname ne space.
ln_fieldcat-qtabname = itabname.
else.
ln_fieldcat-qtabname = tabname_header.
endif.
endif.
ln_fieldcat-emphasize = color_cd.
ln_fieldcat-checkbox = checkbox.
ln_fieldcat-icon = icon.

* Read the fieldcatalog table/ Insert if not present else modify
read table tab_fieldcat with key
fieldname = ln_fieldcat-fieldname
transporting no fields.
if sy-subrc ne 0.
append ln_fieldcat to tab_fieldcat.
elseif sy-subrc = 0.
modify tab_fieldcat from ln_fieldcat index sy-tabix
transporting cfieldname ctabname qfieldname qtabname
text_fieldname emphasize checkbox icon.
if row_pos ne space or col_pos ne space.
modify tab_fieldcat from ln_fieldcat index sy-tabix
transporting row_pos col_pos.
endif.
endif.

endform.


*---------------------------------------------------------------------*
* FORM CHG_COLUMN_TEXT *
*---------------------------------------------------------------------*
* Changes the column header text for an existing field catalog entry *
*---------------------------------------------------------------------*
* --> FLDNAME Fieldname of a fieldcatalog entry *
* --> FLD_HEADER_L Column header for list output - long (40) *
* --> FLD_HEADER_M Column header for list output - medium (20)*
* --> FLD_HEADER_S Column header for list output - short (10) *
*---------------------------------------------------------------------*
form chg_column_text using fldname like ln_fieldcat-fieldname
fld_header_l like ln_fieldcat-seltext_l
fld_header_m like ln_fieldcat-seltext_m
fld_header_s like ln_fieldcat-seltext_s.

clear ln_fieldcat.
ln_fieldcat-fieldname = fldname.
ln_fieldcat-seltext_l = fld_header_l.
ln_fieldcat-seltext_m = fld_header_m.
ln_fieldcat-seltext_s = fld_header_s.
if ln_fieldcat-seltext_l ne space.
ln_fieldcat-reptext_ddic = ln_fieldcat-seltext_l.
elseif ln_fieldcat-seltext_m ne space.
ln_fieldcat-reptext_ddic = ln_fieldcat-seltext_m.
elseif ln_fieldcat-seltext_s ne space.
ln_fieldcat-reptext_ddic = ln_fieldcat-seltext_s.
endif.
if ln_fieldcat-seltext_m eq space.
if ln_fieldcat-seltext_l ne space.
ln_fieldcat-seltext_m = ln_fieldcat-seltext_l.
else.
ln_fieldcat-seltext_m = ln_fieldcat-seltext_s.
endif.
endif.
if ln_fieldcat-seltext_s eq space.
if ln_fieldcat-seltext_m ne space.
ln_fieldcat-seltext_s = ln_fieldcat-seltext_m.
else.
ln_fieldcat-seltext_s = ln_fieldcat-seltext_l.
endif.
endif.
read table tab_fieldcat with key
fieldname = ln_fieldcat-fieldname
transporting no fields.
if sy-subrc eq 0.
modify tab_fieldcat from ln_fieldcat index sy-tabix
transporting seltext_l seltext_m seltext_s reptext_ddic.
endif.

endform.

*---------------------------------------------------------------------*
* FORM FIELDCAT_INIT *
*---------------------------------------------------------------------*
* Initializes the fieldcatalog from the internal table/Database struct*
*---------------------------------------------------------------------*
* --> ITAB Internal Table name *
* --> ISTRUC Structure name *
*---------------------------------------------------------------------*
form fieldcat_init using itab type slis_tabname
istruc type slis_tabname.

data: begin of li_callback_prog_tab,
i_callback_program like sy-repid,
i_tabname like tfdir-funcname,
i_strucname like dd02l-tabname,
end of li_callback_prog_tab .

concatenate ls_repid
itab into li_callback_prog_tab.

free memory id li_callback_prog_tab.

call function 'REUSE_ALV_FIELDCATALOG_MERGE'
exporting
i_program_name = ls_repid
i_internal_tabname = itab
i_structure_name = istruc
i_inclname = ls_repid
i_bypassing_buffer = 'X'
changing
ct_fieldcat = tab_fieldcat
exceptions
inconsistent_interface = 1
program_error = 2
others = 3
.
if sy-subrc <> 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
exit.
endif.

clear ln_fieldcat.
modify tab_fieldcat from ln_fieldcat transporting key where key = 'X'.
endform.

*---------------------------------------------------------------------*
* FORM PRINT_LIST *
*---------------------------------------------------------------------*
* Outputs the lists using either the Classic or the Grid version of *
* the List Viewer *
*---------------------------------------------------------------------*
* --> LS_TYPE Type (L=Classic, G=Grid, S=Header-Detail List)*
* --> P_OPTIMIZE_COLS 'X' = Optimize column widths *
* --> P_DATATAB_HEAD Internal table name(Header) required *
* --> P_DATATAB_ITEM Internal table name(item) can be blank *
*---------------------------------------------------------------------*
form print_list using ls_type type c
p_optimize_cols type c
p_datatab_head type slis_tabname
p_datatab_item type slis_tabname.

data: table type slis_tabname.


if p_datatab_head ne space.
concatenate p_datatab_head '[]' into table.
assign (table) to .
endif.
if p_datatab_item ne space.
concatenate p_datatab_item '[]' into table.
assign (table) to .
endif.

* Set the list layout options
ln_layoutinfo-expand_all = 'X'.
ln_layoutinfo-colwidth_optimize = p_optimize_cols.
ln_layoutinfo-no_min_linesize = space.
ln_layoutinfo-zebra = 'X'.
ln_layoutinfo-detail_popup = 'X'.
ln_layoutinfo-detail_initial_lines = 'X'.
ln_layoutinfo-get_selinfos = pselinfo.
ln_layoutinfo-totals_text = 'Grandtotal'.
ln_layoutinfo-subtotals_text = 'Sub-total'.
*

*amit
* Add the top-of-page event
if not ( topofpageform is initial ).
* perform add_event using slis_ev_top_of_page topofpageform.
endif.

* Call the List Viewer
if ls_type = 'L'.
call function 'REUSE_ALV_LIST_DISPLAY'
exporting
i_callback_program = ls_repid
i_callback_pf_status_set = pfstatform
i_callback_user_command = ucommform
is_layout = ln_layoutinfo
it_fieldcat = tab_fieldcat
it_sort = tab_sortinfo
i_save = p_save
is_variant = gs_variant
it_events = tab_events
tables
t_outtab =
exceptions
program_error = 1
others = 2
.
if sy-subrc <> 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.
elseif ls_type = 'G'.
ln_gridinfo-coll_top_p = 'X'.
ln_gridinfo-top_p_only = 'X'.

read table tab_fieldcat with key fieldname = 'CHECKBOX'
transporting no fields.
if sy-subrc eq 0.
ln_layoutinfo-box_fieldname = 'CHECKBOX'.
ln_layoutinfo-box_tabname = tabname_header.
endif.
call function 'REUSE_ALV_GRID_DISPLAY'
exporting
i_callback_program = ls_repid
* i_callback_pf_status_set = pfstatform
* i_callback_user_command = ucommform
** i_callback_top_of_page = topofpageform
i_callback_top_of_page = 'TOP-OF-PAGE'
* i_grid_settings = ln_gridinfo
is_layout = ln_layoutinfo
it_fieldcat = tab_fieldcat
it_sort = tab_sortinfo
i_save = p_save
* i_save = 'X'
is_variant = gs_variant
it_events = tab_events
tables
t_outtab =
exceptions
program_error = 1
others = 2
.
if sy-subrc <> 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.
elseif ls_type = 'S'.
call function 'REUSE_ALV_HIERSEQ_LIST_DISPLAY'
exporting
i_callback_program = ls_repid
i_callback_pf_status_set = pfstatform
i_callback_user_command = ucommform
is_layout = ln_layoutinfo
it_fieldcat = tab_fieldcat
it_sort = tab_sortinfo
i_save = p_save
is_variant = gs_variant
it_events = tab_events
i_tabname_header = p_datatab_head
i_tabname_item = p_datatab_item
is_keyinfo = ln_keyinfo
tables
t_outtab_header =
t_outtab_item =
exceptions
program_error = 1
others = 2
.
if sy-subrc <> 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.

endif.

endform.
*---------------------------------------------------------------------*
* FORM list_header *
*---------------------------------------------------------------------*
* Adds a commentary to the top of the list in Grid version *
*---------------------------------------------------------------------*
* --> P_TITLE1 1st line in the list header *
* --> P_TITLE2 2nd line in the list header *
* --> P_TITLE3 3rd line in the list header *
* --> P_TITLE4 4th line in the list header *
*---------------------------------------------------------------------*
*form list_header using p_title1 p_title2 p_title3 p_title4.
* clear: ln_listheader, tab_listheader.
* ln_listheader-typ = 'S'.
* if p_title1 ne space.
* ln_listheader-info = p_title1.
* append ln_listheader to tab_listheader.
* endif.
* if p_title2 ne space.
* ln_listheader-info = p_title2.
* append ln_listheader to tab_listheader.
* endif.
* if p_title3 ne space.
* ln_listheader-info = p_title3.
* append ln_listheader to tab_listheader.
* endif.
* if p_title4 ne space.
* ln_listheader-info = p_title4.
* append ln_listheader to tab_listheader.
* endif.
*
* call function 'REUSE_ALV_COMMENTARY_WRITE'
* exporting
* it_list_commentary = tab_listheader.
*endform.

*&---------------------------------------------------------------------*
*&amp; Form HIDE_FIELD
*&---------------------------------------------------------------------*
* Causes a field not to be displayed on the initial list
*----------------------------------------------------------------------*
* --> FLDNAME Fieldname of a fieldcatalog entry *
* --> TECH Technical field - not output/not selectable *
*----------------------------------------------------------------------*
form hide_field using fldname like ln_fieldcat-fieldname
tech like ln_fieldcat-tech.
clear ln_fieldcat.
ln_fieldcat-fieldname = fldname.
ln_fieldcat-no_out = 'X'.
ln_fieldcat-tech = tech.
read table tab_fieldcat with key
fieldname = ln_fieldcat-fieldname
transporting no fields.
if sy-subrc eq 0.
modify tab_fieldcat from ln_fieldcat index sy-tabix
transporting no_out tech.
endif.


endform. " HIDE_FIELD

No comments: