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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值