create dynamic internal table & editable cl_salv_table

REPORT  ZTEST.

*----------------------------------------------------------------------*
*       CLASS lcl_handler DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_handler DEFINITION.
  PUBLIC SECTION.
    METHODS: on_user_command FOR EVENT added_function OF cl_salv_events
                             IMPORTING e_salv_function.
ENDCLASS.                    "lcl_handler DEFINITION

*----------------------------------------------------------------------*
*       CLASS lcl_handler IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS lcl_handler IMPLEMENTATION.
  METHOD on_user_command.
    DATA: l_grid TYPE REF TO cl_gui_alv_grid,
          lv_layout TYPE lvc_s_layo.
    IF e_salv_function EQ 'EDIT'.
      CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
        IMPORTING
          e_grid = l_grid.
      CALL METHOD l_grid->get_frontend_layout
        IMPORTING
          es_layout = lv_layout.
      lv_layout-edit = 'X'.
      CALL METHOD l_grid->set_frontend_layout
        EXPORTING
          is_layout = lv_layout.
      CALL METHOD l_grid->refresh_table_display( ).
    ENDIF.
  ENDMETHOD.                    "on_user_command
ENDCLASS.                    "lcl_handler IMPLEMENTATION

TYPE-POOLS: abap.

*--------------------------------------------------------------------*
* Variable
*--------------------------------------------------------------------*
DATA: w_itab TYPE REF TO data,
      w_wa   TYPE REF TO data.

FIELD-SYMBOLS: <fs> TYPE table,
               <wa>,
               <fld>.
*--------------------------------------------------------------------*
* SELECTION SCREEN
*--------------------------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK blk WITH FRAME TITLE text-001.
PARAMETERS: p_column TYPE i OBLIGATORY,
            p_row    TYPE i OBLIGATORY.
SELECTION-SCREEN END OF BLOCK blk.

*--------------------------------------------------------------------*
* START-OF-SELECTION
*--------------------------------------------------------------------*
START-OF-SELECTION.
  PERFORM prepare_data.
  PERFORM display_alv.

*&---------------------------------------------------------------------*
*&      Form  prepare_data
*&---------------------------------------------------------------------*
FORM prepare_data.
  DATA: lv_num(5)       TYPE n,
        lv_index1       LIKE sy-index,
        lv_index2       LIKE sy-index,
        itab_components TYPE abap_component_tab,
        w_component     TYPE abap_componentdescr,
        lv_type_c       TYPE REF TO cl_abap_datadescr,
        lv_struct       TYPE REF TO cl_abap_structdescr,
        lv_table        TYPE REF TO cl_abap_tabledescr.
  lv_type_c ?= cl_abap_elemdescr=>get_c( 20 ).
  DO p_column TIMES.
    ADD 1 TO lv_num.
    CONCATENATE 'COL' lv_num INTO w_component-name.
    w_component-type = lv_type_c.
    APPEND w_component TO itab_components.
  ENDDO.
  lv_struct = cl_abap_structdescr=>create( itab_components ).
  lv_table  = cl_abap_tabledescr=>create( lv_struct ).
  CREATE DATA w_itab TYPE HANDLE lv_table.
  CREATE DATA w_wa TYPE HANDLE lv_struct.
  ASSIGN w_itab->* TO <fs>.
  ASSIGN w_wa->* TO <wa>.
  DO p_row TIMES.
    lv_index1 = sy-index.
    DO p_column TIMES.
      lv_index2 = sy-index.
      ASSIGN COMPONENT lv_index2 OF STRUCTURE <wa> TO <fld>.
      <fld> = lv_index1 * lv_index2.
    ENDDO.
    APPEND <wa> TO <fs>.
  ENDDO.
ENDFORM.                    "prepare_data

*&---------------------------------------------------------------------*
*&      Form  display_alv
*&---------------------------------------------------------------------*
FORM display_alv.
  DATA: lv_grid TYPE REF TO cl_salv_table,
        lv_functions TYPE REF TO cl_salv_functions_list,
        lv_events    TYPE REF TO cl_salv_events_table,
        lv_handler   TYPE REF TO lcl_handler,
        lv_exception TYPE REF TO cx_root.
  TRY.
      CALL METHOD cl_salv_table=>factory
        IMPORTING
          r_salv_table = lv_grid
        CHANGING
          t_table      = <fs>.
      CREATE OBJECT lv_handler.
      lv_grid->set_screen_status( pfstatus = 'SALV_STANDARD' report = sy-repid
                                  set_functions = lv_grid->c_functions_all ).
      lv_functions = lv_grid->get_functions( ).
      lv_functions->set_all( 'X' ).
      lv_events = lv_grid->get_event( ).
      SET HANDLER lv_handler->on_user_command FOR lv_events.
      lv_grid->display( ).
    CATCH cx_root INTO lv_exception.
  ENDTRY.
ENDFORM.                    "display_alv

你可能感兴趣的:(dynamic)