ALV-Vorlage REUSE ALV GRID DISPLAY LVC

Aus SAP-Wiki
Wechseln zu: Navigation, Suche

"Data & Types

****************
* DATA & TYPES *
****************
Types: begin of ty_itab,
       kunnr type kna1-kunnr,
       end of ty_itab.

Types: tty_itab type standard table of ty_itab. 

Data: gt_itab type Standard table of ty_itab.

Constants: true  type xfeld value 'X',
           false type xfeld value space.

Tables: vbak.

"Parameters

**************
* PARAMETERS *
**************
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.

PARAMETERS: p_vari TYPE disvariant-variant. "Layout

SELECTION-SCREEN END OF BLOCK b1.

"Initialization

******************
* INITIALIZATION *
******************
INITIALIZATION.
 PERFORM initialization.

"At Selection-Screen

***********************
* AT SELECTION-SCREEN *
***********************
at selection-screen.


"At Selection-Screen on value-request for p_vari

*----------------------------------------------------------------------*
* AT SELECTION-SCREEN ON VALUE-REQUEST FOR <field>
*----------------------------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_vari.
 PERFORM at_selection_screen_for_vari
   CHANGING p_vari.

"Start-of-Selection

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

perform get_data
  changing gt_itab.

********************
* END-OF-SELECTION *
********************
end-of-selection.

************************
* Aufruf der ALV-Liste *
************************
perform alv_itab 
    using gt_itab.

"Form-Routinen

"Form Initialization

*&---------------------------------------------------------------------*
*&      Form  INITIALIZATION
*&---------------------------------------------------------------------*
*       Default Routinen
*----------------------------------------------------------------------*
FORM initialization.
  DATA: lv_report  TYPE  rsvar-report,
        lv_variant TYPE  rsvar-variant.

* Variante STANDARD wird automatisch gezogen
  lv_report  = sy-repid.
  lv_variant = 'STANDARD'.

  CALL FUNCTION 'RS_SUPPORT_SELECTIONS'
    EXPORTING
      report               = lv_report
      variant              = lv_variant
    EXCEPTIONS
      variant_not_existent = 1
      variant_obsolete     = 2
      OTHERS               = 3.

  IF sy-subrc <> 0.
*   Variante & ist nicht vorhanden
    MESSAGE i809(00) WITH lv_variant.
  ENDIF.

 DATA: ls_variant TYPE disvariant. "Anzeigevariante (externe Verwendung)
* Defaultlayoutvariante wird in das Feld P_VARI geschrieben
 ls_variant-report = sy-repid.
 ls_variant-handle = gc_handle_num1.

 CALL FUNCTION 'REUSE_ALV_VARIANT_DEFAULT_GET'
   EXPORTING
     i_save     = 'A'
   CHANGING
     cs_variant = ls_variant
   EXCEPTIONS
     not_found  = 2.
 IF sy-subrc = 0.
   p_vari = ls_variant-variant.
 ENDIF.

ENDFORM.                    "INITIALIZATION

"Form at_selection_screen_for_vari

*&---------------------------------------------------------------------*
*&      Form  AT_SELECTION_SCREEN_FOR_VARI
*&---------------------------------------------------------------------*
*       Liste aller Layoutvarianten wird angeboten und gewählt
*----------------------------------------------------------------------*
FORM at_selection_screen_for_vari
  CHANGING pcv_vari TYPE disvariant-variant. "Layout

  DATA: ls_variant_in  TYPE disvariant, "Anzeigevariante (externe Verwendung)
        ls_variant_out TYPE disvariant, "Anzeigevariante (externe Verwendung)
        lv_exit        TYPE c.

**-- Display all existing variants
  ls_variant_in-report = sy-repid.
  ls_variant_in-handle = gc_handle_num1.

* Utilizing the name of the report, this function module will search for a list of
* variants and will fetch the selected one into the parameter field for variants
  CALL FUNCTION 'REUSE_ALV_VARIANT_F4'
    EXPORTING
      is_variant = ls_variant_in
      i_save     = 'A'             "Benutzer und Standardlayoutvarianten
    IMPORTING
      e_exit     = lv_exit
      es_variant = ls_variant_out
    EXCEPTIONS
      not_found  = 2.
  IF sy-subrc = 2.
    MESSAGE ID sy-msgid TYPE 'S'      NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ELSE.
    IF lv_exit = space.
      pcv_vari = ls_variant_out-variant.
    ENDIF.
  ENDIF.

ENDFORM.                    " AT_SELECTION_SCREEN_FOR_VARI

"Form alv_itab

**&---------------------------------------------------------------------
**& Form alv_itab
*&---------------------------------------------------------------------
* ALV Function Mod. 'REUSE_ALV_GRID_DISPLAY_LVC'                    
*----------------------------------------------------------------------
FORM alv_itab
     using it_itab type tty_itab.

  type-pools: slis.

  data: lv_repid   type sy-repid,
        lt_fcat    type LVC_T_FCAT,
        ls_layout  type LVC_S_LAYO,
*       ls_sort    type LVC_S_SORT,
*       lt_sort    type LVC_T_SORT,
        lt_events  type slis_t_event.       
       
  lv_repid = sy-repid.

***************
* Fill Layout *
***************
* ls_layout-zebra      = 'X'.        "Liste wird im Zebra-Look ausgegeben
* LS_LAYOUT-BOX_FNAME  = 'SELKZ'.    "Zeilen der Liste können markiert werden mit Spalte SELKZ
* ls_layout-no_toolbar = 'X'.        "Keine Toolbar 
* ls_layout-EXCP_FNAME = 'AMPEL'.    "'1'=rot, '2'=gelb, '3'=grün
* ls_layout-TOTALS_BEF = 'X'.        "Summen vor Einzelsätzen
  LS_LAYOUT-CWIDTH_OPT = 'A'.        "Spaltenbreite wird optimiert

*************
* Fill Sort *
*************
*  CLEAR ls_sort.                    "Liste wird nach Feldern sortiert
*  ls_sort-fieldname = ' '.          "Identische Werte, die übereinander stehen, werden
*  ls_sort-spos      = '1'.          "verbunden und nur der oberste Wert wird angezeigt
*  ls_sort-up        = 'X'.
*  ls_sort-group     = 'UL'.
*  ls_sort-subtot    = 'X'.          "Zwischensummen
*  APPEND ls_sort TO lt_sort.

***************
* Fill Events *
***************
  perform alv_itab_events            "Definition Events und Form-Routinten für
    changing lt_events.              "User-Command, Top-Of-List und Status

*****************
* Fill Fieldcat *
*****************
  perform alv_itab_fcat              "Definition Felder, die ausgegeben werden sollen
    changing lt_fcat.

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
  EXPORTING
     i_interface_check          = 'I'
     i_callback_program         = lv_repid
     is_layout_lvc              = ls_layout    "Layout ALV
     it_fieldcat_lvc            = lt_fcat      "Tabellen mit Feldern der Liste
    "it_sort_lvc                = lt_sort      "Sortierung
     i_default                  = 'X'          "Default Variante kann abgespeichert werden
     i_save                     = 'A'          "Benutzerabhängige Varianten können gespeichert werden
     IT_EVENTS                  = lt_events    "Event-Tabelle
    "i_screen_start_column      = 10
    "i_screen_start_line        = 10
    "i_screen_end_column        = 50
    "i_screen_end_line          = 50
  TABLES
     t_outtab                   = it_itab    "Daten Ausgabetabelle
  EXCEPTIONS
     program_error              = 1
     OTHERS                     = 2.

  IF sy-subrc <> 0.
    message i531(0u) with 'Problem beim Fuba REUSE_ALV_GRID_DISPLAY_LVC'. 
  ENDIF.

ENDFORM. " alv_itab

"Form pf_status_set

*&---------------------------------------------------------------------*
*&      Form  pf_status_set
*&---------------------------------------------------------------------*
*       Kopie des Standardstatus STANDARD aus der Funktionsgruppe SALV
*----------------------------------------------------------------------*
form alv_itab_pf_status_set
    using it_extab type slis_t_extab.                       "#EC CALLED

  set pf-status 'STATUS_ITAB' excluding it_extab.
  set TITLEBAR 'TITLE_ITAB'.

endform.                    "pf_status_set

"Form alv_itab_events

*&---------------------------------------------------------------------*
*&      Form  alv_itab_events
*&---------------------------------------------------------------------*
*       ALV-Events
*----------------------------------------------------------------------*
form alv_itab_events
   changing pct_events type SLIS_T_EVENT.

Data: ls_events   type slis_alv_event.

ls_events-name = 'USER_COMMAND'.
ls_events-form = 'ALV_ITAB_USER_COMMAND'.
append ls_events to pct_events.

ls_events-name = 'TOP_OF_PAGE'.
ls_events-form = 'ALV_ITAB_TOP_OF_PAGE'.
append ls_events to pct_events.

*ls_events-name = 'PF_STATUS_SET'.
*ls_events-form = 'ALV_ITAB_PF_STATUS_SET'.
*append ls_events to pct_events.

endform.                    " alv_itab_events

"Form alv_itab_fcat

*&---------------------------------------------------------------------*
*&      Form  alv_itab_fcat
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
form alv_itab_fcat
  changing ct_fcat type LVC_T_FCAT.

  data: ls_fcat type LVC_S_FCAT.

** Field =
*  ls_fcat-fieldname   = ' '.
*  ls_fcat-rollname    = ' '.
*  ls_fcat-key         = true.
*  append ls_fcat to ct_fcat.
*  clear ls_fcat.
*  
** Field =
*  ls_fcat-fieldname   = ' '.
*  ls_fcat-rollname    = ' '.
*  ls_fcat-key         = true.
*  append ls_fcat to ct_fcat.
*  clear ls_fcat.
*  
** Field =
*  ls_fcat-fieldname   = ' '.
*  ls_fcat-rollname    = ' '.
*  ls_fcat-key         = .
*  append ls_fcat to ct_fcat.
*  clear ls_fcat.
*  
** Field =
*  ls_fcat-fieldname   = ' '.
*  ls_fcat-rollname    = ' '.
*  ls_fcat-key         = .
*  append ls_fcat to ct_fcat.
*  clear ls_fcat.
*  
** Field =
*  ls_fcat-fieldname   = ' '.
*  ls_fcat-rollname    = ' '.
*  ls_fcat-key         = .
*  append ls_fcat to ct_fcat.
*  clear ls_fcat.

   
** Field = Original Language
* ls_fcat-fieldname   = ' '.
* ls_fcat-rollname    = ' '.
* ls_fcat-reptext     = ' '.
* ls_fcat-seltext     = ' '.
* ls_fcat-outputlen   = 10.
* ls_fcat-emphasize   = 'C300'.  "zrebconstants=>cv_color_fcat_gelb
* ls_fcat-edit        = 'X'. 
* ls_fcat-do_sum      = 'X'.
* APPEND ls_fcat TO ct_fcat.
* CLEAR ls_fcat.

endform.                    " alv_fcat

"Form user-Command

*&---------------------------------------------------------------------
*& Form USER_COMMAND
*&---------------------------------------------------------------------
*  User Command
*----------------------------------------------------------------------
form alv_itab_user_command                                         "#EC CALLED
  using r_ucomm     type sy-ucomm
        rs_selfield type slis_selfield.

  Data: ls_itab type ty_itab.

  DATA: lo_ref_alv TYPE REF TO cl_gui_alv_grid,
        lv_valid   TYPE c.                                   "#EC NEEDED

* geänderte Daten holen
* Referenz auf ALV holen
  CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
       IMPORTING
            e_grid = lo_ref_alv.

* Gibt die geänderte ITAB zurück
  CALL METHOD lo_ref_alv->check_changed_data
    IMPORTING
      e_valid = lv_valid.
      
* Prüfe ob ein Doppelklick auf eine Zeile erfolgte
  case r_ucomm.
  
  WHEN 'NEWLINE'.
    CALL METHOD lo_ref_alv->APPEND_ROWS
      EXPORTING
        I_ROW_COUNT = 1.

  when '&IC1'.

*   Read line of clicked item
    read table gt_itab index rs_selfield-tabindex
    into ls_itab.

*    case rs_selfield-fieldname.  

*     
*      when ' '.

*  endcase.
endcase.

endform. " USER_COMMAND_ITAB

"Form top_of_page

*---------------------------------------------------------------------
*      FORM top_of_page                                              
*---------------------------------------------------------------------
*      Fülle Header für ALV-Listen
*----------------------------------------------------------------------
FORM alv_itab_top_of_page.      "#EC CALLED

  DATA: lt_listheader TYPE slis_t_listheader,
        w_listheader  TYPE slis_listheader,
        lv_datum(10)  TYPE c,
        lv_lines      TYPE i.

* Schreibe Anzahl Datensätze Reporte in Grid Titel
  DESCRIBE TABLE gt_itab LINES lv_lines.

* Datum in Ausgabeform bringen
  WRITE sy-datum TO lv_datum.

  w_listheader-typ = 'S'.
  w_listheader-key = 'Datum'.
  w_listheader-info = lv_datum.
  APPEND w_listheader TO lt_listheader.

  w_listheader-typ = 'S'.
  w_listheader-key = 'Anzahl Datensätze'.
  w_listheader-info = lv_lines.
  APPEND w_listheader TO lt_listheader.

  CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
       EXPORTING
           it_list_commentary = lt_listheader.
*          I_LOGO             =
*          I_END_OF_LIST_GRID =

ENDFORM.

"Form get_data

*&---------------------------------------------------------------------*
*&      Form  GET_DATA
*&---------------------------------------------------------------------*
*       Lesen Daten
*----------------------------------------------------------------------*
FORM get_data
  CHANGING ct_itab type tty_itab.

ENDFORM.