SALV_TO_DD

19
SALV List to DD (Dynamic Documents) List

Transcript of SALV_TO_DD

Page 1: SALV_TO_DD

SALV List to DD (Dynamic Documents) List

Page 2: SALV_TO_DD

Screen Painter:

PROCESS BEFORE OUTPUT. MODULE status_0100. PROCESS AFTER INPUT. MODULE user_command_0100.

Page 3: SALV_TO_DD

Menu Painter: SALV_STANDARD

Page 4: SALV_TO_DD

INFO

Program ZZZ_SALV_TO_DD Selection-Screen:

Source Code: *&---------------------------------------------------------------------* *& Report ZZZ_SALV_TO_DD *& *&---------------------------------------------------------------------* report zzz_salv_to_dd no standard page heading. * TOP types: begin of g_type_s_opt, amount type i, repid type syrepid, display type i, dynamic type sap_bool, end of g_type_s_opt. data: gs_opt type g_type_s_opt. constants: 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. constants: gc_inttab type tabname value 'GT_OUTTAB'.

Page 5: SALV_TO_DD

data: begin of gs_outtab, carrid type alv_t_t2-carrid, connid type alv_t_t2-connid, fldate type alv_t_t2-fldate, carrname type alv_t_t2-carrname, carrid_conv type alv_t_t2-carrid_conv, price type alv_t_t2-price, currency type alv_t_t2-currency, planetype type alv_t_t2-planetype, seatsmax type alv_t_t2-seatsmax, seatsocc type alv_t_t2-seatsocc, paymentsum type alv_t_t2-paymentsum, countryfr type alv_t_t2-countryfr, airpfrom type alv_t_t2-airpfrom, countryto type alv_t_t2-countryto, airpto type alv_t_t2-airpto, fltime type alv_t_t2-fltime, deptime type alv_t_t2-deptime, arrtime type alv_t_t2-arrtime, end of gs_outtab, gt_outtab like standard table of gs_outtab. data: gr_table type ref to cl_salv_table. data: gr_container type ref to cl_gui_custom_container. data: gt_fcat_dd type lvc_t_fcat. data: gr_columns type ref to cl_salv_columns. data: g_tabname like dfies-tabname, g_fieldname like lvc_s_fcat-fieldname. data: gv_view(1) type c. constants: k_view_list(1) type c value 'L', k_view_form(1) type c value 'F'. data: gt_fcat type lvc_t_fcat, gs_fcat type lvc_s_fcat. class cl_gui_cfw definition load. types: begin of sedi_exclude, function type sy-ucomm, end of sedi_exclude, sedi_exclude_tab type sedi_exclude occurs 0. data: exclude_functions type sedi_exclude_tab with header line. *ok code. data: save_ok type sy-ucomm, ok_code type sy-ucomm. * application data data: g_ok_code like sy-ucomm, g_okcode like sy-ucomm. data: return_code type i. include <icon>. constants: gc_true type sap_bool value 'X'. data: g_repid type syrepid.

Page 6: SALV_TO_DD

*----------------------------------------------------------------------* * DD * *----------------------------------------------------------------------* * data declaration data: g_dylang like d020s-spra, g_dyname like d020s-prog, g_dynumb like d020s-dnum. data: gt_fields like dfies occurs 0 with header line. field-symbols: <fs1>, <fsf>, <fsc>. data: gv_cols type i. constants: gc_max_cols type i value 21. data: gv_index type i, gv_indx type i. data: gv_num(2) type n. data: flag(1) type c. * application data data: g_custom_container type ref to cl_gui_custom_container. data: first_display value 'X'. constants: sflg2_vresize(2) type x value '20', sflg2_hresize(2) type x value '10', sflg2_scrollable(2) type x value 'C0'. types: begin of area, name type d021s-fnam, line type i, column type i, height type i, width type i, minh type i, minw type i, dynh type i, dynw type i, end of area, area_table type standard table of area. data: g_document type ref to cl_dd_document. data col01 type ref to cl_dd_area. data col02 type ref to cl_dd_area. data col03 type ref to cl_dd_area. data col04 type ref to cl_dd_area. data col05 type ref to cl_dd_area. data col06 type ref to cl_dd_area. data col07 type ref to cl_dd_area. data col08 type ref to cl_dd_area. data col09 type ref to cl_dd_area. data col10 type ref to cl_dd_area. data col11 type ref to cl_dd_area. data col12 type ref to cl_dd_area. data col13 type ref to cl_dd_area. data col14 type ref to cl_dd_area. data col15 type ref to cl_dd_area. data col16 type ref to cl_dd_area. data col17 type ref to cl_dd_area. data col18 type ref to cl_dd_area.

Page 7: SALV_TO_DD

data col19 type ref to cl_dd_area. data col20 type ref to cl_dd_area. data col21 type ref to cl_dd_area. data: d1t type ref to cl_dd_table_element, d1ta type ref to cl_dd_table_area, d2 type ref to cl_dd_document, d2t type ref to cl_dd_table_element, d2ta type ref to cl_dd_table_area. data: ta1 type ref to cl_dd_table_element, tabt type ref to cl_dd_table_element, colt type ref to cl_dd_area. data gv_text type sdydo_text_element. *----------------------------------------------------------------------* * CLASS * *----------------------------------------------------------------------* *... Definition is later class lcl_handle_events definition deferred. data: gr_events type ref to lcl_handle_events. ***************************************************** * CLASS cl_my_event_handler * ***************************************************** class cl_my_event_handler definition. public section. methods: use_new_resources for event resources_changed of cl_gui_resources. endclass. "cl_my_event_handler DEFINITION data: my_handler type ref to cl_my_event_handler. **************************************************** * cl_my_event_handler implementation * **************************************************** class cl_my_event_handler implementation. method use_new_resources. perform method_use_new_resources. endmethod. "use_new_resources endclass. "cl_my_event_handler IMPLEMENTATION *---------------------------------------------------------------------* * CLASS lcl_handle_events DEFINITION *---------------------------------------------------------------------* * §5.1 define a local class for handling events of cl_salv_table *---------------------------------------------------------------------* class lcl_handle_events definition. public section. methods: on_user_command for event added_function of cl_salv_events importing e_salv_function. endclass. "lcl_handle_events DEFINITION

Page 8: SALV_TO_DD

*---------------------------------------------------------------------* * CLASS lcl_handle_events IMPLEMENTATION *---------------------------------------------------------------------* * §5.2 implement the events for handling the events of cl_salv_table *---------------------------------------------------------------------* class lcl_handle_events implementation. method on_user_command. if e_salv_function(4) = 'INFO'. perform show_info using e_salv_function. endif. endmethod. "on_user_command endclass. "lcl_handle_events IMPLEMENTATION *----------------------------------------------------------------------* * SELECTION-SCREEN - for demonstration purposes only * *----------------------------------------------------------------------* selection-screen begin of block gen with frame. parameters: p_amount type i default 10. selection-screen end of block gen. *----------------------------------------------------------------------* * START-OF-SELECTION * *----------------------------------------------------------------------* start-of-selection. gs_opt-amount = p_amount. gs_opt-repid = sy-repid. perform select_data. *----------------------------------------------------------------------* * END-OF-SELECTION * *----------------------------------------------------------------------* end-of-selection. perform display_list. * PBO/PAI *----------------------------------------------------------------------* * MODULE status_0100 OUTPUT *----------------------------------------------------------------------* * *----------------------------------------------------------------------* module status_0100 output. check first_display eq 'X'. if g_custom_container is initial. create object g_custom_container exporting container_name = 'HTML'. endif. perform show_information. clear first_display. endmodule. "status_0100 OUTPUT *----------------------------------------------------------------------* * MODULE user_command_0100 INPUT *----------------------------------------------------------------------* * *----------------------------------------------------------------------* module user_command_0100 input. clear save_ok. save_ok = g_ok_code. clear g_ok_code.

Page 9: SALV_TO_DD

call method cl_gui_cfw=>dispatch importing return_code = return_code. if return_code <> cl_gui_cfw=>rc_noevent. " a control event occured => exit PAI exit. endif. case save_ok. when 'BACK' or 'EXIT' or 'CANC'. " Finish program perform init_dd. clear g_document. perform init_container. first_display = 'X'. set screen 0. leave screen. when 'PRINT'. call method g_document->print_document exporting reuse_control = 'X'. * print_ctrl = '7'. when 'LIST_VIEW'. gv_view = 'L'. perform init_dd. clear g_document. perform init_container. first_display = 'X'. when 'FORM_VIEW'. gv_view = 'F'. perform init_dd. clear g_document. perform init_container. first_display = 'X'. when others. if g_ok_code(4) = '%_GC'. call method cl_gui_cfw=>dispatch. endif. endcase. * CAUTION: clear ok code! clear g_ok_code. endmodule. "user_command_0100 INPUT * SALV *&---------------------------------------------------------------------* *& Form select_data *&---------------------------------------------------------------------* * §1 to display the data, you first have to select it in some table *----------------------------------------------------------------------* form select_data. select * from alv_t_t2 into corresponding fields of table gt_outtab up to gs_opt-amount rows. "#EC * endform. " select_data *&---------------------------------------------------------------------* *& Form display_list *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* form display_list . *... create an ALV table * set LIST_DISPLAY to 'X' for displaying an ALV List

Page 10: SALV_TO_DD

try. cl_salv_table=>factory( exporting list_display = abap_true importing r_salv_table = gr_table changing t_table = gt_outtab ). catch cx_salv_msg. "#EC NO_HANDLER endtry. *... Functions data: lr_functions type ref to cl_salv_functions_list. gr_table->set_screen_status( pfstatus = 'SALV_STANDARD' report = gs_opt-repid set_functions = gr_table->c_functions_all ). *... set columns gr_columns = gr_table->get_columns( ). gr_columns->set_optimize( abap_true ). perform set_columns_technical using gr_columns. *- register to the events of cl_salv_table data: lr_events type ref to cl_salv_events_table. lr_events = gr_table->get_event( ). create object gr_events. *-- register to the event USER_COMMAND set handler gr_events->on_user_command for lr_events. *... display the table gr_table->display( ). endform. " display_list *&---------------------------------------------------------------------* *& 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.

Page 11: SALV_TO_DD

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 * DD *&---------------------------------------------------------------------* *& Form show_info *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * -->I_FUNCTION text *----------------------------------------------------------------------* form show_info using i_function type salv_de_function. if i_function = 'INFO'. gv_view = k_view_form. else. gv_view = k_view_list. endif. call screen 100. endform. "show_info *&---------------------------------------------------------------------* *& Form show_information *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* form show_information. refresh exclude_functions. if gv_view = k_view_list. exclude_functions = 'LIST_VIEW'. else. exclude_functions = 'FORM_VIEW'. endif. append exclude_functions. set pf-status 'INFO' excluding exclude_functions. set titlebar 'T01' with 'INFORMATION'. if my_handler is initial. create object my_handler.

Page 12: SALV_TO_DD

endif. * SET PF-STATUS 'BRP'. set handler my_handler->use_new_resources. * create document create object g_document. * initialize documents call method g_document->initialize_document. * EXPORTING * background_color = cl_dd_document=>col_textarea. * Prepare document perform prepare_document. * merge documents call method g_document->merge_document. * display the document inside the container named 'HTML' call method g_document->display_document exporting parent = g_custom_container * parent = cl_gui_container=>default_screen exceptions html_display_error = 1. endform. "show_information *&---------------------------------------------------------------------* *& Form method_use_new_resources *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* form method_use_new_resources. if first_display is initial. * initialize documents call method g_document->initialize_document. * prepare_document perform prepare_document. * merge documents call method g_document->merge_document. * display documents call method g_document->display_document exporting reuse_control = 'X' reuse_registration = 'X'. endif. endform. " METHOD_USE_NEW_RESOURCES *&---------------------------------------------------------------------* *& Form prepare_document *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* form prepare_document. clear gv_text. call method g_document->new_line. move 'INFO' to gv_text. call method g_document->add_text exporting text = gv_text sap_style = 'HEADING'. call method g_document->new_line.

Page 13: SALV_TO_DD

call method g_document->new_line. perform show_lines. call method g_document->new_line. call method g_document->new_line. endform. "prepare_document *&---------------------------------------------------------------------* *& Form show_lines *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* form show_lines. data : lv_table type dd03p-tabname, lv_struct type dd03p-tabname. data: lv_fname type lvc_fname. data: lv_lenght type i. data: lv_flag type c. field-symbols: <it_data> type standard table, <is_data> type any. data: lr_column type ref to cl_salv_column. data: lt_column_ref type salv_t_column_ref, ls_column_ref type salv_s_column_ref. lv_table = gc_inttab. * DD if gv_view = k_view_form. perform add_form_table_header. lt_column_ref = gr_columns->get( ). loop at gt_outtab into gs_outtab. if sy-tabix > 1. perform add_line_to_table. endif. clear: gv_index. loop at lt_column_ref into ls_column_ref. try. lr_column ?= gr_columns->get_column( ls_column_ref-columnname ). catch cx_salv_not_found. endtry. check lr_column is not initial. * IS_Technical lv_flag = lr_column->is_technical( ). check lv_flag is initial. * IS_Visible lv_flag = lr_column->is_visible( ). check lv_flag = abap_true. * gv_index = gv_index + 1. check gv_index <= gc_max_cols. move gv_index to gv_num. concatenate 'col' gv_num into g_fieldname. assign (g_fieldname) to <fsc>. lv_fname = lr_column->get_columnname( ). concatenate 'GS_OUTTAB-' lv_fname into g_fieldname. assign (g_fieldname) to <fsf>.

Page 14: SALV_TO_DD

clear gv_text. perform field_conversion using <fsf> changing gv_text. perform add_text_columm using <fsc> gv_text. endloop. endloop. else. lt_column_ref = gr_columns->get( ). loop at gt_outtab into gs_outtab. if sy-tabix > 1. clear: ta1, col01, col02. call method g_document->new_line. call method g_document->new_line. endif. perform add_list_table. clear: gv_indx. loop at lt_column_ref into ls_column_ref. try. lr_column ?= gr_columns->get_column( ls_column_ref-columnname ). catch cx_salv_not_found. endtry. check lr_column is not initial. * IS_Technical lv_flag = lr_column->is_technical( ). check lv_flag is initial. * IS_Visible lv_flag = lr_column->is_visible( ). check lv_flag = abap_true. gv_indx = gv_indx + 1. check gv_indx <= gc_max_cols. if gv_indx > 1. perform add_line_to_table. endif. move 'col01' to g_fieldname. assign (g_fieldname) to <fsc>. lv_lenght = lr_column->get_output_length( ). gv_text = lr_column->get_long_text( ). perform add_text_columm using <fsc> gv_text. move 'col02' to g_fieldname. assign (g_fieldname) to <fsc>. lv_fname = lr_column->get_columnname( ). concatenate 'GS_OUTTAB-' lv_fname into g_fieldname. assign (g_fieldname) to <fsf>. clear gv_text. perform field_conversion using <fsf> changing gv_text. perform add_text_columm using <fsc> gv_text. endloop. endloop. endif. endform. "show_lines *&---------------------------------------------------------------------* *& Form add_header *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * -->P_HEADER text

Page 15: SALV_TO_DD

*----------------------------------------------------------------------* form add_header using p_header. data text11 type sdydo_text_element. text11 = p_header. * HEADER call method g_document->add_text exporting text = text11 sap_style = 'heading'. * New line perform add_line. endform. "add_header *&---------------------------------------------------------------------* *& Form add_form_table_header *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* form add_form_table_header . data head type sdydo_text_element. data: lv_column type ref to cl_dd_area. data: lr_column type ref to cl_salv_column. data: lt_column_ref type salv_t_column_ref, ls_column_ref type salv_s_column_ref. data: lv_index like sy-tabix. data: lv_flag type c. data: lv_lenght type i. lt_column_ref = gr_columns->get( ). clear gv_cols. loop at lt_column_ref into ls_column_ref. try. lr_column ?= gr_columns->get_column( ls_column_ref-columnname ). catch cx_salv_not_found. endtry. check lr_column is not initial. * IS_Technical lv_flag = lr_column->is_technical( ). check lv_flag is initial. * IS_Visible lv_flag = lr_column->is_visible( ). check lv_flag = abap_true. gv_cols = gv_cols + 1. endloop. * create Table 1.1 call method g_document->add_table exporting no_of_columns = gv_cols cell_background_transparent = space with_heading = 'X' importing table = ta1. clear gv_cols. loop at lt_column_ref into ls_column_ref. try. lr_column ?= gr_columns->get_column( ls_column_ref-columnname ). catch cx_salv_not_found.

Page 16: SALV_TO_DD

endtry. check lr_column is not initial. lv_flag = lr_column->is_technical( ). check lv_flag is initial. lv_flag = lr_column->is_visible( ). check lv_flag = abap_true. lv_index = lv_index + 1. move lv_index to gv_num. check lv_index <= gc_max_cols. concatenate 'col' gv_num into g_fieldname. assign (g_fieldname) to <fsc>. lv_lenght = lr_column->get_output_length( ). if lv_lenght <= 10. head = lr_column->get_short_text( ). elseif lv_lenght <= 20. head = lr_column->get_medium_text( ). else. head = lr_column->get_long_text( ). endif. call method ta1->add_column exporting heading = head importing column = <fsc>. endloop. endform. " *&---------------------------------------------------------------------* *& Form add_list_table *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* form add_list_table . data head type sdydo_text_element. data: lv_column type ref to cl_dd_area. * create Table 1.1 call method g_document->add_table exporting no_of_columns = 2 cell_background_transparent = space with_heading = ' ' importing table = ta1. call method ta1->add_column importing column = col01. call method ta1->add_column exporting width = '250' importing column = col02. call method ta1->set_column_style

Page 17: SALV_TO_DD

exporting col_no = 1 sap_color = cl_dd_area=>list_heading. endform. "add_list_table *&---------------------------------------------------------------------* *& Form add_line *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* form add_line. call method g_document->new_line exporting repeat = 1. endform. "add_line *&---------------------------------------------------------------------* *& Form dd_add_text_columm *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * -->LV_IDX text * -->P_TEXT text *----------------------------------------------------------------------* form dd_add_text_columm using lv_idx "coluna p_text. data lv_col type ref to object. data col1 type ref to cl_dd_area. * set text read table ta1->table_of_columns index lv_idx into lv_col. col1 ?= lv_col. " Type cast from Object to CL_DD_AREA. call method col1->add_text exporting text = p_text. endform. "dd_add_text_columm *&---------------------------------------------------------------------* *& Form add_column *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * -->COLX text *----------------------------------------------------------------------* form add_column using colx type ref to cl_dd_area. call method ta1->add_column importing column = colx. endform. "add_column *&---------------------------------------------------------------------* *& Form add_text_columm *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * -->COLX text * -->P_TEXT text *----------------------------------------------------------------------* form add_text_columm using colx type ref to cl_dd_area p_text. call method colx->add_text exporting

Page 18: SALV_TO_DD

text = p_text. endform. "add_text_columm *&---------------------------------------------------------------------* *& Form add_line_to_table *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* form add_line_to_table. if flag ne 'X'. call method ta1->new_row exporting sap_color = cl_dd_area=>list_background. flag = 'X'. else. call method ta1->new_row. clear: flag. endif. clear: gv_index. endform. "add_line_to_table *&---------------------------------------------------------------------* *& Form init_container *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* form init_container. check not g_custom_container is initial. call method g_custom_container->free exceptions cntl_system_error = 1 cntl_error = 2. if sy-subrc <> 0. message a000(zsd) with 'GUI Error'. endif. call method cl_gui_cfw=>flush exceptions cntl_system_error = 1 cntl_error = 2. if sy-subrc <> 0. message a000(zsd) with 'GUI Error'. endif. clear g_custom_container. endform. "init_container *&---------------------------------------------------------------------* *& Form init_dd *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* form init_dd. clear: ta1, flag, col01, col02, col03, col04, col05, col06, col07, col08, col09, col10, col11, col12, col13, col14, col15, col16, col17, col18, col19, col20, col21. endform. "init_dd *&---------------------------------------------------------------------* *& Form field_conversion *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * -->F_IN text * -->F_OUT text *----------------------------------------------------------------------* form field_conversion using f_in changing f_out.

Page 19: SALV_TO_DD

if not gs_fcat-convexit is initial. case gs_fcat-convexit. when 'ALPHA'. call function 'CONVERSION_EXIT_ALPHA_OUTPUT' exporting input = f_in importing output = f_out. exit. endcase. endif. case gs_fcat-inttype. when 'D'. call function 'CONVERT_DATE_TO_EXTERNAL' exporting date_internal = f_in importing date_external = f_out exceptions date_internal_is_invalid = 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. when 'T'. when others. f_out = f_in. endcase. endform. "field_conversion