0% found this document useful (0 votes)
173 views16 pages

ABAP OO ALV Sample Program To Display Table Structure

This document contains ABAP code that defines data structures and variables, performs data retrieval and processing, and generates an ALV output table displaying metadata about a database table. Key points: 1) It defines data structures like W_ALVTAB to store metadata, retrieves metadata using CL_ABAP_STRUCTDESCR, and populates the structures. 2) It sets properties of the generated ALV grid like column formatting, layout handling, and display settings. 3) The code processes the metadata, sets colors for certain fields, and calls CL_SALV_TABLE to generate the final ALV output displaying the metadata.

Uploaded by

German Martinez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
173 views16 pages

ABAP OO ALV Sample Program To Display Table Structure

This document contains ABAP code that defines data structures and variables, performs data retrieval and processing, and generates an ALV output table displaying metadata about a database table. Key points: 1) It defines data structures like W_ALVTAB to store metadata, retrieves metadata using CL_ABAP_STRUCTDESCR, and populates the structures. 2) It sets properties of the generated ALV grid like column formatting, layout handling, and display settings. 3) The code processes the metadata, sets colors for certain fields, and calls CL_SALV_TABLE to generate the final ALV output displaying the metadata.

Uploaded by

German Martinez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

REPORT ZCA_BOB_CL_ABAP_STRUCTDESCR.

PARAMETERS P_TABNAM type TABNAME OBLIGATORY.

Data: wa_spfli type spfli,


r_descr type REF TO cl_abap_structdescr,
wa_comp TYPE abap_compdescr.

** Create references to the needed ALV Global Classes


data: lr_events type ref to cl_salv_events_table.
data: gr_table type ref to cl_salv_table.
Data: r_grid TYPE REF TO cl_salv_table.
data: r_title_text TYPE REF TO cl_alv_variant,
r_grid_title TYPE LVC_TITLE.

Data: abap_compdescr_tab TYPE STANDARD TABLE OF abap_compdescr


WITH KEY name.
**
types: begin of w_alvtab.
include type abap_compdescr.
*types: exception type char1,
* checkbox type sap_bool,
* icon type icon_d,
* symbol type icon_d,
* hotspot type icon_d,
* button type icon_d,
types: t_color type lvc_t_scol,
* t_celltype type salv_t_int4_column,
* t_hyperlink type salv_t_int4_column,
* t_dropdown type salv_t_int4_column,
end of w_alvtab.

data: it_alvtab type standard table of w_alvtab.


data: wa_alvtab like LINE OF it_alvtab.

Data: wk_length(6) type n,


wk_decimals(6) type n.

types: begin of wa_int_tab,


mandt type mandt,
matnr type matnr,
werks type werks,
lgort type lgort,
end of wa_int_tab.
data: it_int_tab TYPE STANDARD TABLE OF wa_int_tab.

selection-screen begin of block lay with frame.


parameters:
p_lay01 radiobutton group lay, "Save All
p_lay02 radiobutton group lay, "Save User Specific
p_lay03 radiobutton group lay. "Save Across all users

selection-screen skip.

parameters:
p_lay04 as checkbox. "DEFAULT Layouts

selection-screen skip.

parameters:
p_lay05 as checkbox default 'X', "Start with Layout
p_lay06 like disvariant-variant. "LAYOUT
selection-screen end of block lay.

types: begin of g_type_s_test,


amount type i,
repid type syrepid,
display type i,
restrict type salv_de_layout_restriction,
default type sap_bool,
layout type disvariant-variant,
load_layo type sap_bool,
end of g_type_s_test.

constants: gc_true type sap_bool value 'X',

begin of gc_s_display,
list type i value 1,
fullscreen type i value 2,
grid type i value 3,
end of gc_s_display.

*class lcl_handle_events definition deferred.


data: gs_test type g_type_s_test.

DATA: BEGIN OF IT_DAY_ATTR OCCURS 0.


include structure casdayattr.
DATA: END OF IT_DAY_ATTR.

DATA: lt_color type lvc_t_scol,


ls_color type lvc_s_scol.

include <color>.
include <icon>.
include <symbol>.

*----------------------------------------------------------------------*
* AT SELECTION-SCREEN ON VALUE-REQUEST *
*----------------------------------------------------------------------*
at selection-screen on value-request for p_lay06.
perform f4_layouts using gs_test-restrict changing p_lay06.

*----------------------------------------------------------------------*
* START-OF-SELECTION *
*----------------------------------------------------------------------*
start-of-selection.

**--------------------------------------------------------------------**
** B E G I N
** this section of code deals with the handling of the layouts
**--------------------------------------------------------------------**

* gs_test-amount = p_amount.
gs_test-repid = sy-repid.

case 'X'.
when p_lay01. "SAVE All
*... §4.3 set Layout save restriction (3) no restriction
gs_test-restrict = if_salv_c_layout=>restrict_none.
when p_lay02. "SAVE USER-SPECIFIC
*... §4.3 set Layout save restriction (1) restrict to user dependant
gs_test-restrict = if_salv_c_layout=>restrict_user_dependant.
when p_lay03. "SAVE ACROSS USERS
*... §4.3 set Layout save restriction (2) restrict to user independant
gs_test-restrict = if_salv_c_layout=>restrict_user_independant.
endcase.

gs_test-default = p_lay04. "Allow saving of Default Layouts

gs_test-load_layo = p_lay05. "Start with Layout


gs_test-layout = p_lay06. "Start Layout name

**--------------------------------------------------------------------**
** E N D
** this section of code deals with the handling of the layouts
**--------------------------------------------------------------------**

** ?= means cast
** r_descr ?= cl_abap_typedescr=>describe_by_data( wa_spfli ).
r_descr ?= cl_abap_typedescr=>describe_by_name( P_TABNAM ).
Loop at r_descr->components into wa_comp.

* write:/ wa_comp-name, wa_comp-type_kind, wa_comp-length,


* wa_comp-decimals.
If wa_comp-type_kind = 'C'
or wa_comp-type_kind = 'D'
or wa_comp-type_kind = 'N'.
Divide wa_comp-length by 2.
Else.
** go out to table DD03L to get LENG and DECIMALS
SELECT single LENG DECIMALS from DD03L
into (wk_length, wk_decimals)
where tabname = p_tabnam
and fieldname = wa_comp-name.
wa_comp-length = wk_length.
wa_comp-decimals = wk_decimals.
Endif.
append wa_comp to abap_compdescr_tab.
MOVE-CORRESPONDING wa_comp to wa_alvtab.
append wa_alvtab to it_alvtab.

EndLoop.

field-symbols: <ls_alvtab> type w_alvtab.

loop at it_alvtab assigning <ls_alvtab>.


if <ls_alvtab>-TYPE_KIND = 'P'.
clear lt_color.
clear ls_color.
*
ls_color-fname = 'TYPE_KIND'.
ls_color-color-col = col_negative.
ls_color-color-int = 0.
ls_color-color-inv = 0.
append ls_color to lt_color.

<ls_alvtab>-t_color = lt_color.

endif.
endloop.

** Call the method to create the ALV output


TRY.
CALL METHOD CL_SALV_TABLE=>FACTORY
EXPORTING
LIST_DISPLAY = IF_SALV_C_BOOL_SAP=>FALSE
* R_CONTAINER =
* CONTAINER_NAME =
IMPORTING
R_SALV_TABLE = r_grid
CHANGING
T_TABLE = it_alvtab. "abap_compdescr_tab.

CATCH CX_SALV_MSG .
ENDTRY.

*... §3 Functions
*... §3.1 activate ALV generic Functions
data: lr_functions type ref to cl_salv_functions_list.

lr_functions = r_grid->get_functions( ).
lr_functions->set_default( abap_true ).

*... set the columns technical


data: lr_columns type ref to cl_salv_columns.

lr_columns = r_grid->get_columns( ).
lr_columns->set_optimize( abap_true ).

perform set_columns_technical using lr_columns.


PERFORM set_columns_color using lr_columns.

**--------------------------------------------------------------------**
** B E G I N
** this code deals with the handling of column headings
** it will overwrite the data element specified in the data dictionary
**--------------------------------------------------------------------**
data: l_column TYPE REF TO CL_SALV_COLUMN.

l_column ?= lr_columns->get_column( 'LENGTH' ).


l_column->set_long_text( 'Length' ).
l_column->set_medium_text( 'LENGTH' ).
l_column->set_short_text( 'LEN' ).

l_column ?= lr_columns->get_column( 'NAME' ).


l_column->set_long_text( 'Field Name' ).
l_column->set_medium_text( 'Field Nm' ).
l_column->set_short_text( 'Field' ).

l_column ?= lr_columns->get_column( 'TYPE_KIND' ).


l_column->set_long_text( 'Type' ).
l_column->set_medium_text( 'Type' ).
l_column->set_short_text( 'Type' ).

l_column ?= lr_columns->get_column( 'DECIMALS' ).


l_column->set_long_text( 'Decimals' ).
l_column->set_medium_text( 'DECIMALS' ).
l_column->set_short_text( 'DEC' ).

**--------------------------------------------------------------------**
** E N D
** this code deals with the handling of column headings
** it will overwrite the data element specified in the data dictionary
**--------------------------------------------------------------------**
**

perform create_header_and_footer .

** code needed for Event Handling


*lr_events = r_grid->get_event( ).
* create object gr_events.
* set handler gr_events->on_double_click for lr_events.
*
*&---------------------------------------------------------------------*
** B E G I N
*& this code deals with Layout processing
*&---------------------------------------------------------------------*

*... §4 set layout


data: lr_layout type ref to cl_salv_layout,
ls_key type salv_s_layout_key.

lr_layout = r_grid->get_layout( ).

*... §4.1 set the Layout Key


ls_key-report = gs_test-repid.
lr_layout->set_key( ls_key ).

*... §4.2 set usage of default Layouts


lr_layout->set_default( gs_test-default ).

*... §4.3 set Layout save restriction


lr_layout->set_save_restriction( gs_test-restrict ).

*... §4.4 set initial Layout


if gs_test-load_layo eq gc_true.
lr_layout->set_initial_layout( gs_test-layout ).
endif.

*&---------------------------------------------------------------------*
** E N D
*& this code deals with Layout processing
*&---------------------------------------------------------------------*
**
**
**--------------------------------------------------------------------**
** B E G I N
** this code deals with the handling of display of lines
**--------------------------------------------------------------------**
* ref to the class
data: lr_display_settings type ref to cl_salv_display_settings,
l_title type lvc_title,
STRIPED_PATTERN type SAP_BOOL value 'X',"space = false x = true
VERTICAL_LINES type SAP_BOOL value 'X',
HORIZONTAL_LINES type SAP_BOOL value 'X'.
l_title = 'Display of Table Structure'.

** create the instance


lr_display_settings = r_grid->get_display_settings( ).

** call the methods


lr_display_settings->SET_STRIPED_PATTERN( STRIPED_PATTERN ).
lr_display_settings->SET_VERTICAL_LINES( VERTICAL_LINES ).
lr_display_settings->SET_HORIZONTAL_LINES( HORIZONTAL_LINES ).
**--------------------------------------------------------------------**
** E N D
** this code deals with the handling of display of lines
**--------------------------------------------------------------------**

r_grid->display( ).

*----------------------------------------------------------------------*
* INITIALIZATION *
*----------------------------------------------------------------------*
initialization.
perform get_default_layout using gs_test-restrict changing p_lay06.

*&---------------------------------------------------------------------*
*& Form set_columns_technical
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
form set_columns_technical using ir_columns type ref to cl_salv_columns.

data: lr_column type ref to cl_salv_column.

try.
lr_column = ir_columns->get_column( 'MANDT' ).
lr_column->set_technical( if_salv_c_bool_sap=>true ).
catch cx_salv_not_found. "#EC NO_HANDLER
endtry.

try.
lr_column = ir_columns->get_column( 'FLOAT_FI' ).
lr_column->set_technical( if_salv_c_bool_sap=>true ).
catch cx_salv_not_found. "#EC NO_HANDLER
endtry.

try.
lr_column = ir_columns->get_column( 'STRING_F' ).
lr_column->set_technical( if_salv_c_bool_sap=>true ).
catch cx_salv_not_found. "#EC NO_HANDLER
endtry.

try.
lr_column = ir_columns->get_column( 'XSTRING' ).
lr_column->set_technical( if_salv_c_bool_sap=>true ).
catch cx_salv_not_found. "#EC NO_HANDLER
endtry.

try.
lr_column = ir_columns->get_column( 'INT_FIEL' ).
lr_column->set_technical( if_salv_c_bool_sap=>true ).
catch cx_salv_not_found. "#EC NO_HANDLER
endtry.

try.
lr_column = ir_columns->get_column( 'HEX_FIEL' ).
lr_column->set_technical( if_salv_c_bool_sap=>true ).
catch cx_salv_not_found. "#EC NO_HANDLER
endtry.

try.
lr_column = ir_columns->get_column( 'DROPDOWN' ).
lr_column->set_technical( if_salv_c_bool_sap=>true ).
catch cx_salv_not_found. "#EC NO_HANDLER
endtry.

try.
lr_column = ir_columns->get_column( 'TAB_INDEX' ).
lr_column->set_technical( if_salv_c_bool_sap=>true ).
catch cx_salv_not_found. "#EC NO_HANDLER
endtry.

endform. " set_columns_technical(


**

*&---------------------------------------------------------------------*
*& Form set_columns_color
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
form set_columns_color using ir_columns type ref to cl_salv_columns.

data: lr_column type ref to cl_salv_column_table.

*... §5 set color


*... §5.1 set the color of a complete column
data: ls_color type lvc_s_colo.
*
* try.
** l_column ?= lr_columns->get_column( 'LENGTH' ).
* lr_column ?= ir_columns->get_column( 'LENGTH' ).
** lr_column ?= lr_columns->get_column( 'LENGTH' ).
* ls_color-col = col_background.
* ls_color-int = 0.
* ls_color-inv = 0.
* lr_column->set_color( ls_color ).
* catch cx_salv_not_found.
* endtry.
*
* try.
* lr_column ?= ir_columns->get_column( 'DECIMALS' ).
* ls_color-col = col_key.
* ls_color-int = 0.
* ls_color-inv = 0.
* lr_column->set_color( ls_color ).
* catch cx_salv_not_found.
* endtry.

try.
lr_column ?= ir_columns->get_column( 'NAME' ).
ls_color-col = col_heading.
ls_color-int = 0.
ls_color-inv = 0.
lr_column->set_color( ls_color ).
catch cx_salv_not_found.
endtry.
*
*
**... §5.2 set the color of a complete row
** (1) register the column in which the color information
** for the row is held
***
data: lr_columns type ref to cl_salv_columns_table.
types: t_color type lvc_t_scol.
**
lr_columns = r_grid->get_columns( ).
**
try.
lr_columns->set_color_column( 'T_COLOR' ).
catch cx_salv_data_error. "#EC NO_HANDLER
endtry.

endform. " set_columns_color


**
**
*&---------------------------------------------------------------------*
** FORM CREATE_HEADER_AND_FOOTER
*&---------------------------------------------------------------------*
**
form create_header_and_footer .
data: lr_top_element type ref to cl_salv_form_layout_grid,
lr_end_element type ref to cl_salv_form_layout_flow,
lr_grid type ref to cl_salv_form_layout_grid,
lr_header type ref to cl_salv_form_header_info,
lr_action type ref to cl_salv_form_action_info,

lr_textview1 type ref to cl_salv_form_text,


lr_textview2 type ref to cl_salv_form_text,
lr_textview3 type ref to cl_salv_form_text,
lr_textview4 type ref to cl_salv_form_text,
lr_textview5 type ref to cl_salv_form_text,

lr_icon type ref to cl_salv_form_icon,

lr_layout_grid1 type ref to cl_salv_form_layout_data_grid,


lr_layout_grid2 type ref to cl_salv_form_layout_data_grid,
lr_layout_grid3 type ref to cl_salv_form_layout_data_grid,
lr_layout_grid4 type ref to cl_salv_form_layout_data_grid,
lr_layout_grid5 type ref to cl_salv_form_layout_data_grid.

Data: wk_hdg_line_1(50).
Data: wk_hdg_line_2(50).

** instantiate lr_top_element ( type ref to CL_SALV_FORM_LAYOUT_GRID )


create object lr_top_element
EXPORTING
COLUMNS = 5.

CONCATENATE: 'Field List and descriptions for Table' P_TABNAM


into wk_hdg_line_1 separated by space.

lr_header = lr_top_element->create_header_information(
row = 1
column = 1
text = wk_hdg_line_1
tooltip = 'space for tooltip' ).

CONCATENATE: 'Program Id:' sy-repid


into wk_hdg_line_2 separated by space.

lr_action = lr_top_element->create_action_information(
row = 2
column = 1
text = wk_hdg_line_2
tooltip = 'tooltip description - if any' ).

CALL FUNCTION 'DAY_ATTRIBUTES_GET'


EXPORTING
* FACTORY_CALENDAR =''
* HOLIDAY_CALENDAR =''
DATE_FROM = SY-DATUM
DATE_TO = SY-DATUM
LANGUAGE = SY-LANGU
* NON_ISO =''
* IMPORTING
* YEAR_OF_VALID_FROM =
* YEAR_OF_VALID_TO =
* RETURNCODE =
TABLES
DAY_ATTRIBUTES = IT_DAY_ATTR.

lr_action = lr_top_element->create_action_information(
row = 2
column = 5
text = IT_DAY_ATTR-DAY_STRING ).
Data: wk_text_1(30),
wk_text_2(30),
wk_text_3(30),
wk_text_4(30),
wk_text_5(30),
wk_output_date(10),
wk_output_time(10).

lr_grid = lr_top_element->create_grid( row = 3


column = 1 ).

** ROW 1
write sy-datlo to wk_output_date.
CONCATENATE 'Date:' wk_output_date into wk_text_1 SEPARATED BY space.
lr_textview1 = lr_grid->create_text(
row = 1
column = 1
text = wk_text_1
tooltip = 'Tooltip' ).

** ROW 2
WRITE sy-timlo to wk_output_time.
CONCATENATE 'Time:' wk_output_time into wk_text_1 SEPARATED BY space.
lr_textview2 = lr_grid->create_text(
row = 2
column = 1
text = wk_text_1
tooltip = 'Tooltip' ).

wk_text_2 = 'lr_layout_grid3'.
lr_textview3 = lr_grid->create_text(
row = 2
column = 2
text = wk_text_2
tooltip = 'Tooltip' ).

wk_text_3 = 'this is where col 3 is'.


lr_textview3 = lr_grid->create_text(
row = 2
column = 3
text = wk_text_3
tooltip = 'Tooltip' ).

wk_text_4 = 'this is column4'.


lr_textview4 = lr_grid->create_text(
row = 2
column = 4
text = wk_text_4
tooltip = 'Tooltip' ).

wk_text_5 = 'this is where col 5 is'.


lr_textview5 = lr_grid->create_text(
row = 2
column = 5
text = wk_text_5
tooltip = 'Tooltip' ).

lr_layout_grid1 ?= lr_textview1->get_layout_data( ).
lr_layout_grid2 ?= lr_textview2->get_layout_data( ).
lr_layout_grid3 ?= lr_textview3->get_layout_data( ).
lr_layout_grid4 ?= lr_textview4->get_layout_data( ).
lr_layout_grid5 ?= lr_textview5->get_layout_data( ).

lr_layout_grid1->set_h_align( if_salv_form_c_h_align=>left ).
lr_layout_grid2->set_h_align( if_salv_form_c_h_align=>left ).
lr_layout_grid3->set_h_align( if_salv_form_c_h_align=>left ).
* lr_layout_grid4->set_h_align( if_salv_form_c_h_align=>center ).

create object lr_icon


exporting
icon = ICON_DATABASE_TABLE
tooltip = 'Icon for Database Table'.
*
call method lr_grid->set_element
exporting
row =3
column = 1
r_element = lr_icon.

create object lr_icon


exporting
icon = ICON_SIMPLE_FIELD "ICON_DATABASE_TABLE
tooltip = 'Icon for Simple Field'.

call method lr_grid->set_element


exporting
row =3
column = 2
r_element = lr_icon.

** call method to output the header


r_grid->set_top_of_list( lr_top_element ).

data: lr_eol type ref to cl_salv_form_header_info.


create object lr_eol
exporting
text = 'This is my Footer'. "#EC NOTEXT

**&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&**
**&&&& check class CL_SALV_FORM_UIE_IMAGE &&&&&**
**&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&**

r_grid->set_end_of_list( lr_eol ).

endform. " create_header_and_footer

*&---------------------------------------------------------------------*
*& Form f4_layouts
*&---------------------------------------------------------------------*
* §4.5 F4 Layouts
* cl_salv_layout provides a method for handling the f4 help of the
* layouts for the specified layout key. It is also possible to use
* the static class cl_salv_layout_service.
*----------------------------------------------------------------------*
form f4_layouts using i_restrict type salv_de_layout_restriction
changing c_layout type disvariant-variant.

data: ls_layout type salv_s_layout_info,


ls_key type salv_s_layout_key.

ls_key-report = sy-repid.

ls_layout = cl_salv_layout_service=>f4_layouts(
s_key = ls_key
restrict = i_restrict ).

c_layout = ls_layout-layout.

endform. " f4_layouts

*&---------------------------------------------------------------------*
*& Form get_default_layout
*&---------------------------------------------------------------------*
* §4.4 Get Default Layout
* cl_salv_layout provides a method for retieving the valid default
* layout. It is also possible to use the static class
* cl_salv_layout_service. The rules for determinig the valid
* default Layout is as follows:
* (1) check if user-dependant default layout exists if yes then
* this layout is loaded
* (2) check if user-independant standard layout exists if yes then
* this layout is loaded
* (3) check if user-independant SAP standard layout exists if yes
* then this layout is loaded
*----------------------------------------------------------------------*
form get_default_layout using i_restrict type salv_de_layout_restriction
changing c_layout type disvariant-variant.

data: ls_layout type salv_s_layout_info,


ls_key type salv_s_layout_key.

ls_key-report = sy-repid.

ls_layout = cl_salv_layout_service=>get_default_layout(
s_key = ls_key
restrict = i_restrict ).

c_layout = ls_layout-layout.

endform. " get_default_layout

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy