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

simple ALV using oops example

Former Member
52,690

HI Experts

Im new to this forum . Can some one send a sample code to create a simple alv using oops.

Thanks and regards

Anish

1 ACCEPTED SOLUTION
Read only

RichHeilman
Developer Advocate
Developer Advocate
24,329

Here is a very simple example.

REPORT ZRICH_0006.

TABLES: MARA.

TYPE-POOLS: SLIS, ICON.

* Internal Tables
DATA: BEGIN OF IALV OCCURS 0,
      MATNR TYPE MARA-MATNR,
      MAKTX TYPE MAKT-MAKTX,
      END OF IALV .

DATA: ALV_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER,
      ALV_GRID TYPE REF TO CL_GUI_ALV_GRID,
      OK_CODE LIKE SY-UCOMM,
      FIELDCAT TYPE LVC_T_FCAT.

SELECT-OPTIONS: S_MATNR FOR MARA-MATNR.

START-OF-SELECTION.

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

  SORT IALV ASCENDING BY MATNR.

  IF IALV[] IS INITIAL.
    MESSAGE S429(MO).
    EXIT.
  ENDIF.

  CALL SCREEN 100.

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


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

  CREATE OBJECT ALV_GRID
         EXPORTING  I_PARENT =  ALV_CONTAINER.

*  Populate Field Catalog
  PERFORM GET_FIELDCATALOG.


  CALL METHOD ALV_GRID->SET_TABLE_FOR_FIRST_DISPLAY
      CHANGING
           IT_OUTTAB       = IALV[]
           IT_FIELDCATALOG = FIELDCAT[].


ENDMODULE.

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

  CASE SY-UCOMM.

    WHEN 'BACK' OR 'CANC' or 'EXIT'.
        LEAVE PROGRAM.
  ENDCASE.

ENDMODULE.


************************************************************************
*      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-FIELDNAME  = 'MATNR'.
  LS_FCAT-REF_TABLE  = 'IALV'.
  LS_FCAT-OUTPUTLEN  = '18'.
  APPEND LS_FCAT TO FIELDCAT.

  CLEAR: LS_FCAT.
  LS_FCAT-REPTEXT    = 'Material Description'.
  LS_FCAT-FIELDNAME  = 'MAKTX'.
  LS_FCAT-REF_TABLE  = 'IALV'.
  LS_FCAT-OUTPUTLEN  = '40'.
  APPEND LS_FCAT TO FIELDCAT.

ENDFORM.

Regards,

Rich Heilman

5 REPLIES 5
Read only

Former Member
0 Likes
24,328

Hello,

Take a look at these: [ALV Grid Tutorial|https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/e8a1d690-0201-0010-b7ad-d9719a415907], [ABAP Code Sample to Display Data in ALV Grid Using Object Oriented Programming|https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/79b5e890-0201-0010-7f8e-b7c207edf7c2], [ABAP Code Sample to Edit ALV Grid|https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/ec31e990-0201-0010-f4b6-c02d876ce033] and [ABAP Code Sample for Data Browser Using ALV Grid|https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/925ae990-0201-0010-fb88-f073bd310d1e].

Regards,

Reward if useful

Read only

Sm1tje
Active Contributor
0 Likes
24,328

standard SAP has a lot example reports.

look for BCALV*

Loads and loads of examples

Read only

RichHeilman
Developer Advocate
Developer Advocate
24,330

Here is a very simple example.

REPORT ZRICH_0006.

TABLES: MARA.

TYPE-POOLS: SLIS, ICON.

* Internal Tables
DATA: BEGIN OF IALV OCCURS 0,
      MATNR TYPE MARA-MATNR,
      MAKTX TYPE MAKT-MAKTX,
      END OF IALV .

DATA: ALV_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER,
      ALV_GRID TYPE REF TO CL_GUI_ALV_GRID,
      OK_CODE LIKE SY-UCOMM,
      FIELDCAT TYPE LVC_T_FCAT.

SELECT-OPTIONS: S_MATNR FOR MARA-MATNR.

START-OF-SELECTION.

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

  SORT IALV ASCENDING BY MATNR.

  IF IALV[] IS INITIAL.
    MESSAGE S429(MO).
    EXIT.
  ENDIF.

  CALL SCREEN 100.

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


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

  CREATE OBJECT ALV_GRID
         EXPORTING  I_PARENT =  ALV_CONTAINER.

*  Populate Field Catalog
  PERFORM GET_FIELDCATALOG.


  CALL METHOD ALV_GRID->SET_TABLE_FOR_FIRST_DISPLAY
      CHANGING
           IT_OUTTAB       = IALV[]
           IT_FIELDCATALOG = FIELDCAT[].


ENDMODULE.

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

  CASE SY-UCOMM.

    WHEN 'BACK' OR 'CANC' or 'EXIT'.
        LEAVE PROGRAM.
  ENDCASE.

ENDMODULE.


************************************************************************
*      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-FIELDNAME  = 'MATNR'.
  LS_FCAT-REF_TABLE  = 'IALV'.
  LS_FCAT-OUTPUTLEN  = '18'.
  APPEND LS_FCAT TO FIELDCAT.

  CLEAR: LS_FCAT.
  LS_FCAT-REPTEXT    = 'Material Description'.
  LS_FCAT-FIELDNAME  = 'MAKTX'.
  LS_FCAT-REF_TABLE  = 'IALV'.
  LS_FCAT-OUTPUTLEN  = '40'.
  APPEND LS_FCAT TO FIELDCAT.

ENDFORM.

Regards,

Rich Heilman

Read only

Former Member
0 Likes
24,328

Hi,

OOPS program in SE38 with BCALV * and RSDEMO * wich will show you all standard programs with diffrent functinality.

https://forums.sdn.sap.com/click.jspa?searchID=12043392&messageID=4593478

https://www.sdn.sap.com/irj/sdn/advancedsearch?query=alv&cat=sdn_codesamples

Regards

Kiran Sure

Read only

Former Member
0 Likes
24,328

Welcome to SDN.

A simple ALV code

REPORT zinfotype .

TYPE-POOLS : slis.

*Structure declaration for Infotypes for customer

TYPES : BEGIN OF ty_table,

infty TYPE infty,

pnnnn TYPE pnnnn_d,

END OF ty_table.

*Structure for infotype text

TYPES : BEGIN OF ty_itext,

infty TYPE infty,

itext TYPE intxt,

sprsl TYPE sprsl,

END OF ty_itext.

*Structure for output display

TYPES : BEGIN OF ty_output,

infty TYPE infty,

itext TYPE intxt,

pnnnn TYPE pnnnn_d,

END OF ty_output.

*internal table and work area declarations

DATA : it_table TYPE STANDARD TABLE OF ty_table INITIAL SIZE 0,

it_output TYPE STANDARD TABLE OF ty_output INITIAL SIZE 0,

it_ittext TYPE STANDARD TABLE OF ty_itext INITIAL SIZE 0,

wa_table TYPE ty_table,

wa_output TYPE ty_output,

wa_ittext TYPE ty_itext.

*Data declarations for ALV

DATA: c_ccont TYPE REF TO cl_gui_custom_container, "Custom cont

c_alvgd TYPE REF TO cl_gui_alv_grid, "ALV grid

it_fcat TYPE lvc_t_fcat, Fieldcat

it_layout TYPE lvc_s_layo. "Layout

INITIALIZATION.

START-OF-SELECTION.

*select the infotypes maintained

SELECT infty

pnnnn

FROM t582a

INTO TABLE it_table.

*Select the infotype texts

IF it_table[] IS NOT INITIAL.

SELECT itext

infty

sprsl

FROM t582s

INTO CORRESPONDING FIELDS OF TABLE it_ittext

FOR ALL ENTRIES IN it_table

WHERE infty = it_table-infty

AND sprsl = 'E'.

ENDIF.

*Apppending the data to the internal table of ALV output

LOOP AT it_table INTO wa_table.

wa_output-infty = wa_table-infty.

wa_output-pnnnn = wa_table-pnnnn.

  • For texts

READ TABLE it_ittext INTO wa_ittext WITH KEY infty = wa_table-infty.

wa_output-itext = wa_ittext-itext.

APPEND wa_output TO it_output.

CLEAR wa_output.

ENDLOOP.

  • Calling the ALV screen with custom container

CALL SCREEN 0600.

*On this statement double click it takes you to the screen painter SE51.Enter the attributes

*Create a Custom container and name it CC_CONT 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

&----


*& Module STATUS_0600 OUTPUT

*&----


  • text

*----


module STATUS_0600 output.

  • SET PF-STATUS 'xxxxxxxx'.

  • SET TITLEBAR 'xxx'.

endmodule. " STATUS_0600 OUTPUT

  • calling the PBO module ALV_GRID.

&----


*& Module ALV_GRID OUTPUT

*&----


  • text

*----


MODULE alv_grid OUTPUT.

CREATE OBJECT c_ccont

EXPORTING

container_name = 'CC_CONT'.

CREATE OBJECT c_alvgd

EXPORTING

i_parent = c_ccont.

  • Set field for ALV

PERFORM alv_build_fieldcat.

  • Set ALV attributes FOR LAYOUT

PERFORM alv_report_layout.

CHECK NOT c_alvgd IS INITIAL.

  • Call ALV GRID

CALL METHOD c_alvgd->set_table_for_first_display

EXPORTING

is_layout = it_layout

CHANGING

it_outtab = it_output

it_fieldcatalog = it_fcat

EXCEPTIONS

invalid_parameter_combination = 1

program_error = 2

too_many_lines = 3

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.ENDMODULE. " ALV_GRID OUTPUT

*&----


*& Form alv_build_fieldcat

*&----


  • text

*----


  • <--P_IT_FCAT text

*----


FORM alv_build_fieldcat.

DATA lv_fldcat TYPE lvc_s_fcat.

CLEAR lv_fldcat.

lv_fldcat-row_pos = '1'.

lv_fldcat-col_pos = '1'.

lv_fldcat-fieldname = 'INFTY'.

lv_fldcat-tabname = 'IT_OUTPUT'.

lv_fldcat-outputlen = 8.

lv_fldcat-scrtext_m = 'Infotype'.

lv_fldcat-icon = 'X'.

APPEND lv_fldcat TO it_fcat. CLEAR lv_fldcat.

lv_fldcat-row_pos = '1'.

lv_fldcat-col_pos = '2'.

lv_fldcat-fieldname = 'PNNNN'.

lv_fldcat-tabname = 'IT_OUTPUT'.

lv_fldcat-outputlen = 15.

lv_fldcat-scrtext_m = 'Structure'.

lv_fldcat-icon = ''.

APPEND lv_fldcat TO it_fcat. CLEAR lv_fldcat.

lv_fldcat-row_pos = '1'.

lv_fldcat-col_pos = '3'.

lv_fldcat-fieldname = 'ITEXT'.

lv_fldcat-tabname = 'IT_OUTPUT'.

lv_fldcat-outputlen = 60.

lv_fldcat-scrtext_m = 'Description'.

lv_fldcat-icon = ''.

APPEND lv_fldcat TO it_fcat. CLEAR lv_fldcat.

ENDFORM. " alv_build_fieldcat

&----


*& Form alv_report_layout

*&----


  • text

*----


  • <--P_IT_LAYOUT text

*----


FORM alv_report_layout.

it_layout-cwidth_opt = 'X'.

it_layout-zebra = 'X'.

ENDFORM. " alv_report_layout

  • 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.

&----


*& Module USER_COMMAND_0600 INPUT

*&----


  • text

*----


module USER_COMMAND_0600 input.

endmodule. " USER_COMMAND_0600 INPUT

YOu can find the link and step by step creation at SAPTECHNICAL at

http://saptechnical.com/Tutorials/HRABAP/ListInfotypes/DemoPrg.htm