Tuesday, February 24, 2009

Check box in ALV Grid (Select multiple lines for processing in ALV)

LVLVDuring interactive reporting, it is required to selected more than one line in the output ALV. After selecting multiple lines we might have to process the selection.

For example; The first ALV has the sales order header data. We might have to select more than one line from the Sales order header list. After selecting the lines, when we click a button, it should show the item details of the selected sales orders.




The following code shows how to achieve this.

REPORT znstest.
************************************************************************
* Written by : Saravanan Natarajan *
* Purpose : Enable multiple line selection in ALV grid didsplay. *
* To show checkbox button in front of every line in *
* ALV Grid display. After selectiing multiple lines *
* press a button and display the item level *
************************************************************************

TYPE-POOLS : slis.

TYPES : BEGIN OF t_header,
vbeln LIKE vbak-vbeln,
erdat LIKE vbak-erdat,
ernam LIKE vbak-ernam,
chkbox(1) TYPE c, "Checkbox field in ALV
END OF t_header.

TYPES : BEGIN OF t_item,
vbeln LIKE vbap-vbeln,
posnr LIKE vbap-posnr,
matnr LIKE vbap-matnr,
kwmeng LIKE vbap-kwmeng,
END OF t_item.

DATA : it_header TYPE STANDARD TABLE OF t_header,
it_item TYPE STANDARD TABLE OF t_item.


START-OF-SELECTION.

PERFORM get_data.

END-OF-SELECTION.

IF it_header IS NOT INITIAL.

PERFORM display_alv.

ENDIF.



*&---------------------------------------------------------------------*
*& Form get_data
*&---------------------------------------------------------------------*
* Select data from the VBAK table ( first 10 rows only )
*----------------------------------------------------------------------*
FORM get_data.

SELECT vbeln
erdat
ernam
FROM vbak
INTO TABLE it_header UP TO 10 ROWS.

IF sy-subrc IS INITIAL.
SORT it_header BY vbeln.
ENDIF.

ENDFORM. "get_data

*&---------------------------------------------------------------------*
*& Form display_alv
*&---------------------------------------------------------------------*
* To display the final output in the form of the ALV GRID report
*----------------------------------------------------------------------*
FORM display_alv.
*----------------------------------------------------------------------*
*-Local Internal table declaration
DATA : l_it_fcat TYPE slis_t_fieldcat_alv .

*-Local data declaration
DATA : l_layo TYPE slis_layout_alv .

*----------------------------------------------------------------------*

*-Build the field catalog used for displaying the ALV
PERFORM build_fieldcat CHANGING l_it_fcat.

*-set layout for ALV
*-inorder to show the checkbox button in every line, pass the checkbox
*-field name to the followingn field
l_layo-box_fieldname = 'CHKBOX'.

*-Display the data in ALV Grid

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = sy-repid
i_callback_pf_status_set = 'ALV_EVENT_PF_STATUS_SET'
i_callback_user_command = 'ALV_EVENT_USER_COMMAND'
is_layout = l_layo
it_fieldcat = l_it_fcat
i_default = 'X'
i_save = 'A'
TABLES
t_outtab = it_header
EXCEPTIONS
program_error = 1
OTHERS = 2.

IF NOT sy-subrc IS INITIAL.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

ENDFORM. "display_alv


*&---------------------------------------------------------------------*
*& Form build_fieldcat
*&---------------------------------------------------------------------*
* This subroutine is used to build field catalog
*----------------------------------------------------------------------*
* <--P_L_IT_FCAT - Contains the field catalog

*----------------------------------------------------------------------*

FORM build_fieldcat CHANGING p_l_it_fcat TYPE slis_t_fieldcat_alv. *----------------------------------------------------------------------* *-Local Data Declarations-* DATA: l_fcat TYPE slis_fieldcat_alv. *----------------------------------------------------------------------* CLEAR l_fcat. l_fcat-fieldname = 'VBELN'. l_fcat-col_pos = 0. l_fcat-seltext_l = 'Sales Order Number'. l_fcat-seltext_m = 'Order Number'. l_fcat-seltext_s = 'Order #'. l_fcat-outputlen = 12. APPEND l_fcat TO p_l_it_fcat. CLEAR l_fcat. l_fcat-fieldname = 'ERDAT'. l_fcat-col_pos = 1. l_fcat-seltext_l = 'Created on'. l_fcat-seltext_m = 'Created on'. l_fcat-seltext_s = 'Created on'. l_fcat-outputlen = 12. APPEND l_fcat TO p_l_it_fcat. CLEAR l_fcat. l_fcat-fieldname = 'ERNAM'. l_fcat-col_pos = 2. l_fcat-seltext_l = 'Created by'. l_fcat-seltext_m = 'Created by'. l_fcat-seltext_s = 'Created by'. l_fcat-outputlen = 30. APPEND l_fcat TO p_l_it_fcat. ENDFORM. "build_fieldcat *---------------------------------------------------------------------* * FORM alv_event_pf_status_set * *---------------------------------------------------------------------* *Passing an EXIT routine indicates to the ALV that the caller wants * *to set a self-defined user status. * *Set the PF status in this form which is called dynamically. * *---------------------------------------------------------------------* FORM alv_event_pf_status_set USING rt_extab TYPE slis_t_extab. "#EC * SET PF-STATUS 'STATUS_HEADER'. ENDFORM. "alv_event_pf_status_set *---------------------------------------------------------------------* * FORM ALV_EVENT_USER_COMMAND * *---------------------------------------------------------------------* * This subroutine is triggered during the user interaction in ALV * *---------------------------------------------------------------------* FORM alv_event_user_command USING r_ucomm LIKE sy-ucomm rs_selfield TYPE slis_selfield."#EC CALLED *---------------------------------------------------------------------* DATA : lt_header TYPE STANDARD TABLE OF t_header. *---------------------------------------------------------------------* CASE r_ucomm. *-Click the button after selecting the header lines WHEN '&ITM'. *-when the checkbox in front of the every ALV line is clicked. The *-CHKBOX field of the respective line is marked 'X'. REFRESH lt_header. lt_header = it_header. REFRESH it_item. DELETE lt_header WHERE chkbox NE 'X'. SELECT vbeln posnr matnr kwmeng FROM vbap INTO TABLE it_item FOR ALL ENTRIES IN lt_header WHERE vbeln EQ lt_header-vbeln. IF sy-subrc IS INITIAL. SORT it_item. PERFORM display_item. ENDIF. ENDCASE. ENDFORM. "alv_event_user_command *&---------------------------------------------------------------------* *& Form display_item *&---------------------------------------------------------------------* * To display the item level interactive ALV *----------------------------------------------------------------------* FORM display_item. *----------------------------------------------------------------------* *-Local Internal table declaration DATA : l_it_fcat TYPE slis_t_fieldcat_alv . *----------------------------------------------------------------------* *-Build the field catalog used for displaying the ALV PERFORM build_fieldcat_item CHANGING l_it_fcat. *-Display the data in ALV Grid CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY' EXPORTING i_callback_program = sy-repid it_fieldcat = l_it_fcat i_default = 'X' i_save = 'A' TABLES t_outtab = it_item EXCEPTIONS program_error = 1 OTHERS = 2. IF NOT sy-subrc IS INITIAL. MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. ENDIF. ENDFORM. "display_item *&---------------------------------------------------------------------* *& Form build_fieldcat *&---------------------------------------------------------------------* * This subroutine is used to build field catalog *----------------------------------------------------------------------* * <--P_L_IT_FCAT - Contains the field catalog *----------------------------------------------------------------------* FORM build_fieldcat_item CHANGING p_l_it_fcat TYPE slis_t_fieldcat_alv. *----------------------------------------------------------------------* *-Local Data Declarations-* DATA: l_fcat TYPE slis_fieldcat_alv. *----------------------------------------------------------------------* CLEAR l_fcat. l_fcat-fieldname = 'VBELN'. l_fcat-col_pos = 0. l_fcat-seltext_l = 'Sales Order Number'. l_fcat-seltext_m = 'Order Number'. l_fcat-seltext_s = 'Order #'. l_fcat-outputlen = 12. APPEND l_fcat TO p_l_it_fcat. CLEAR l_fcat. l_fcat-fieldname = 'POSNR'. l_fcat-col_pos = 1. l_fcat-seltext_l = 'Item #'. l_fcat-seltext_m = 'Item #'. l_fcat-seltext_s = 'Item #'. l_fcat-outputlen = 8. APPEND l_fcat TO p_l_it_fcat. CLEAR l_fcat. l_fcat-fieldname = 'MATNR'. l_fcat-col_pos = 2. l_fcat-seltext_l = 'Material'. l_fcat-seltext_m = 'Material'. l_fcat-seltext_s = 'Material'. l_fcat-outputlen = 20. APPEND l_fcat TO p_l_it_fcat. CLEAR l_fcat. l_fcat-fieldname = 'KWMENG'. l_fcat-col_pos = 2. l_fcat-seltext_l = 'Quantity'. l_fcat-seltext_m = 'Quantity'. l_fcat-seltext_s = 'Quantity'. l_fcat-outputlen = 10. APPEND l_fcat TO p_l_it_fcat. ENDFORM. "build_fieldcat_item

4 comments:

  1. DATA: gd_repid LIKE sy-repid, "Exists
    ref_grid TYPE REF TO cl_gui_alv_grid.


    IF ref_grid IS INITIAL.
    CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
    IMPORTING
    e_grid = ref_grid.
    ENDIF.

    IF NOT ref_grid IS INITIAL.
    CALL METHOD ref_grid->check_changed_data
    .
    ENDIF.

    ReplyDelete
    Replies
    1. mayank singh
      shakti pumps(i)
      indore

      Delete
  2. Respect and that i have a super give: How Much House Renovation Cost average cost to remodel a house

    ReplyDelete