Tuesday, November 16, 2010

Editable Field catalogue for ALV display

*&---------------------------------------------------------------------*
*& Report ZALV_FCAT *
*& Author : Swarna.S
*&---------------------------------------------------------------------*
*& AS : ALV report which displays the contents of the table T006
*& (as a docking container in the bottom) along with the
*& editable ALV which contains the ALV fieldcatalogue table structure.
*& With the available toolbar options of the editable ALV in the output,
*& user can change the fieldcatalogue as per his requirement.
*& When the user clicks 'SUBMIT',the display of the ALV with table T006
*& gets modified and customised accordingly to the user's requirement.
*&---------------------------------------------------------------------*
REPORT  zalv_fcat.
* Output table T006 structure declaration
TYPES : BEGIN OF ty_t006.
INCLUDE STRUCTURE t006.
TYPES : END OF ty_t006.
*Internal table and wa declaration for T006
DATA : it_t006 TYPE STANDARD TABLE OF ty_t006,
wa_t006 TYPE ty_t006.
*declarations for ALV
DATA: ok_code TYPE sy-ucomm,
* fieldcatalog for T006
it_fielcat TYPE lvc_t_fcat,
* fieldcatalog for fieldcatalog itself:
it_fielcatalogue TYPE lvc_t_fcat,
it_layout TYPE lvc_s_layo.
*declaration for toolbar function
DATA: it_excl_func TYPE ui_functions.
* Controls to display it_t006 and corresponding fieldcatalog
DATA: cont_dock TYPE REF TO cl_gui_docking_container,
cont_alvgd TYPE REF TO cl_gui_alv_grid.
*controls to display the fieldcatalog as editable alv grid and container
DATA: cont_cust TYPE REF TO cl_gui_custom_container,
cont_editalvgd TYPE REF TO cl_gui_alv_grid.
*intialization event
INITIALIZATION.
*start of selection event
START-OF-SELECTION.
**************************************************************
* LOCAL CLASS Definition for data changed in fieldcatalog ALV
**************************************************************
CLASS lcl_event_receiver DEFINITION.
PUBLIC SECTION.
METHODS handle_data_changed
FOR EVENT data_changed OF cl_gui_alv_grid
IMPORTING er_data_changed.
ENDCLASS. "lcl_event_receiver DEFINITION
**************************************************************
* LOCAL CLASS implementation for data changed in fieldcatalog ALV
**************************************************************
CLASS lcl_event_receiver IMPLEMENTATION.
METHOD handle_data_changed.
ENDMETHOD. "handle_data_changed
ENDCLASS. "lcl_event_receiver IMPLEMENTATION
*data declaration for event receiver
DATA: event_receiver TYPE REF TO lcl_event_receiver.
*end of selection event
END-OF-SELECTION.
*setting the screen for alv output for table display and
*changed fieldcatalalogue display
SET SCREEN 600.
*On this statement double click  it takes you to the screen painter SE51.
* Enter the attributes
*Create a Custom container and name it CCONT and OK code as OK_CODE.
*Save check and Activate the screen painter.
*Now a normal screen with number 600 is created which holds the ALV grid.
* PBO of the actual screen , Here we can give a title and customized menus
*Go to SE41 and create status 'STATUS600' and create THE function code 'SUBMIT'
*and 'EXIT' with icons and icon texts
*Also create a TitleBar 'TITLE600' and give the relevant title.
*&---------------------------------------------------------------------*
*& Module STATUS_0600 OUTPUT
*&---------------------------------------------------------------------*
MODULE status_0600 OUTPUT.
SET PF-STATUS 'STATUS600'.
SET TITLEBAR 'TITLE600'.
* CREATE ALV GRID CONTROL IF DOES NOT EXISTS INITIALLY
IF cont_dock IS INITIAL.
PERFORM create_alv.
ENDIF.
ENDMODULE.                             " STATUS_0600  OUTPUT
* PAI module of the screen created. In case we use an interactive ALV or
*for additional functionalities we can create OK codes and based on the
*user command we can do the coding as shown below
*&---------------------------------------------------------------------*
*& Module USER_COMMAND_0600 INPUT
*&---------------------------------------------------------------------*
MODULE user_command_0600 INPUT.
CASE ok_code.
WHEN 'SUBMIT'.
*TO GET THE CURRENT FIELDCATALOGUE FROM THE FRONTEND
CALL METHOD cont_alvgd->set_frontend_fieldcatalog
EXPORTING
it_fieldcatalog = it_fielcat.
*refresh the alv
CALL METHOD cont_alvgd->refresh_table_display.
*to Send Buffered Automation Queue to Frontend
CALL METHOD cl_gui_cfw=>flush.
*Exit button clicked to leave the program
WHEN 'EXIT'.
LEAVE PROGRAM.
  ENDCASE.
ENDMODULE.                             " USER_COMMAND_0600  INPUT
*&---------------------------------------------------------------------*
*& Form CREATE_ALV
*&---------------------------------------------------------------------*
FORM create_alv.
*create a docking container and dock the control at the botton
CREATE OBJECT cont_dock
EXPORTING
dynnr = '600'
extension = 100
side = cl_gui_docking_container=>dock_at_bottom.
*create the alv grid for display the table
CREATE OBJECT cont_alvgd
EXPORTING
i_parent = cont_dock.
*create custome container for alv
CREATE OBJECT cont_cust
EXPORTING
container_name = 'CCONT'.
*create alv editable grid
CREATE OBJECT cont_editalvgd
EXPORTING
i_parent = cont_cust.
* register events for the editable alv
CREATE OBJECT event_receiver.
SET HANDLER event_receiver->handle_data_changed FOR cont_editalvgd.
  CALL METHOD cont_editalvgd->register_edit_event
EXPORTING
i_event_id = cl_gui_alv_grid=>mc_evt_modified.
*building the fieldcatalogue for the initial display
PERFORM build_fieldcat CHANGING it_fielcat it_fielcatalogue.
*building the fieldcatalogue after the user has changed it
PERFORM change_fieldcat CHANGING it_fielcatalogue.
*fetch data from the table
PERFORM fetch_data.
*    Get excluding functions for the alv editable tool bar
  APPEND cl_gui_alv_grid=>mc_fc_loc_append_row TO it_excl_func.
APPEND cl_gui_alv_grid=>mc_fc_loc_insert_row TO it_excl_func.
APPEND cl_gui_alv_grid=>mc_fc_loc_cut TO it_excl_func.
APPEND cl_gui_alv_grid=>mc_fc_sort TO it_excl_func.
APPEND cl_gui_alv_grid=>mc_fc_sort_asc TO it_excl_func.
APPEND cl_gui_alv_grid=>mc_fc_sort_dsc TO it_excl_func.
APPEND cl_gui_alv_grid=>mc_fc_subtot TO it_excl_func.
APPEND cl_gui_alv_grid=>mc_fc_sum TO it_excl_func.
APPEND cl_gui_alv_grid=>mc_fc_graph TO it_excl_func.
APPEND cl_gui_alv_grid=>mc_fc_info TO it_excl_func.
APPEND cl_gui_alv_grid=>mc_fc_print TO it_excl_func.
APPEND cl_gui_alv_grid=>mc_fc_filter TO it_excl_func.
APPEND cl_gui_alv_grid=>mc_fc_views TO it_excl_func.
APPEND cl_gui_alv_grid=>mc_mb_export TO it_excl_func.
APPEND cl_gui_alv_grid=>mc_mb_sum TO it_excl_func.
APPEND cl_gui_alv_grid=>mc_mb_sum TO it_excl_func.
APPEND cl_gui_alv_grid=>mc_mb_paste TO it_excl_func.
APPEND cl_gui_alv_grid=>mc_fc_find TO it_excl_func.
APPEND cl_gui_alv_grid=>mc_fc_loc_copy TO it_excl_func.
*Alv display for the T006 table at the bottom
CALL METHOD cont_alvgd->set_table_for_first_display
CHANGING
it_outtab = it_t006[]
it_fieldcatalog = it_fielcat[].
* optimize column width of grid displaying fieldcatalog
it_layout-cwidth_opt = 'X'.
* Get fieldcatalog of table T006 - alv might have
* modified it after passing.
CALL METHOD cont_alvgd->get_frontend_fieldcatalog
IMPORTING
et_fieldcatalog = it_fielcat[].
*to Send Buffered Automation Queue to Frontend
  CALL METHOD cl_gui_cfw=>flush.
* Display fieldcatalog of table T006 in editable alv grid
CALL METHOD cont_editalvgd->set_table_for_first_display
EXPORTING
is_layout = it_layout
it_toolbar_excluding = it_excl_func
CHANGING
it_outtab = it_fielcat[]
it_fieldcatalog = it_fielcatalogue[].
ENDFORM.                               " CREATE_alv
*&---------------------------------------------------------------------*
*& Form fetch_data
*&---------------------------------------------------------------------*
FORM fetch_data.
* select data of T006
SELECT * FROM t006 INTO TABLE it_t006 UP TO 50 ROWS.
ENDFORM.                               " fetch_data
*&---------------------------------------------------------------------*
*& Form BUILD_FIELDCAT
*&---------------------------------------------------------------------*
FORM build_fieldcat CHANGING it_fldcat TYPE lvc_t_fcat
it_fcat TYPE lvc_t_fcat.
* Fieldcatalog for table T006: it_fldcat
* to generate the fields automatically
  CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
EXPORTING
i_structure_name = 'T006'
CHANGING
ct_fieldcat = it_fldcat[]
EXCEPTIONS
inconsistent_interface = 1
program_error = 2
OTHERS = 3.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
*----------------------------------------------------
* Fieldcatalog for table LVC_T_FCAT:it_fcat
*----------------------------------------------------
* Generate fieldcatalog of fieldcatalog structure.
* This fieldcatalog is used to display fieldcatalog 'it_fldcat'
* on the top of the screen.
  CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
EXPORTING
i_structure_name = 'LVC_S_FCAT'
CHANGING
ct_fieldcat = it_fcat[]
EXCEPTIONS
inconsistent_interface = 1
program_error = 2
OTHERS = 3.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
ENDFORM.                               " BUILD_FIELDCAT
*&---------------------------------------------------------------------*
*& Form CHANGE_FIELDCAT
*&---------------------------------------------------------------------*
*after the user has modified the fieldcatalogue we build another fieldcat
*for the modified alv display
FORM change_fieldcat CHANGING it_fcat TYPE lvc_t_fcat.
  DATA ls_fcat TYPE lvc_s_fcat.
  LOOP AT it_fcat INTO ls_fcat.
ls_fcat-coltext = ls_fcat-fieldname.
ls_fcat-edit = 'X'.
    IF ls_fcat-fieldname = 'COL_POS' OR ls_fcat-fieldname = 'FIELDNAME'.
ls_fcat-key = 'X'.
ENDIF.
    MODIFY it_fcat FROM ls_fcat.
ENDLOOP.
ENDFORM.                               " CHANGE_FIELDCAT

Output:

For the display we don’t consider the field MANDT, its just meant for the purpose of display in the editable ALV grid.

Now we change the field catalogue in the editable ALV by deleting some fields of the table ‘T006’ and changing the column position as shown below.

Now when the ‘SUBMIT’ button is clicked, the output gets modified as shown here.

No comments:

Tutorials on SAP-ABAP

Adobe Interactive Forms Tutorials

Business Server Pages (BSP)

Userexits/BADIs

Web Dynpro for ABAP (Step by step procedure for web dynpro,Tutorials on Web Dynpro,)

ALV Tutorials

Blog Archive

goodsites