메뉴 건너뛰기

SAP 한국 커뮤니티



REPORT z_alv_grid_ctrl_refresh_2

노름마치 2007.11.05 21:46 조회 수 : 3935

REPORT z_alv_grid_ctrl_refresh_2.
***********************************************************************
* ALV Grid Control                                                    *
* This report reads and displays data from table MARA,  using         *
* the Method set_table_for_first_display of CL_GUI_ALV_GRID           *
* Button 'NEXT_PAGE'  : displays the next N records                   *
* Button 'PREV_PAGE'  : displays the previous N records               *
* Button 'FIRST_PAGE' : displays the first page                       *
* Button 'LAST_PAGE'  : displays the last page                        *
* When the buttons Sort up, sort down, Filter, Delete Filter are      *
* pressed, N record are still displayed                               *
*---------------------------------------------------------------------*
* Steps :                                                             *
* - Create the report Z_ALV_GRID_CTRL_REFRESH_2                       *
* - Create the Dynpro 0100 (size 27x120)                              *
* - Add OKCODE (type OK) in the element list                          *
* - Modify the flow logic of dynpro 0100 :                            *
*   * PROCESS BEFORE OUTPUT.                                          *
*       MODULE pbo_0100.                                              *
*   * PROCESS AFTER INPUT.                                            *
*       MODULE user_command_0100.                                     *
* - Create a status named 'MAIN'                                      *
*   with the buttons : REFRESH BACK EXIT                              *
*   and the buttons : FIRST_PAGE PREV_PAGE NEXT_PAGE LAST_PAGE        *
*---------------------------------------------------------------------*
* Author : Michel PIOUD                                               *
* Email : mpioud@yahoo.fr  HomePage : http://www.geocities.com/mpioud *
***********************************************************************
CONSTANTS:
  c_first_page TYPE syucomm VALUE 'FIRST_PAGE',
  c_next_page  TYPE syucomm VALUE 'NEXT_PAGE',
  c_prev_page  TYPE syucomm VALUE 'PREV_PAGE',
  c_last_page  TYPE syucomm VALUE 'LAST_PAGE'.


TYPES:
  BEGIN OF ty_s_mara,
    ernam LIKE mara-ernam,
    matnr LIKE mara-matnr,
    ersda LIKE mara-ersda,
    brgew LIKE mara-brgew,
  END OF ty_s_mara.


CLASS lcl_event_alv DEFINITION DEFERRED.


DATA:
  gt_mara TYPE STANDARD TABLE OF ty_s_mara,
  go_container TYPE REF TO cl_gui_docking_container,
  go_alv_grid  TYPE REF TO cl_gui_alv_grid,
  go_events    TYPE REF TO lcl_event_alv,


  gt_mara_ftr TYPE STANDARD TABLE OF ty_s_mara,   " Data filtered
  gt_mara_all TYPE STANDARD TABLE OF ty_s_mara,   " Data readfrom DB


  okcode    TYPE syucomm,
  gv_okcode TYPE syucomm.


*---------------------------------------------------------------------*
*       CLASS lcl_event_alv DEFINITION
*---------------------------------------------------------------------*
CLASS lcl_event_alv DEFINITION.


  PUBLIC SECTION.
    METHODS:
      h_user_command FOR EVENT after_user_command OF cl_gui_alv_grid
                     IMPORTING e_ucomm
                               sender.


ENDCLASS.                              " LCL_EVENT_ALV DEFINITION
*---------------------------------------------------------------------*
*        Class (Implementation)  lcl_event_alv
*---------------------------------------------------------------------*
CLASS lcl_event_alv IMPLEMENTATION.


  METHOD h_user_command.
    CASE e_ucomm.
      WHEN '&SORT_ASC' OR '&SORT_DSC'. " Sort
        PERFORM f_sort_big_table.
        PERFORM f_read_data USING c_first_page.
        PERFORM f_refresh_table.
      WHEN '&FILTER'.                  " Filter
        PERFORM f_filter_data.
        PERFORM f_read_data USING c_first_page.
        PERFORM f_refresh_table.
      WHEN '&DELETE_FILTER'.           " Delete filter
        gt_mara_ftr[] = gt_mara_all[].
        PERFORM f_read_data USING c_first_page.
        PERFORM f_refresh_table.
    ENDCASE.
  ENDMETHOD.                           " user_command


ENDCLASS.                              " LCL_EVENT_ALV
*---------------------------------------------------------------------*
SELECTION-SCREEN :
BEGIN OF LINE,COMMENT 10(20) v_1 FOR FIELD p_max.           "#EC NEEDED
PARAMETERS p_max(2) TYPE n DEFAULT '30' OBLIGATORY.
SELECTION-SCREEN END OF LINE.
*---------------------------------------------------------------------*
INITIALIZATION.


  v_1 = 'Lines per page'.


*---------------------------------------------------------------------*
START-OF-SELECTION.


  SELECT matnr ernam ersda brgew
    INTO TABLE gt_mara_all
    FROM mara.


  gt_mara_ftr[] = gt_mara_all[].


  PERFORM f_read_data USING c_first_page.


  CALL SCREEN 100.


*---------------------------------------------------------------------*
*       Module  pbo_0100  OUTPUT
*---------------------------------------------------------------------*
MODULE pbo_0100 OUTPUT.


  SET PF-STATUS 'MAIN'.


  PERFORM create_and_init_alv.


ENDMODULE.                             " PBO_0100  OUTPUT
*---------------------------------------------------------------------*
*       Module  user_command_0100  INPUT
*---------------------------------------------------------------------*
MODULE user_command_0100 INPUT.


  gv_okcode = okcode.
  CLEAR okcode.


  CASE gv_okcode.
    WHEN 'BACK'.
      SET SCREEN 0.
    WHEN 'EXIT'.
      LEAVE PROGRAM.
    WHEN c_first_page OR c_next_page OR c_last_page OR c_prev_page.
      PERFORM f_read_data USING gv_okcode.            " Update gt_mara
      PERFORM f_refresh_table.
  ENDCASE.


ENDMODULE.                             " USER_COMMAND_0100  INPUT
*---------------------------------------------------------------------*
*      Form  f_read_data
*---------------------------------------------------------------------*
FORM f_read_data USING u_ucomm TYPE syucomm.


  STATICS :
    l_1   TYPE sytabix,
    l_2   TYPE sytabix.


  DATA l_max TYPE sytabix.             " Internal table size


  DESCRIBE TABLE gt_mara_ftr LINES l_max.


  CASE u_ucomm.
    WHEN c_first_page.                 " 1st page
      l_1 = 1.
    WHEN c_prev_page.                  " Previous page
      SUBTRACT p_max FROM l_1.
      IF l_1 < 1.
        l_1 = 1.
      ENDIF.
    WHEN c_next_page.                  " Next page
      IF l_1 IS INITIAL.
        l_1 = 1.
      ELSE.
        ADD p_max TO l_1.
      ENDIF.


      IF l_1 > l_max.
        l_1 = l_max.
      ENDIF.
    WHEN c_last_page.                  " Last page
      l_1 = l_max - p_max + 1.
      IF l_1 < 1.
        l_1 = 1.
      ENDIF.
  ENDCASE.


  l_2 = l_1 + p_max - 1.


  IF l_2 > l_max.
    l_2 = l_max.
  ENDIF.


  REFRESH gt_mara.
  IF l_max > 0.
    APPEND LINES OF gt_mara_ftr FROM l_1
                                  TO l_2
                                  TO gt_mara.
  ENDIF.


ENDFORM.                               " F_READ_DATA
*---------------------------------------------------------------------*
*       Form  create_and_init_alv
*---------------------------------------------------------------------*
FORM create_and_init_alv.


* Macro definition
  DEFINE m_fieldcat.
    add 1 to ls_alv_cat-col_pos.
    ls_alv_cat-fieldname = &1.
    ls_alv_cat-ref_table = 'MARA'.
    append ls_alv_cat to lt_alv_cat.
  END-OF-DEFINITION.


  DATA:
    ls_variant      TYPE disvariant,
    lt_alv_cat      TYPE lvc_t_fcat,
    ls_alv_cat      TYPE lvc_s_fcat,
    ls_alv_lay      TYPE lvc_s_layo,
    l_offline       TYPE char1.


  CHECK go_container IS INITIAL.


  CALL METHOD cl_gui_alv_grid=>offline
    RECEIVING e_offline = l_offline.


  IF l_offline EQ 0.
    CREATE OBJECT go_container
        EXPORTING
            extension = 2000
        EXCEPTIONS
            cntl_error = 1
            cntl_system_error = 2
            create_error = 3
            lifetime_error = 4
            lifetime_dynpro_dynpro_link = 5
            others                      = 6.
    IF sy-subrc NE 0.
      MESSAGE e208(00) WITH 'The control could not be created'.
    ENDIF.
  ENDIF.


* Create an instance of alv control
  CREATE OBJECT go_alv_grid
      EXPORTING i_parent = go_container.


  CREATE OBJECT go_events.
  SET HANDLER go_events->h_user_command FOR go_alv_grid.


* Build field catalog
  m_fieldcat 'ERNAM'.
  m_fieldcat 'MATNR'.
  m_fieldcat 'ERSDA'.
  m_fieldcat 'BRGEW'.


* Layout
  CLEAR ls_alv_lay.
  ls_alv_lay-zebra      = 'X'.
  ls_alv_lay-cwidth_opt = 'X'.


  ls_variant-report = sy-cprog.


* Display
  CALL METHOD go_alv_grid->set_table_for_first_display
    EXPORTING
      is_variant      = ls_variant
      is_layout       = ls_alv_lay
      i_save          = 'A'
    CHANGING
      it_outtab       = gt_mara
      it_fieldcatalog = lt_alv_cat.


ENDFORM.                               " CREATE_AND_INIT_ALV
*---------------------------------------------------------------------*
*      Form  F_REFRESH_TABLE
*---------------------------------------------------------------------*
FORM f_refresh_table.


  DATA: ls_layout TYPE lvc_s_layo.


  CALL METHOD go_alv_grid->get_frontend_layout
    IMPORTING es_layout = ls_layout.


  ls_layout-cwidth_opt = 'X'.


  CALL METHOD go_alv_grid->set_frontend_layout
    EXPORTING is_layout = ls_layout.


  CALL METHOD go_alv_grid->refresh_table_display.


ENDFORM.                               " F_REFRESH_TABLE
*---------------------------------------------------------------------*
*       Form  F_SORT_BIG_TABLE
*---------------------------------------------------------------------*
FORM f_sort_big_table.


  DATA:
    lt_sort_kkblo TYPE kkblo_t_sortinfo,
    lt_sort       TYPE lvc_t_sort.


  CALL METHOD go_alv_grid->get_sort_criteria
    IMPORTING et_sort = lt_sort.


  CHECK NOT lt_sort[] IS INITIAL.


* Format LVC --> KKBLO
  CALL FUNCTION 'LVC_TRANSFER_TO_KKBLO'
       EXPORTING
            it_sort_lvc   = lt_sort
       IMPORTING
            et_sort_kkblo = lt_sort_kkblo.


* The big tables must be sorted like the small one
  PERFORM fb_outtab_sort(saplkkbl) TABLES gt_mara_ftr
                                          lt_sort_kkblo
                                    USING 'X'
                                          'X'.


  PERFORM fb_outtab_sort(saplkkbl) TABLES gt_mara_all
                                          lt_sort_kkblo
                                    USING 'X'
                                          'X'.


ENDFORM.                               " F_SORT_BIG_TABLE
*---------------------------------------------------------------------*
*      Form  f_filter_data
*---------------------------------------------------------------------*
FORM f_filter_data.


  DATA:
    lt_filter_lvc   TYPE lvc_t_filt,
    lt_filter_index TYPE lvc_t_fidx WITH HEADER LINE.


  CALL METHOD go_alv_grid->get_filter_criteria
    IMPORTING et_filter = lt_filter_lvc.


* Find data to filter
  CALL FUNCTION 'LVC_FILTER_APPLY'
       EXPORTING
            it_filter       = lt_filter_lvc
       IMPORTING
            et_filter_index = lt_filter_index[]
       TABLES
            it_data         = gt_mara_all.


  gt_mara_ftr[] = gt_mara_all[].


  SORT lt_filter_index DESCENDING.
  LOOP AT lt_filter_index.
    DELETE gt_mara_ftr INDEX lt_filter_index.
  ENDLOOP.


ENDFORM.                               " F_FILTER_DATA
********* END OF PROGRAM Z_ALV_GRID_CTRL_REFRESH_2 ********************