ALV ad Oggetti
Ecco una breve guida per la creazione di un ALV utilizzando la programmazione ad oggetti.
Per prima cosa bisogna dichiarare quelle che saranno le strutture e contenitori della nostra ALV
TYPE-POOLS: slis. INCLUDE <cntn01>. DATA: *--- ALV Grid instance reference alvgrid TYPE REF TO cl_gui_alv_grid , *--- Custom container instance reference ccontainer TYPE REF TO cl_gui_custom_container , *--- Field catalog table wa_fieldcat TYPE lvc_s_fcat, i_fieldcat LIKE STANDARD TABLE OF wa_fieldcat, *--- Layout structure layout TYPE lvc_s_layo .
Fatto ciò procediamo con la creazione di una custom control, all’interno della dynpro interessata premiamo il pulsante evidenziato
e disegniamo il container.
Ricordiamoci di dare un NOME al container che utilizzeremo per la definizione dell’oggetto pilota.
Terminato con il layout creiamo all’interno del PBO un modulo che lo chiameremo creazione_alv , all’interno di questo modulo inseriamo il seguente codice.
IF ccontainer IS INITIAL . ** creazione oggetto PERFORM creaz_ogg_alv USING 'ALV_CONTAINER'."Nome del container *** Creazione grid PERFORM creazione_grid USING abap_true space. *>> Definiamo il Layout CLEAR layout. layout-zebra = abap_true. layout-grid_title = space. layout-no_toolbar = abap_true. **** Creazione FieldCat IF i_fieldcat[] IS INITIAL. REFRESH i_fieldcat. PERFORM fieldcat TABLES i_fieldcat USING: 'ID_CAUSALE' 'I_TABELLA_OUT' 15 'Causale' abap_true abap_true space space space space space space space space space, 'IMPORTO_IMP' 'I_TABELLA_OUT' 15 'Totale' abap_true space space space space space space space 'DEC' 'C' space. ENDIF. ***** Creazione ALV PERFORM alv TABLES i_tabella_out i_fieldcat. *>> Modalità NON editabile ( se settato a 1 i campi diventano editabili ) CALL METHOD alvgrid->set_ready_for_input EXPORTING i_ready_for_input = 0. ELSE. CALL METHOD alvgrid->refresh_table_display. ENDIF. CALL FUNCTION 'AC_SYSTEM_FLUSH' EXCEPTIONS OTHERS = 1.
Terminato con il modulo procediamo con le singole Performe
- creaz_ogg_alv
Creiamo l’oggetto pilota che definisce in quale container bisogna creare ALV ( questo perché possiamo avere N container all’interno della stessa dynpro e quindi creare N ALV differenti all’interno della stessa schermata )
FORM creaz_ogg_alv USING p_name TYPE scrfname. *----Creating custom container instance CREATE OBJECT ccontainer EXPORTING container_name = p_name EXCEPTIONS cntl_error = 1 cntl_system_error = 2 create_error = 3 lifetime_error = 4 lifetime_dynpro_dynpro_link = 5 OTHERS = 6. IF sy-subrc <> 0. *--Exception handling ENDIF. ENDFORM. " creaz_ogg_alv
- creazione_grid
Definito il container creiamo la Grid all’interno di esso.
FORM creazione_grid USING p_no_toolbar p_grid_title. IF alvgrid IS INITIAL. *----Creating ALV Grid instance CREATE OBJECT alvgrid EXPORTING i_appl_events = abap_true " abilita gli eventi i_parent = ccontainer EXCEPTIONS error_cntl_create = 1 error_cntl_init = 2 error_cntl_link = 3 error_dp_create = 4 OTHERS = 5. IF sy-subrc <> 0. *--Exception handling ENDIF. ENDIF. ENDFORM. " creazione_tab
- fieldcat
Definiamo la struttura di output dell’alv ( molto simile alla struttura fieldcat dell’alv tramite function module ).
FORM fieldcat TABLES i_fieldcat STRUCTURE wa_fieldcat USING p_fieldname p_tabname p_leng p_desc p_edit p_f4 p_icon p_dragid p_checktable p_hotspot p_cfieldname p_currency p_datatype p_inttype p_checkbox. CLEAR wa_fieldcat. wa_fieldcat-edit = p_edit. wa_fieldcat-fieldname = p_fieldname. wa_fieldcat-ref_field = p_fieldname. wa_fieldcat-icon = p_icon. wa_fieldcat-tabname = p_tabname. wa_fieldcat-f4availabl = p_f4. wa_fieldcat-outputlen = p_leng. wa_fieldcat-reptext = p_desc. wa_fieldcat-drdn_hndl = p_dragid. wa_fieldcat-checktable = p_checktable. wa_fieldcat-hotspot = p_hotspot. wa_fieldcat-cfieldname = p_cfieldname. wa_fieldcat-currency = p_currency. wa_fieldcat-no_zero = abap_true. wa_fieldcat-datatype = p_datatype. wa_fieldcat-inttype = p_inttype. wa_fieldcat-checkbox = p_checkbox. APPEND wa_fieldcat TO i_fieldcat. CLEAR wa_fieldcat. ENDFORM. " FIELDCAT
- ALV
Mostriamo l’ALV a video, se alvgrid è valorizzata ( quindi abbiamo già chiamato per la prima volta la first display) non creo di nuovo ALV ma effettuo l’aggiornamento a video ( se i dati nella tabella interna di output sono stati modificati durante un elaborazione post-alv la refresh mostra automaticamente i nuovi valori ).
FORM alv TABLES i_out i_fieldcat . IF alvgrid IS NOT INITIAL. *** ALV CALL METHOD alvgrid->set_table_for_first_display EXPORTING it_toolbar_excluding = lt_exclude is_layout = layout CHANGING it_outtab = i_out[] it_fieldcatalog = i_fieldcat[] EXCEPTIONS invalid_parameter_combination = 1 program_error = 2 too_many_lines = 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. ENDIF. ELSE. CALL METHOD alvgrid->refresh_table_display EXCEPTIONS finished = 1 OTHERS = 2. IF sy-subrc <> 0. *--Exception ENDIF. ENDIF. ENDFORM. " ALV
A questo punto abbiamo terminato la creazione della nostra ALV ad oggetti.