cancel
Showing results for 
Search instead for 
Did you mean: 

How to create an alv program

Former Member
0 Kudos

Can any one tell me how to create an alv program,

and can any post a small or some examples,in alv grid.

How to use double click event in table control.

Where can i get an alv material. Means not the theory i need some examples. I have the theory material with me but without any syntax.

how to put name to the push button in the application tool bar when it is created with selection-screen command

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi,

check this site for code samples of different types of ALV:

http://www.sapdevelopment.co.uk/reporting/alvhome.htm

to add pushbutton to application tool bar:

http://www.sapdevelopment.co.uk/reporting/selscr/but_appbut.htm

Hope that helps:

Regards,

Anjali

Answers (11)

Answers (11)

Bema
Active Participant
0 Kudos
Former Member
0 Kudos

Hi,

Here is the SIMPLE ALV PROGRAM. Do it in SE38.

REPORT zsomalv1 NO STANDARD PAGE HEADING.

Description----


This program shows a simple ALV display of the table QMEL. Use the

  • 'Pattern' button to write the call to function

  • 'REUSE_ALV_GRID_DISPLAY'. Pass the table i_qmel to the function.

*----


DATA: i_qmel LIKE qmel OCCURS 0.

SELECT * FROM qmel INTO TABLE i_qmel.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

i_structure_name = 'QMEL'

TABLES

t_outtab = i_qmel

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.

Regs,

Venkat Ramanan

Former Member
0 Kudos

Hi

To use ALV u have to create reference variables to alv grid and container....after that u can create objects and can use their methods to fill data in alv....

while calling these methods u have to export an intyernal table having data and a field catalogue having the structure same as ur table.....

following is an example......

TABLES: ZP10.

*----


  • G L O B A L I N T E R N A L T A B L E S

  • ZSTRUCT is a structure being used ad field catalogue here.

*----


DATA:

ITAB LIKE TABLE OF ZSTRUCT.

*----


  • G L O B A L D A T A

*----


DATA: ok_code LIKE sy-ucomm,

g_wa_sale LIKE itab.

  • Declare reference variables to the ALV grid and the container

DATA:

go_grid TYPE REF TO cl_gui_alv_grid,

go_custom_container TYPE REF TO cl_gui_custom_container.

*----


  • S T A R T - O F - S E L E C T I O N.

*----


START-OF-SELECTION.

SET SCREEN '100'.

&----


*& Module USER_COMMAND_0100 INPUT

&----


MODULE user_command_0100 INPUT.

CASE ok_code.

WHEN 'EXIT'.

LEAVE PROGRAM.

ENDCASE.

ENDMODULE. " USER_COMMAND_0100 INPUT

&----


*& Module STATUS_0100 OUTPUT

&----


MODULE status_0100 OUTPUT.

  • Create objects

IF go_custom_container IS INITIAL.

CREATE OBJECT go_custom_container

EXPORTING container_name = 'ALV_CONTAINER'.

CREATE OBJECT go_grid

EXPORTING

i_parent = go_custom_container.

PERFORM load_data_into_grid.

ENDIF.

ENDMODULE. " STATUS_0100 OUTPUT

&----


*& Form load_data_into_grid

&----


FORM load_data_into_grid.

  • Read data from table Zp10

SELECT *

FROM ZP10

INTO TABLE ITAB.

  • Load data into the grid and display them

CALL METHOD go_grid->set_table_for_first_display

EXPORTING i_structure_name = 'ZSTRUCT'

CHANGING it_outtab = ITAB.

ENDFORM. " load_data_into_grid

***************************************************

if this helps u anyway, plz let me know.

regards,

Pragya

jayanthi_jayaraman
Active Contributor
0 Kudos

Hi,

Check this concept for OOPS ALV.

http://www.henrikfrank.dk/abapuk.html

Check this standard program for Classical ALV

BCALV_TEST_GRID_EVENTS

It explains all the events in ALV.

Former Member
0 Kudos

Hi,

U can go through these sample programs

http://www.sapgenie.com/abap/controls/alvgrid.htm

http://www.asug.com/client_files/Calendar/Upload/ASUG%20ALV%20Programming%20in%20ABAP%2006-24-2005.p...

http://www.sapdevelopment.co.uk/reporting/alv/alvgrid_basic.htm

Also u can search in code sample part in SDN to create a simple ALV program.

Goto Download > code sample > Webas >click download icon u can get lot of programs with detail flow how to create.

Hope this helps.

Note: If ur problem got solved kindly reward points to teh answers that helped u and close the thread.

Thanks & Regards,

Judith.

former_member181959
Contributor
0 Kudos

Hi anup,

You can display data using ALV’s function module like

REUSE_ALV_LIST_DISPLAY .

To handle the events use SLIS structure.

To get the information about the field on which the user has clicked use

GET CURSOR FIELD <fname> VALUE <fvalue >. in AT LINE-SELECTION event.

I hope it may help u.

With regards…

Prasad.

Former Member
0 Kudos

Hi,

Please check these code samples too

https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/com.sap.km.cm.docs/library/webas/abap/abap code samples/alv grid/abap code sample to display data in alv grid using object oriented programming.doc

https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/com.sap.km.cm.docs/library/webas/abap/abap code samples/alv grid/abap code sample to create alv grid from flat file.pdf

Let me know if that really helped you.

Also check the "Easy reference to ALV" -> https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/com.sap.km.cm.docs/library/webas/abap/an easy reference for alv grid control.pdf

Cheers,

Kathir~

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

Here is an example of an OO based ALV grid which uses a dynpro. This also has drill down capabilities.



REPORT ZRICH_0001.

TABLES: MARA.

DATA: BEGIN OF I_ALV OCCURS 0,
      MATNR TYPE MARA-MATNR,
      MAKTX TYPE MAKT-MAKTX,
      END OF I_ALV.

***********************************************************************
*       CLASS cl_event_receiver DEFINITION      Handles Double Click
***********************************************************************
CLASS CL_EVENT_RECEIVER DEFINITION.
  PUBLIC SECTION.
    METHODS HANDLE_DOUBLE_CLICK
      FOR EVENT DOUBLE_CLICK OF CL_GUI_ALV_GRID
      IMPORTING E_ROW E_COLUMN.
  PRIVATE SECTION.
ENDCLASS.

***********************************************************************
*       CLASS CL_EVENT_RECEIVER IMPLEMENTATION    Handles Double Click
***********************************************************************
CLASS CL_EVENT_RECEIVER IMPLEMENTATION.
  METHOD HANDLE_DOUBLE_CLICK.
    PERFORM DRILL_DOWN USING E_ROW-INDEX.
  ENDMETHOD.
ENDCLASS.


DATA: ALV_CONTAINER  TYPE REF TO CL_GUI_CUSTOM_CONTAINER.
DATA: EVENT_RECEIVER TYPE REF TO CL_EVENT_RECEIVER.
DATA: ALV_GRID       TYPE REF TO CL_GUI_ALV_GRID.
DATA: LAYOUT    TYPE LVC_S_LAYO.
DATA: FIELDCAT  TYPE LVC_T_FCAT.

SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-001 .
SELECT-OPTIONS: S_MATNR FOR MARA-MATNR.
SELECTION-SCREEN END OF BLOCK B1.

START-OF-SELECTION.

  PERFORM GET_DATA.
  CALL SCREEN 100.

************************************************************************
*      Module  status_0100  OUTPUT
************************************************************************
MODULE STATUS_0100 OUTPUT.
  SET PF-STATUS '0100'.
  SET TITLEBAR '0100'.

  DATA: LT_EXCLUDE TYPE UI_FUNCTIONS.
  DATA: VARIANT TYPE  DISVARIANT.

  VARIANT-REPORT = SY-REPID.
  VARIANT-USERNAME = SY-UNAME.

* Create Controls
  CREATE OBJECT ALV_CONTAINER
         EXPORTING
               CONTAINER_NAME    = 'ALV_CONTAINER'.

  CREATE OBJECT ALV_GRID
         EXPORTING
               I_PARENT          =  ALV_CONTAINER.

*  Create Event Receiver
  CREATE OBJECT EVENT_RECEIVER.

*  ALV Specific. Data selection.
*  Populate Field Catalog
  PERFORM GET_FIELDCATALOG.


*   Optionally restrict generic functions to 'change only'.
*   (The user shall not be able to add new lines).
  PERFORM EXCLUDE_TB_FUNCTIONS CHANGING LT_EXCLUDE.

  CALL METHOD ALV_GRID->SET_TABLE_FOR_FIRST_DISPLAY
      EXPORTING
           IS_LAYOUT              = LAYOUT
           IS_VARIANT             = VARIANT
           I_SAVE                 = 'U'
           I_STRUCTURE_NAME       = 'I_ALV'
           it_toolbar_excluding   = lt_exclude
      CHANGING
           IT_OUTTAB       = I_ALV[]
           IT_FIELDCATALOG = FIELDCAT[].

*   handler for ALV grid
  SET HANDLER EVENT_RECEIVER->HANDLE_DOUBLE_CLICK FOR ALV_GRID.

ENDMODULE.

************************************************************************
*      Module  USER_COMMAND_0100  INPUT
************************************************************************
MODULE USER_COMMAND_0100 INPUT.

  CASE SY-UCOMM.
    WHEN 'BACK' OR 'CANC'.
      IF NOT ALV_CONTAINER IS INITIAL.
        CALL METHOD ALV_CONTAINER->FREE.
        CLEAR: ALV_CONTAINER.
        FREE : ALV_CONTAINER.
      ENDIF.
      IF SY-SUBRC = 0.
        SET SCREEN 0.
        LEAVE SCREEN.
      ELSE.
        LEAVE PROGRAM.
      ENDIF.
    WHEN 'EXIT'.
      IF NOT ALV_CONTAINER IS INITIAL.
        CALL METHOD ALV_CONTAINER->FREE.
        CLEAR: ALV_CONTAINER.
        FREE : ALV_CONTAINER.
      ENDIF.
      LEAVE PROGRAM.
  ENDCASE.

ENDMODULE.

************************************************************************
* FORM GET_DATA
************************************************************************
FORM GET_DATA.

  SELECT * INTO CORRESPONDING FIELDS OF TABLE I_ALV
        FROM MARA
          INNER JOIN MAKT
            ON MARA~MATNR = MAKT~MATNR
               WHERE MARA~MATNR IN S_MATNR
                 AND MAKT~SPRAS = SY-LANGU.

  SORT I_ALV ASCENDING BY MATNR.

ENDFORM.

************************************************************************
*      Form  Get_Fieldcatalog - Set Up Columns/Headers
************************************************************************
FORM GET_FIELDCATALOG.

  DATA: LS_FCAT TYPE LVC_S_FCAT.
  REFRESH: FIELDCAT.

  CLEAR: LS_FCAT.
  LS_FCAT-REPTEXT    = 'Material Number'.
  LS_FCAT-COLTEXT    = 'Material Number'.
  LS_FCAT-FIELDNAME  = 'MATNR'.
  LS_FCAT-REF_TABLE  = 'I_ALV'.
  LS_FCAT-OUTPUTLEN  = '18'.
  LS_FCAT-COL_POS    = 1.
  APPEND LS_FCAT TO FIELDCAT.

  CLEAR: LS_FCAT.
  LS_FCAT-REPTEXT    = 'Material Description'.
  LS_FCAT-COLTEXT    = 'Material Description'.
  LS_FCAT-FIELDNAME  = 'MAKTX'.
  LS_FCAT-REF_TABLE  = 'I_ALV'.
  LS_FCAT-OUTPUTLEN  = '40'.
  LS_FCAT-COL_POS    = 2.
  APPEND LS_FCAT TO FIELDCAT.

ENDFORM.

************************************************************************
* DRILL_DOWN
************************************************************************
FORM DRILL_DOWN USING INDEX.

  READ TABLE I_ALV INDEX INDEX.
  IF SY-SUBRC = 0.
    SET PARAMETER ID 'MAT' FIELD I_ALV-MATNR.
    CALL TRANSACTION 'MM03' AND SKIP FIRST SCREEN.
    IF NOT ALV_CONTAINER IS INITIAL.
      CALL METHOD ALV_CONTAINER->FREE.
      CLEAR: ALV_CONTAINER.
      FREE : ALV_CONTAINER.
    ENDIF.
  ENDIF.

ENDFORM.

***********************************************************************
*      Form  EXCLUDE_TB_FUNCTIONS
***********************************************************************
FORM EXCLUDE_TB_FUNCTIONS CHANGING PT_EXCLUDE TYPE UI_FUNCTIONS.
* Only allow to change data not to create new entries (exclude
* generic functions).

  DATA LS_EXCLUDE TYPE UI_FUNC.

  LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_LOC_COPY_ROW.
  APPEND LS_EXCLUDE TO PT_EXCLUDE.
  LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_LOC_DELETE_ROW.
  APPEND LS_EXCLUDE TO PT_EXCLUDE.
  LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_LOC_APPEND_ROW.
  APPEND LS_EXCLUDE TO PT_EXCLUDE.
  LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_LOC_INSERT_ROW.
  APPEND LS_EXCLUDE TO PT_EXCLUDE.
  LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_LOC_MOVE_ROW.
  APPEND LS_EXCLUDE TO PT_EXCLUDE.

  LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_SORT_ASC.
  APPEND LS_EXCLUDE TO PT_EXCLUDE.

  LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_SORT_DSC.
  APPEND LS_EXCLUDE TO PT_EXCLUDE.


**  This excludes all buttons
*  LS_EXCLUDE = '&EXCLALLFC'.
*  APPEND LS_EXCLUDE TO PT_EXCLUDE.

ENDFORM.

Regards,

Rich Heilman

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

This is an example of the function module based ALV grid.



report zrich_0004
       no standard page heading.

type-pools slis.

data: fieldcat type slis_t_fieldcat_alv.

data: begin of imara occurs 0,
      matnr type mara-matnr,
      maktx type makt-maktx,
      end of imara.

* Selection Screen
selection-screen begin of block b1 with frame title text-001 .
select-options: s_matnr for imara-matnr .
selection-screen end of block b1.

start-of-selection.

  perform get_data.
  perform write_report.


************************************************************************
*  Get_Data
************************************************************************
form get_data.

  select  mara~matnr makt~maktx
            into corresponding fields of table imara
              from mara
               inner join makt
                 on mara~matnr = makt~matnr
                    where mara~matnr in s_matnr
                      and makt~spras = sy-langu.

endform.

************************************************************************
*  WRITE_REPORT
************************************************************************
form write_report.

  perform build_field_catalog.

* CALL ABAP LIST VIEWER (ALV)
  call function 'REUSE_ALV_GRID_DISPLAY'
       exporting
            it_fieldcat = fieldcat
       tables
            t_outtab    = imara.

endform.

************************************************************************
* BUILD_FIELD_CATALOG
************************************************************************
form build_field_catalog.

  data: fc_tmp type slis_t_fieldcat_alv with header line.
  clear: fieldcat. refresh: fieldcat.

  clear: fc_tmp.
  fc_tmp-reptext_ddic    = 'Material Number'.
  fc_tmp-fieldname  = 'MATNR'.
  fc_tmp-tabname   = 'IMARA'.
  fc_tmp-outputlen  = '18'.
  fc_tmp-col_pos    = 2.
  append fc_tmp to fieldcat.


  clear: fc_tmp.
  fc_tmp-reptext_ddic    = 'Material'.
  fc_tmp-fieldname  = 'MAKTX'.
  fc_tmp-tabname   = 'IMARA'.
  fc_tmp-outputlen  = '40'.
  fc_tmp-col_pos    = 3.
  append fc_tmp to fieldcat.

endform.

Regards,

Rich Heilman

former_member214131
Active Contributor
0 Kudos

Hello,

Check for report programs 'BCALV*'.

Perhaps 'BCALV_TEST_GRID_EVENTS' would help you.

Regards, Murugesh AS

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

This is an example of an ALV grid implemented in a secondary selection-screen. You don't have to code dynpro here.



report zrich_0001.

* Used to limit user commands on selection-screen
include rsdbc1xx.

data: begin of i_alv occurs 0,
      matnr type mara-matnr,
      maktx type makt-maktx,
      end of i_alv.

data: alv_grid       type ref to cl_gui_alv_grid.
data: fieldcat  type lvc_t_fcat.

selection-screen begin of block b1 with frame title text-001 .
select-options: s_matnr for i_alv-matnr.
selection-screen end of block b1.

selection-screen begin of screen 1010.
selection-screen end of screen 1010.

* Events
at selection-screen output.
  if sy-dynnr = '1010'.
    current_scr-mode = 'S'.
    append 'SPOS' to current_scr-excl.
    append 'SCRH' to current_scr-excl.
    append 'ONLI' to current_scr-excl.
  endif.

start-of-selection.

  perform get_data.

  create object alv_grid
         exporting
               i_parent           =  cl_gui_container=>screen0.

*  Populate Field Catalog
  perform get_fieldcatalog.

  call method alv_grid->set_table_for_first_display
      changing
           it_outtab              = i_alv[]
           it_fieldcatalog        = fieldcat[].

  call selection-screen 1010.

************************************************************************
* FORM GET_DATA
************************************************************************
form get_data.

  select * into corresponding fields of table i_alv
        from mara
          inner join makt
            on mara~matnr = makt~matnr
               where mara~matnr in s_matnr
                 and makt~spras = sy-langu.

  sort i_alv ascending by matnr.

endform.

************************************************************************
*      Form  Get_Fieldcatalog - Set Up Columns/Headers
************************************************************************
form get_fieldcatalog.

  data: ls_fcat type lvc_s_fcat.
  refresh: fieldcat.

  clear: ls_fcat.
  ls_fcat-reptext    = 'Material Number'.
  ls_fcat-coltext    = 'Material Number'.
  ls_fcat-fieldname  = 'MATNR'.
  ls_fcat-ref_table  = 'I_ALV'.
  ls_fcat-outputlen  = '18'.
  ls_fcat-col_pos    = 1.
  append ls_fcat to fieldcat.

  clear: ls_fcat.
  ls_fcat-reptext    = 'Material Description'.
  ls_fcat-coltext    = 'Material Description'.
  ls_fcat-fieldname  = 'MAKTX'.
  ls_fcat-ref_table  = 'I_ALV'.
  ls_fcat-outputlen  = '40'.
  ls_fcat-col_pos    = 2.
  append ls_fcat to fieldcat.

endform.

Regards,

Rich Heilman