Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Selection Sequence in ALV

Former Member
0 Likes
547

I have an ALV that displays a list, each row of the list has a Checkbox so is possible to select specific rows for further processing, my question is:

How can I get the selected rows in the exact sequence in which were selected?

1 ACCEPTED SOLUTION
Read only

uwe_schieferstein
Active Contributor
0 Likes
466

Hello

There is a simple and a more elaborate answer to your question:

<i>Simple answer: </i>You cannot. The ALV grid has no kind of "memory" which would store this information.

<i>Elaborate answer:</i> You can, but you have to implement the logic yourself. You may use sample report <b>ZUS_SDN_ALVGRID_EDITABLE_9</b> as template. Please note that I did not elaborate the logic when a checkbox is <b><u>de</u></b>selected.

*&---------------------------------------------------------------------*
*& Report  ZUS_SDN_ALVGRID_EDITABLE_9
*&
*& Description: store sequence of marked checkboxes
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  zus_sdn_alvgrid_editable_9.


TYPE-POOLS: abap.


DATA:
  gd_repid         TYPE syrepid,
  gd_okcode        TYPE ui_func,
*
  gs_layout        TYPE lvc_s_layo,
  gt_fcat          TYPE lvc_t_fcat,
  go_docking       TYPE REF TO cl_gui_docking_container,
  go_grid          TYPE REF TO cl_gui_alv_grid.


TYPES: BEGIN OF ty_s_outtab.
INCLUDE TYPE knb1.
TYPES: END OF ty_s_outtab.
TYPES: ty_t_outtab    TYPE STANDARD TABLE OF ty_s_outtab
                      WITH DEFAULT KEY.

DATA:
  gt_outtab        TYPE ty_t_outtab,
  gt_outtab_mark   TYPE ty_t_outtab,
  gt_marked_rows   TYPE lvc_t_row.



*---------------------------------------------------------------------*
*       CLASS lcl_eventhandler DEFINITION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_eventhandler DEFINITION.

  PUBLIC SECTION.

    CLASS-METHODS:
      handle_data_changed
        FOR EVENT data_changed OF cl_gui_alv_grid
        IMPORTING
          er_data_changed
          e_onf4
          e_onf4_before
          e_onf4_after
          e_ucomm
          sender,

      handle_hotspot_click FOR EVENT hotspot_click OF cl_gui_alv_grid
        IMPORTING
          e_row_id
          e_column_id
          es_row_no
          sender.  " grid instance that raised the event




ENDCLASS.                    "lcl_eventhandler DEFINITION


*---------------------------------------------------------------------*
*       CLASS lcl_eventhandler IMPLEMENTATION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_eventhandler IMPLEMENTATION.

  method handle_data_changed.

  endmethod.

  METHOD handle_hotspot_click.
*   define local data
    DATA:
      ld_msg        TYPE bapi_msg,
      ls_outtab     TYPE ty_s_outtab,
      ls_col_id     TYPE lvc_s_col.

    READ TABLE gt_outtab INTO ls_outtab INDEX e_row_id-index.
    IF ( ls_outtab-loevm = abap_false ).
      ls_outtab-loevm = abap_true.
      modify gt_outtab from ls_outtab index e_row_id-index.

      APPEND ls_outtab TO gt_outtab_mark.
      APPEND e_row_id TO gt_marked_rows.

      WRITE e_row_id-index TO ld_msg NO-ZERO.
      CONDENSE ld_msg NO-GAPS.
      CONCATENATE 'Row' ld_msg 'selected'
        INTO ld_msg
        SEPARATED BY space.

      MESSAGE ld_msg TYPE 'S'.

    ELSE.
      ls_outtab-loevm = abap_false.
      modify gt_outtab from ls_outtab index e_row_id-index.

      DELETE gt_outtab_mark WHERE ( kunnr = ls_outtab-kunnr
                            AND     bukrs = ls_outtab-bukrs ).
    ENDIF.


    CALL METHOD cl_gui_cfw=>set_new_ok_code
      EXPORTING
        new_code = 'DUMMY'  " trigger PAI
*      IMPORTING
*        RC       =
        .



  ENDMETHOD.                    "handle_hotspot_click

ENDCLASS.                    "lcl_eventhandler IMPLEMENTATION




START-OF-SELECTION.


  SELECT * FROM knb1 INTO TABLE gt_outtab UP TO 20 ROWS
    WHERE bukrs = '1000'.

  REFRESH: gt_outtab_mark.



* Create docking container
  CREATE OBJECT go_docking
    EXPORTING
      parent                      = cl_gui_container=>screen0
      ratio                       = 90
    EXCEPTIONS
      OTHERS                      = 6.
  IF sy-subrc <> 0.
*   MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*              WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.


* Create ALV grid
  CREATE OBJECT go_grid
    EXPORTING
      i_parent          = go_docking
    EXCEPTIONS
      OTHERS            = 5.
  IF sy-subrc <> 0.
*   MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*              WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.



* Build fieldcatalog
  PERFORM build_fieldcatalog.

  PERFORM set_layout.

  SET HANDLER:
    lcl_eventhandler=>handle_data_changed  for go_grid,
    lcl_eventhandler=>handle_hotspot_click FOR go_grid.



* Display data
  CALL METHOD go_grid->set_table_for_first_display
    EXPORTING
      is_layout       = gs_layout
    CHANGING
      it_outtab       = gt_outtab
      it_fieldcatalog = gt_fcat
    EXCEPTIONS
      OTHERS          = 4.
  IF sy-subrc <> 0.
*   MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*              WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.



  CALL METHOD go_grid->register_edit_event
    EXPORTING
      i_event_id = cl_gui_alv_grid=>mc_evt_enter
    EXCEPTIONS
      error      = 1
      OTHERS     = 2.
  IF sy-subrc <> 0.
*   MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*              WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.


* Link the docking container to the target dynpro
  gd_repid = syst-repid.
  CALL METHOD go_docking->link
    EXPORTING
      repid                       = gd_repid
      dynnr                       = '0100'
*      CONTAINER                   =
    EXCEPTIONS
      OTHERS                      = 4.
  IF sy-subrc <> 0.
*   MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*              WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.


* ok-code field = GD_OKCODE
  CALL SCREEN '0100'.
* Flow logic (no elements on screen):
*  PROCESS BEFORE OUTPUT.
*    MODULE STATUS_0100.
**
*  PROCESS AFTER INPUT.
*    MODULE USER_COMMAND_0100.


END-OF-SELECTION.

*&---------------------------------------------------------------------*
*&      Module  STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
  SET PF-STATUS 'STATUS_0100'.
*  SET TITLEBAR 'xxx'.

  CALL METHOD go_grid->refresh_table_display
*      EXPORTING
*        IS_STABLE      =
*        I_SOFT_REFRESH =
    EXCEPTIONS
      finished       = 1
      OTHERS         = 2.
  IF sy-subrc <> 0.
*     MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*                WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.

ENDMODULE.                 " STATUS_0100  OUTPUT

*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE user_command_0100 INPUT.

  TRANSLATE gd_okcode TO UPPER CASE.

* Fetch changes on ALV grid
  go_grid->check_changed_data( ).

  CASE gd_okcode.
    WHEN 'BACK' OR
         'END'  OR
         'CANC'.
      SET SCREEN 0. LEAVE SCREEN.



    WHEN 'DISPLAY'.
      PERFORM display_data.

    WHEN OTHERS.
  ENDCASE.





  CLEAR: gd_okcode.

ENDMODULE.                 " USER_COMMAND_0100  INPUT


*&---------------------------------------------------------------------*
*&      Form  BUILD_FIELDCATALOG_KNB1
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM build_fieldcatalog .
* define local data
  DATA:
    ls_fcat        TYPE lvc_s_fcat.



  CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
    EXPORTING
*     I_BUFFER_ACTIVE              =
      i_structure_name             = 'KNB1'
*     I_CLIENT_NEVER_DISPLAY       = 'X'
*     I_BYPASSING_BUFFER           =
*     I_INTERNAL_TABNAME           =
    CHANGING
      ct_fieldcat                  = gt_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.




  " Set required fields editable
  LOOP AT gt_fcat INTO ls_fcat
                  WHERE ( fieldname = 'LOEVM' ).
    ls_fcat-edit     = abap_true.
    ls_fcat-checkbox = abap_true.
    ls_fcat-hotspot  = abap_true.


    MODIFY gt_fcat FROM ls_fcat.
  ENDLOOP.

  DELETE gt_fcat WHERE ( fieldname = 'ZINRT' ).


ENDFORM.                    " BUILD_FIELDCATALOG


*&---------------------------------------------------------------------*
*&      Form  SET_LAYOUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM set_layout .

  CLEAR: gs_layout.

  gs_layout-cwidth_opt = abap_true.
  gs_layout-zebra      = abap_true.

**  gs_layout-stylefname = 'CELLTAB'.

ENDFORM.                    " SET_LAYOUT




*&---------------------------------------------------------------------*
*&      Form  SAVE_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM display_data .
* define local data






  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
    EXPORTING
      i_structure_name = 'LVC_S_ROW'
      i_grid_title     = 'Selected Rows'
    TABLES
      t_outtab         = gt_marked_rows
    EXCEPTIONS
      program_error    = 1
      OTHERS           = 2.
  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.                    " DISPLAY_DATA

Regards,

Uwe

2 REPLIES 2
Read only

uwe_schieferstein
Active Contributor
0 Likes
467

Hello

There is a simple and a more elaborate answer to your question:

<i>Simple answer: </i>You cannot. The ALV grid has no kind of "memory" which would store this information.

<i>Elaborate answer:</i> You can, but you have to implement the logic yourself. You may use sample report <b>ZUS_SDN_ALVGRID_EDITABLE_9</b> as template. Please note that I did not elaborate the logic when a checkbox is <b><u>de</u></b>selected.

*&---------------------------------------------------------------------*
*& Report  ZUS_SDN_ALVGRID_EDITABLE_9
*&
*& Description: store sequence of marked checkboxes
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  zus_sdn_alvgrid_editable_9.


TYPE-POOLS: abap.


DATA:
  gd_repid         TYPE syrepid,
  gd_okcode        TYPE ui_func,
*
  gs_layout        TYPE lvc_s_layo,
  gt_fcat          TYPE lvc_t_fcat,
  go_docking       TYPE REF TO cl_gui_docking_container,
  go_grid          TYPE REF TO cl_gui_alv_grid.


TYPES: BEGIN OF ty_s_outtab.
INCLUDE TYPE knb1.
TYPES: END OF ty_s_outtab.
TYPES: ty_t_outtab    TYPE STANDARD TABLE OF ty_s_outtab
                      WITH DEFAULT KEY.

DATA:
  gt_outtab        TYPE ty_t_outtab,
  gt_outtab_mark   TYPE ty_t_outtab,
  gt_marked_rows   TYPE lvc_t_row.



*---------------------------------------------------------------------*
*       CLASS lcl_eventhandler DEFINITION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_eventhandler DEFINITION.

  PUBLIC SECTION.

    CLASS-METHODS:
      handle_data_changed
        FOR EVENT data_changed OF cl_gui_alv_grid
        IMPORTING
          er_data_changed
          e_onf4
          e_onf4_before
          e_onf4_after
          e_ucomm
          sender,

      handle_hotspot_click FOR EVENT hotspot_click OF cl_gui_alv_grid
        IMPORTING
          e_row_id
          e_column_id
          es_row_no
          sender.  " grid instance that raised the event




ENDCLASS.                    "lcl_eventhandler DEFINITION


*---------------------------------------------------------------------*
*       CLASS lcl_eventhandler IMPLEMENTATION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_eventhandler IMPLEMENTATION.

  method handle_data_changed.

  endmethod.

  METHOD handle_hotspot_click.
*   define local data
    DATA:
      ld_msg        TYPE bapi_msg,
      ls_outtab     TYPE ty_s_outtab,
      ls_col_id     TYPE lvc_s_col.

    READ TABLE gt_outtab INTO ls_outtab INDEX e_row_id-index.
    IF ( ls_outtab-loevm = abap_false ).
      ls_outtab-loevm = abap_true.
      modify gt_outtab from ls_outtab index e_row_id-index.

      APPEND ls_outtab TO gt_outtab_mark.
      APPEND e_row_id TO gt_marked_rows.

      WRITE e_row_id-index TO ld_msg NO-ZERO.
      CONDENSE ld_msg NO-GAPS.
      CONCATENATE 'Row' ld_msg 'selected'
        INTO ld_msg
        SEPARATED BY space.

      MESSAGE ld_msg TYPE 'S'.

    ELSE.
      ls_outtab-loevm = abap_false.
      modify gt_outtab from ls_outtab index e_row_id-index.

      DELETE gt_outtab_mark WHERE ( kunnr = ls_outtab-kunnr
                            AND     bukrs = ls_outtab-bukrs ).
    ENDIF.


    CALL METHOD cl_gui_cfw=>set_new_ok_code
      EXPORTING
        new_code = 'DUMMY'  " trigger PAI
*      IMPORTING
*        RC       =
        .



  ENDMETHOD.                    "handle_hotspot_click

ENDCLASS.                    "lcl_eventhandler IMPLEMENTATION




START-OF-SELECTION.


  SELECT * FROM knb1 INTO TABLE gt_outtab UP TO 20 ROWS
    WHERE bukrs = '1000'.

  REFRESH: gt_outtab_mark.



* Create docking container
  CREATE OBJECT go_docking
    EXPORTING
      parent                      = cl_gui_container=>screen0
      ratio                       = 90
    EXCEPTIONS
      OTHERS                      = 6.
  IF sy-subrc <> 0.
*   MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*              WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.


* Create ALV grid
  CREATE OBJECT go_grid
    EXPORTING
      i_parent          = go_docking
    EXCEPTIONS
      OTHERS            = 5.
  IF sy-subrc <> 0.
*   MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*              WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.



* Build fieldcatalog
  PERFORM build_fieldcatalog.

  PERFORM set_layout.

  SET HANDLER:
    lcl_eventhandler=>handle_data_changed  for go_grid,
    lcl_eventhandler=>handle_hotspot_click FOR go_grid.



* Display data
  CALL METHOD go_grid->set_table_for_first_display
    EXPORTING
      is_layout       = gs_layout
    CHANGING
      it_outtab       = gt_outtab
      it_fieldcatalog = gt_fcat
    EXCEPTIONS
      OTHERS          = 4.
  IF sy-subrc <> 0.
*   MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*              WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.



  CALL METHOD go_grid->register_edit_event
    EXPORTING
      i_event_id = cl_gui_alv_grid=>mc_evt_enter
    EXCEPTIONS
      error      = 1
      OTHERS     = 2.
  IF sy-subrc <> 0.
*   MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*              WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.


* Link the docking container to the target dynpro
  gd_repid = syst-repid.
  CALL METHOD go_docking->link
    EXPORTING
      repid                       = gd_repid
      dynnr                       = '0100'
*      CONTAINER                   =
    EXCEPTIONS
      OTHERS                      = 4.
  IF sy-subrc <> 0.
*   MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*              WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.


* ok-code field = GD_OKCODE
  CALL SCREEN '0100'.
* Flow logic (no elements on screen):
*  PROCESS BEFORE OUTPUT.
*    MODULE STATUS_0100.
**
*  PROCESS AFTER INPUT.
*    MODULE USER_COMMAND_0100.


END-OF-SELECTION.

*&---------------------------------------------------------------------*
*&      Module  STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
  SET PF-STATUS 'STATUS_0100'.
*  SET TITLEBAR 'xxx'.

  CALL METHOD go_grid->refresh_table_display
*      EXPORTING
*        IS_STABLE      =
*        I_SOFT_REFRESH =
    EXCEPTIONS
      finished       = 1
      OTHERS         = 2.
  IF sy-subrc <> 0.
*     MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*                WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.

ENDMODULE.                 " STATUS_0100  OUTPUT

*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE user_command_0100 INPUT.

  TRANSLATE gd_okcode TO UPPER CASE.

* Fetch changes on ALV grid
  go_grid->check_changed_data( ).

  CASE gd_okcode.
    WHEN 'BACK' OR
         'END'  OR
         'CANC'.
      SET SCREEN 0. LEAVE SCREEN.



    WHEN 'DISPLAY'.
      PERFORM display_data.

    WHEN OTHERS.
  ENDCASE.





  CLEAR: gd_okcode.

ENDMODULE.                 " USER_COMMAND_0100  INPUT


*&---------------------------------------------------------------------*
*&      Form  BUILD_FIELDCATALOG_KNB1
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM build_fieldcatalog .
* define local data
  DATA:
    ls_fcat        TYPE lvc_s_fcat.



  CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
    EXPORTING
*     I_BUFFER_ACTIVE              =
      i_structure_name             = 'KNB1'
*     I_CLIENT_NEVER_DISPLAY       = 'X'
*     I_BYPASSING_BUFFER           =
*     I_INTERNAL_TABNAME           =
    CHANGING
      ct_fieldcat                  = gt_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.




  " Set required fields editable
  LOOP AT gt_fcat INTO ls_fcat
                  WHERE ( fieldname = 'LOEVM' ).
    ls_fcat-edit     = abap_true.
    ls_fcat-checkbox = abap_true.
    ls_fcat-hotspot  = abap_true.


    MODIFY gt_fcat FROM ls_fcat.
  ENDLOOP.

  DELETE gt_fcat WHERE ( fieldname = 'ZINRT' ).


ENDFORM.                    " BUILD_FIELDCATALOG


*&---------------------------------------------------------------------*
*&      Form  SET_LAYOUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM set_layout .

  CLEAR: gs_layout.

  gs_layout-cwidth_opt = abap_true.
  gs_layout-zebra      = abap_true.

**  gs_layout-stylefname = 'CELLTAB'.

ENDFORM.                    " SET_LAYOUT




*&---------------------------------------------------------------------*
*&      Form  SAVE_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM display_data .
* define local data






  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
    EXPORTING
      i_structure_name = 'LVC_S_ROW'
      i_grid_title     = 'Selected Rows'
    TABLES
      t_outtab         = gt_marked_rows
    EXCEPTIONS
      program_error    = 1
      OTHERS           = 2.
  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.                    " DISPLAY_DATA

Regards,

Uwe

Read only

Former Member
0 Likes
466

Hi,

Instead of using checkbox, why don't you use the standard button on the left of the ALV, and use method Get Selected Rows for further processing instead?