Posted: Sat Mar 22, 2008 10:00 pm Post subject: Building dynamic columns report (ALV)
Code:
TYPE-POOLS: abap,
col.
*&---------------------------------------------------------------------*
*& Dynamic Table
*&---------------------------------------------------------------------*
FIELD-SYMBOLS: <dyn_table> TYPE STANDARD TABLE,
<dyn_wa>,
<fs>.
DATA: gt_dyn TYPE REF TO data,
gw_dyn TYPE REF TO data,
r_table TYPE REF TO cl_salv_table,
r_header TYPE REF TO cl_salv_form_element,
r_footer TYPE REF TO cl_salv_form_element,
r_columns_table TYPE REF TO cl_salv_columns_table,
r_column_table TYPE REF TO cl_salv_column_table,
r_datadescr TYPE REF TO cl_abap_datadescr,
r_structdescr TYPE REF TO cl_abap_structdescr,
gw_component TYPE abap_componentdescr,
gt_component TYPE abap_component_tab.
*&---------------------------------------------------------------------*
*& Form build_dynamic_table
*&---------------------------------------------------------------------*
FORM build_dynamic_table .
DATA: l_idx TYPE c,
l_desc TYPE char50,
l_hslxx TYPE p LENGTH 5 DECIMALS 2,
lt_color TYPE lvc_t_scol.
* Column 1
r_datadescr ?= cl_abap_datadescr=>describe_by_data( l_desc ).
gw_component-name = 'COLUMN'.
gw_component-type = r_datadescr.
APPEND gw_component TO gt_component.
* Column 2 - Types of color for each line
* Do not display this column
r_datadescr ?= cl_abap_datadescr=>describe_by_data( lt_color ).
gw_component-name = 'COLOR'.
gw_component-type = r_datadescr.
APPEND gw_component TO gt_component.
DO 5 TIMES.
l_idx = sy-index.
r_datadescr ?= cl_abap_datadescr=>describe_by_data( l_hslxx ).
CONCATENATE 'COL' l_idx INTO gw_component-name.
gw_component-type = r_datadescr.
APPEND gw_component TO gt_component.
ENDDO.
TRY.
r_structdescr = cl_abap_structdescr=>create( p_components = gt_component ).
CATCH cx_sy_struct_creation .
WRITE: / 'CX_SY_STRUCT_CREATION ERROR'.
ENDTRY.
* Fill the table with data from GT_DATA
CREATE DATA gw_dyn TYPE HANDLE r_structdescr.
ASSIGN gw_dyn->* TO <dyn_wa>.
CREATE DATA gt_dyn LIKE STANDARD TABLE OF <dyn_wa>.
ASSIGN gt_dyn->* TO <dyn_table>.
ENDFORM. " build_dynamic_table
*&---------------------------------------------------------------------*
*& Form get_data
*&---------------------------------------------------------------------*
FORM get_data .
* select statement
ENDFORM. " get_data
*&---------------------------------------------------------------------*
*& Form display_report
*&---------------------------------------------------------------------*
FORM display_report .
PERFORM display_header. " Display ALV Header
PERFORM display_footer. " DIsplay ALV Footer
PERFORM fill_data. " Fill data
PERFORM set_color. " Set color
PERFORM display_list. " Display the ALV
ENDFORM. " display_report
*&---------------------------------------------------------------------*
*& Form display_header
*&---------------------------------------------------------------------*
FORM display_header .
DATA: lr_grid TYPE REF TO cl_salv_form_layout_grid.
CREATE OBJECT lr_grid.
lr_grid->create_text(
row = 1
column = 1
text = 'Header Line 1' ).
lr_grid->create_text(
row = 2
column = 1
text = 'Header Line 2' ).
r_header = lr_grid.
ENDFORM. " display_header
*&---------------------------------------------------------------------*
*& Form display_footer
*&---------------------------------------------------------------------*
FORM display_footer .
DATA: lr_grid TYPE REF TO cl_salv_form_layout_grid.
CREATE OBJECT lr_grid.
lr_grid->create_text(
row = 1
column = 1
text = text-266 ).
lr_grid->create_text(
row = 2
column = 1
text = text-267 ).
r_footer = lr_grid.
ENDFORM. " display_footer
*&---------------------------------------------------------------------*
*& Form set_color
*&---------------------------------------------------------------------*
FORM set_color.
DATA: lt_color TYPE lvc_t_scol,
ls_color LIKE LINE OF lt_color,
l_idx TYPE sy-tabix.
FIELD-SYMBOLS: <fs_color>.
LOOP AT <dyn_table> INTO <dyn_wa>.
l_idx = sy-tabix.
ASSIGN COMPONENT 'COLOR' OF STRUCTURE <dyn_wa> TO <fs_color>.
ls_color-color-col = col_total.
* ls_color-color-col = col_normal.
* ls_color-color-col = col_group.
APPEND ls_color TO lt_color.
<fs_color> = lt_color.
MODIFY <dyn_table> FROM <dyn_wa> INDEX l_idx.
UNASSIGN: <fs_color>.
ENDLOOP.
ENDFORM. " set_color
*&---------------------------------------------------------------------*
*& Form display_list
*&---------------------------------------------------------------------*
FORM display_list.
DATA: r_display TYPE REF TO cl_salv_display_settings.
* Prepare the internal table for display
cl_salv_table=>factory(
EXPORTING
list_display = 'X'
IMPORTING
r_salv_table = r_table
CHANGING
t_table = <dyn_table> ).
* Set report page title
r_table->set_top_of_list( r_header ).
* Set report footer
r_table->set_end_of_list( r_footer ).
r_display = r_table->get_display_settings( ).
* r_display->set_vertical_lines( ' ' ).
* Assign all the column names
PERFORM set_column_attr.
* Display the report
r_table->display( ).
ENDFORM. " display_list
*&---------------------------------------------------------------------*
*& Form set_column_attr
*&---------------------------------------------------------------------*
FORM set_column_attr.
DATA: l_idx TYPE c,
l_text(4) TYPE c,
colname TYPE lvc_fname,
outps TYPE scrtext_s,
outpm TYPE scrtext_m,
outpl TYPE scrtext_l.
r_columns_table = r_table->get_columns( ).
* r_columns_table->set_headers_visible( abap_false ).
* column 2
* set color column
r_columns_table->set_color_column( 'COLOR' ).
DO 5 TIMES.
l_idx = sy-index.
CONCATENATE 'COL' l_idx INTO colname.
outps = colname.
outpm = colname.
outpl = colname.
r_column_table ?= r_columns_table->get_column( colname ).
r_column_table->set_optimized( value = abap_true ).
r_column_table->set_alignment( value = 1 ).
r_column_table->set_zero( value = space ).
r_column_table->set_short_text( outps ).
r_column_table->set_medium_text( outpm ).
r_column_table->set_long_text( outpl ).
ENDDO.
ENDFORM. "set_column_attr
*&---------------------------------------------------------------------*
*& Form fill_data
*&---------------------------------------------------------------------*
FORM fill_data .
DATA: l_idx TYPE c,
l_col TYPE string.
DO 5 TIMES.
l_idx = sy-index.
CONCATENATE 'COL' l_idx INTO l_col.
ASSIGN COMPONENT l_col OF STRUCTURE <dyn_wa> TO <fs>.
<fs> = 0.
UNASSIGN <fs>.
ENDDO.
DO 5 TIMES.
l_idx = sy-index.
ASSIGN COMPONENT 'COLUMN' OF STRUCTURE <dyn_wa> TO <fs>.
CONCATENATE 'Row' l_idx INTO <fs> SEPARATED BY space.
UNASSIGN <fs>.
APPEND <dyn_wa> TO <dyn_table>.
ENDDO.
ENDFORM. " fill_data
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG. SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG. Every effort is made to ensure content integrity. Use information on this site at your own risk.