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

Insert a row in ALV

former_member314455
Participant
0 Likes
19,319

Hi ,

I need to insert a row in ALV output. On click of a Inser Row button a pop up should show with a parameter.Once the value in entered in the parameter enter is hit , the first column of the new row should have value put in that parameter as non editable field.

The Insert row of standard toolbar inserts a blank row . Is there any way to incorporate the above logic in the standard toolbar?

I'm using CL_GUI_ALV_GRID to display the ALV.

It would be helpful if you provide sample coding.

Thanks.

Ajith

Edited by: Ajith Krishna on Oct 28, 2008 8:01 PM

Hi ,

I need to insert a row in ALV output. On click of a Inser Row button a pop up should show with a parameter.Once the value in entered in the parameter enter is hit , the first column of the new row should have value put in that parameter as non editable field.

The Insert row of standard toolbar inserts a blank row . Is there any way to incorporate the above logic in the standard toolbar?

I'm using CL_GUI_ALV_GRID to display the ALV.

It would be helpful if you provide sample coding.

Thanks.

Ajith

Edited by: Ajith Krishna on Oct 28, 2008 8:01 PM

6 REPLIES 6
Read only

Former Member
0 Likes
6,095

Check sample programs :

BCALV_EDIT_01 to BCALV_EDIT_08 in the package SLIS.

They will solve all your queries.

regards,

Advait

Read only

Former Member
0 Likes
6,095

Hi

U need to deactive the standard function for inserting a new row of toolbar and create an your own buttom.

Max

Read only

0 Likes
6,095

Max,

Can you give me an example on the same.

Thanks

Read only

uwe_schieferstein
Active Contributor
0 Likes
6,095

Hello Ajith

The enhanced version of sample report ZUS_SDN_ALVGRID_EDITABLE_8A demonstrates how to implement your requirement.


*&---------------------------------------------------------------------*
*& ZUS_SDN_ALVGRID_EDITABLE_8A
*&
*&---------------------------------------------------------------------*
*& Thread: Insert a row in ALV
*& <a class="jive_macro jive_macro_thread" href="" __jive_macro_name="thread" modifiedtitle="true" __default_attr="1105097"></a>
*&
*& Thread: Blanking values on ALV Grid Row Duplicate
*& <a class="jive_macro jive_macro_thread" href="" __jive_macro_name="thread" modifiedtitle="true" __default_attr="1057161"></a>
*&
*& Thread: Delete line event in ALV
*& <a class="jive_macro jive_macro_thread" href="" __jive_macro_name="thread" modifiedtitle="true" __default_attr="945471"></a>
*&---------------------------------------------------------------------*

REPORT  zus_sdn_alvgrid_editable_8a.

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:
  gd_okcode        TYPE ui_func,
  gd_repid         TYPE syst-repid,
*
  gt_fcat          TYPE lvc_t_fcat,
  go_docking       TYPE REF TO cl_gui_docking_container,
  go_grid          TYPE REF TO cl_gui_alv_grid.


DATA:
  gt_outtab        TYPE ty_t_outtab.



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

  PUBLIC SECTION.
    CLASS-DATA:
      mt_sel_rows     TYPE lvc_t_row.

    CLASS-METHODS:
      handle_toolbar
        FOR EVENT toolbar OF cl_gui_alv_grid
        IMPORTING
          e_object
          sender,

      handle_user_command
        FOR EVENT user_command OF cl_gui_alv_grid
        IMPORTING
          e_ucomm
          sender.

ENDCLASS.                    "lcl_eventhandler DEFINITION


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

  METHOD handle_toolbar.
* define local data
    DATA: ls_button     TYPE stb_button.

    LOOP AT e_object->mt_toolbar INTO ls_button.
      CASE ls_button-function.
        when cl_gui_alv_grid=>MC_FC_LOC_INSERT_ROW.
          ls_button-function = 'INSERT_ROW'.
          MODIFY e_object->mt_toolbar FROM ls_button INDEX syst-tabix.

        WHEN cl_gui_alv_grid=>mc_fc_loc_delete_row.
          ls_button-function = 'DELETE_ROW'.
          MODIFY e_object->mt_toolbar FROM ls_button INDEX syst-tabix.

        WHEN cl_gui_alv_grid=>mc_fc_loc_copy_row OR
             cl_gui_alv_grid=>mc_fc_loc_copy.
          ls_button-function = 'COPY_ROW'.
          MODIFY e_object->mt_toolbar FROM ls_button INDEX syst-tabix.
        WHEN OTHERS.
          CONTINUE.
      ENDCASE.

    ENDLOOP.

  ENDMETHOD.                    "handle_toolbar

  METHOD handle_user_command.
* define local data
    DATA: lt_rows       TYPE lvc_t_row,
          ls_row        TYPE lvc_s_row.


    REFRESH: mt_sel_rows.

    CASE e_ucomm.
      when 'INSERT_ROW'.

      WHEN 'DELETE_ROW'.

      WHEN 'COPY_ROW'.

      WHEN OTHERS.
        RETURN.
    ENDCASE.


    "   User wants to delete or copy rows => store them in class attribute
    "   and trigger PAI afterwards where we actually delete /copy the rows
    "   and do the recalculations (in case of deletion)
    CALL METHOD sender->get_selected_rows
      IMPORTING
        et_index_rows = mt_sel_rows
*        et_row_no     =
        .


*   Trigger PAI
    CALL METHOD cl_gui_cfw=>set_new_ok_code
      EXPORTING
        new_code = e_ucomm
*      IMPORTING
*        rc       =
        .

  ENDMETHOD.                    "user_command

ENDCLASS.                    "lcl_eventhandler IMPLEMENTATION



PARAMETERS:
  p_bukrs      TYPE bukrs  DEFAULT '2000'  OBLIGATORY.




START-OF-SELECTION.

  SELECT  * FROM  knb1 INTO CORRESPONDING FIELDS OF TABLE gt_outtab
         UP TO 15 ROWS
         WHERE  bukrs  = p_bukrs.


  PERFORM init_controls.

  SET HANDLER:
    lcl_eventhandler=>handle_toolbar      FOR go_grid,
    lcl_eventhandler=>handle_user_command FOR go_grid.

  " Used to replace standard toolbar function code with custom FC
  go_grid->set_toolbar_interactive( ).


* 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'.


END-OF-SELECTION.

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

**      CALL METHOD go_grid1->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.

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

    WHEN 'INSERT_ROW'.
      perform INSERT_ROW.

    WHEN 'DELETE_ROW'.
      PERFORM delete_rows.

    WHEN 'COPY_ROW'.
      PERFORM copy_rows.


    WHEN OTHERS.
  ENDCASE.

  CLEAR: gd_okcode.

  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.                 " USER_COMMAND_0100  INPUT




*&---------------------------------------------------------------------*
*&      Form  BUILD_FIELDCATALOG_KNB1
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM build_fieldcatalog_knb1 .
* 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.


* Only non-key fields are editable
  ls_fcat-edit = 'X'.
  MODIFY gt_fcat FROM ls_fcat
    TRANSPORTING edit
    WHERE ( key NE 'X' ).


ENDFORM.                    " BUILD_FIELDCATALOG_KNB1


*&---------------------------------------------------------------------*
*&      Form  INIT_CONTROLS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM init_controls .

* 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 and set hotspot for field KUNNR
  PERFORM build_fieldcatalog_knb1.



* Display data
  CALL METHOD go_grid->set_table_for_first_display
    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.


ENDFORM.                    " INIT_CONTROLS


*&---------------------------------------------------------------------*
*&      Form  delete_rows
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM delete_rows .
* define local data
  DATA: ls_row    TYPE lvc_s_row.

  SORT lcl_eventhandler=>mt_sel_rows BY index DESCENDING. " !!!


  LOOP AT lcl_eventhandler=>mt_sel_rows INTO ls_row.
    DELETE gt_outtab INDEX ls_row-index.
  ENDLOOP.

  " After deleting rows do RE-CALCULATION
*  perform RECALCULATION.

ENDFORM.                    " delete_rows


*&---------------------------------------------------------------------*
*&      Form  COPY_ROWS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM copy_rows .
* define local data
  DATA: ld_next   TYPE i,
        ls_row    TYPE lvc_s_row,
        ls_outtab TYPE ty_s_outtab.


  SORT lcl_eventhandler=>mt_sel_rows BY index DESCENDING. " !!!

  LOOP AT lcl_eventhandler=>mt_sel_rows INTO ls_row.
    READ TABLE gt_outtab INTO ls_outtab INDEX ls_row-index.

    CLEAR: ls_outtab-akont. " In your case: clear GUID
    ld_next = ls_row-index + 1.
    INSERT ls_outtab INTO gt_outtab INDEX ld_next.
  ENDLOOP.

ENDFORM.                    " COPY_ROWS


*&---------------------------------------------------------------------*
*&      Form  INSERT_ROW
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form INSERT_ROW .
* define local data
  DATA: ld_value1  type SPOP-VARVALUE1,
        ls_outtab  TYPE ty_s_outtab.

  CALL FUNCTION 'POPUP_TO_GET_ONE_VALUE'
    EXPORTING
      textline1            = 'Enter Value (4 Chars):'
*     TEXTLINE2            = ' '
*     TEXTLINE3            = ' '
      titel                = 'Enter Value'
      valuelength          = 4
    IMPORTING
*     ANSWER               =
      VALUE1               = ld_value1
    EXCEPTIONS
      TITEL_TOO_LONG       = 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.

  ls_outtab-kunnr = ld_value1.
  ls_outtab-bukrs = ld_value1.

  append ls_outtab to gt_outtab.

endform.                    " INSERT_ROW

Regards

Uwe

Read only

Former Member
0 Likes
6,095

Hi,

Hope the following Threads will help you regarding your problem.

https://www.sdn.sap.com/irj/sdn/advancedsearch?query=insertarowinALV&cat=sdn_all

Thanks.

Nitesh

Read only

former_member314455
Participant
0 Likes
6,095

Solved