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

AlV through oops and function module

Former Member
0 Likes
718

Hi

I required a code to generate the alv through

oops and function module.

Thanks

Jatender

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
690

The below is the obj oriented method.....



REPORT  ZAMIT_TEST2.


TYPE-POOLS: icon, col.
* type for internal table
TYPES: BEGIN OF t_marc.
        INCLUDE STRUCTURE marc.
* internal table for cell color information
TYPES : it_colors TYPE lvc_t_scol,
END OF t_marc.

DATA: it_marc TYPE TABLE OF t_marc.
DATA: wa_marc LIKE LINE OF it_marc.
DATA: wa_colors LIKE LINE OF wa_marc-it_colors.

DATA: gr_alv TYPE REF TO cl_salv_table.
DATA: lr_columns TYPE REF TO cl_salv_columns_table.

SELECT-OPTIONS: so_mat FOR wa_marc-matnr MEMORY ID car.

START-OF-SELECTION.
* retrieve data into internal table
  SELECT * FROM marc
    INTO CORRESPONDING FIELDS OF TABLE it_marc
  WHERE matnr IN so_mat.

  LOOP AT it_marc INTO wa_marc.

    IF wa_marc-werks = '1060'.
      CLEAR wa_colors.
      wa_colors-fname = ''.
      wa_colors-color-col = col_positive.
      wa_colors-color-int = 1.
      APPEND wa_colors TO wa_marc-it_colors.
    ENDIF.
    MODIFY it_marc FROM wa_marc TRANSPORTING it_colors.
  ENDLOOP.

  CALL METHOD cl_salv_table=>factory
  EXPORTING
  list_display = if_salv_c_bool_sap=>false
  IMPORTING
  r_salv_table = gr_alv
  CHANGING
  t_table = it_marc.

  lr_columns = gr_alv->get_columns( ).

  lr_columns->set_color_column( value = 'IT_COLORS' ).

* display ALV
gr_alv->display( ).

6 REPLIES 6
Read only

Former Member
0 Likes
690
Read only

Former Member
0 Likes
691

The below is the obj oriented method.....



REPORT  ZAMIT_TEST2.


TYPE-POOLS: icon, col.
* type for internal table
TYPES: BEGIN OF t_marc.
        INCLUDE STRUCTURE marc.
* internal table for cell color information
TYPES : it_colors TYPE lvc_t_scol,
END OF t_marc.

DATA: it_marc TYPE TABLE OF t_marc.
DATA: wa_marc LIKE LINE OF it_marc.
DATA: wa_colors LIKE LINE OF wa_marc-it_colors.

DATA: gr_alv TYPE REF TO cl_salv_table.
DATA: lr_columns TYPE REF TO cl_salv_columns_table.

SELECT-OPTIONS: so_mat FOR wa_marc-matnr MEMORY ID car.

START-OF-SELECTION.
* retrieve data into internal table
  SELECT * FROM marc
    INTO CORRESPONDING FIELDS OF TABLE it_marc
  WHERE matnr IN so_mat.

  LOOP AT it_marc INTO wa_marc.

    IF wa_marc-werks = '1060'.
      CLEAR wa_colors.
      wa_colors-fname = ''.
      wa_colors-color-col = col_positive.
      wa_colors-color-int = 1.
      APPEND wa_colors TO wa_marc-it_colors.
    ENDIF.
    MODIFY it_marc FROM wa_marc TRANSPORTING it_colors.
  ENDLOOP.

  CALL METHOD cl_salv_table=>factory
  EXPORTING
  list_display = if_salv_c_bool_sap=>false
  IMPORTING
  r_salv_table = gr_alv
  CHANGING
  t_table = it_marc.

  lr_columns = gr_alv->get_columns( ).

  lr_columns->set_color_column( value = 'IT_COLORS' ).

* display ALV
gr_alv->display( ).

Read only

Former Member
0 Likes
690

Hi ,

Refer to the following link.

1.using function modules:

http://saptechnical.com/Tutorials/ALV/SampleALVGridProgram.htm

2.Using OOPs.

http://saptechnical.com/Tutorials/ALV/Interactive/oops.htm

Regards,

Jaya Vani

Read only

Former Member
0 Likes
690

Jatinder,

welcome to SDN.

just refer:

dont forget to reward.

Amit.

Read only

vinod_vemuru2
Active Contributor
0 Likes
690

Hi Jatender,

Check below simple code to understand better.


REPORT z75694_alv.
*----------------------------------------------------------------------
*Internal tables
*----------------------------------------------------------------------
DATA: i_mara  TYPE STANDARD TABLE OF mara,
      i_fieldcat       TYPE STANDARD TABLE OF lvc_s_fcat,
      container        TYPE REF TO cl_gui_custom_container,
      container2       TYPE REF TO cl_gui_custom_container,
      grid             TYPE REF TO cl_gui_alv_grid,
      grid_c           TYPE REF TO cl_gui_alv_grid.
*----------------------------------------------------------------------
*Work-Areas
*----------------------------------------------------------------------
DATA: wa_fieldcat TYPE lvc_s_fcat.

*----------------------------------------------------------------------
*Selection Screen
*----------------------------------------------------------------------
SELECTION-SCREEN BEGIN OF BLOCK main WITH FRAME.
SELECTION-SCREEN BEGIN OF BLOCK sub1 WITH FRAME.
SELECT-OPTIONS: so_matnr FOR mara-matnr.
SELECTION-SCREEN END OF BLOCK sub1.
SELECTION-SCREEN END OF BLOCK main.
*----------------------------------------------------------------------
*START-OF-SELECTION.
*----------------------------------------------------------------------
START-OF-SELECTION.
PERFORM select_data.
*----------------------------------------------------------------------
*END-OF-SELECTION.
*----------------------------------------------------------------------
END-OF-SELECTION.
PERFORM display_alv.

*&---------------------------------------------------------------------*
*&      Form  select_data
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM select_data.
  SELECT * INTO TABLE i_mara
           FROM mara
           WHERE matnr IN so_matnr.
ENDFORM.                    " select_data

*&---------------------------------------------------------------------*
*&      Form  display_alv
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM display_alv.
  CALL SCREEN 100.
ENDFORM.                    " display_alv

*&---------------------------------------------------------------------*
*&      Form  fieldcat
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM fieldcat USING fname tname pos edit.
  CLEAR wa_fieldcat.
  wa_fieldcat-fieldname = fname.
  wa_fieldcat-tabname   = tname.
  wa_fieldcat-scrtext_l = fname.
  wa_fieldcat-scrtext_m = fname.
  wa_fieldcat-scrtext_s = fname.
  wa_fieldcat-just      = ' '.
  wa_fieldcat-col_pos   = pos.
  wa_fieldcat-edit = edit.
  APPEND wa_fieldcat TO i_fieldcat.
ENDFORM.                    " fieldcat

*&---------------------------------------------------------------------*
*&      Module  STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
  SET PF-STATUS '100'.
  SET TITLEBAR '100'.
ENDMODULE.                 " STATUS_0100  OUTPUT

*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE user_command_0100 INPUT.
  CASE sy-ucomm.
    WHEN 'BACK' OR 'EXIT'.
      LEAVE TO SCREEN 0.
    WHEN 'EXIT'.
      LEAVE PROGRAM.
    WHEN 'SAVE' OR 'UPDATE'.
*      PERFORM update_table.
  ENDCASE.
ENDMODULE.                 " USER_COMMAND_0100  INPUT

*&---------------------------------------------------------------------*
*&      Module  display_data  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE display_data OUTPUT.
  PERFORM: fieldcat USING 'MATNR'     'ZVINU' 2 space.

*clear i_fieldcat.

  CREATE OBJECT container
    EXPORTING
      container_name    = 'CON'.

  CREATE OBJECT grid
    EXPORTING
      i_parent          = container.


  CALL METHOD grid->set_table_for_first_display
*    EXPORTING
*      is_layout                     = wa_layout
    CHANGING
      it_outtab                     = i_mara
      it_fieldcatalog               = i_fieldcat
    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.                 " display_data  OUTPUT

Define screen 100 with one container name as CON

Thanks,

Vinod.

Read only

Former Member
0 Likes
690

oops method....



REPORT ZWA_ALV_COLORS .
 
tables : BKPF.
 
types : begin of ty_BKPF,
  belnr like BKPF-belnr,
  bukrs like BKPF-bukrs,
  counter(4) type n,
  color_line(4) type c, " Line color
  color_cell type lvc_t_scol, " Cell color
end of ty_BKPF.
 
* Structures
data : wa_BKPF type ty_BKPF,
wa_fieldcat type lvc_s_fcat,
is_layout type lvc_s_layo,
wa_color type lvc_s_scol.
 
* Internal table
data : it_BKPF type standard table of ty_BKPF,
       it_fieldcat type standard table of lvc_s_fcat,
       it_color type table of lvc_s_scol.
 
* Variables
data : okcode like sy-ucomm,
       w_alv_grid type ref to cl_gui_alv_grid,
       w_docking_container type ref to cl_gui_docking_container.
 
 
parameters : p_column as checkbox DEFAULT 'X',
             p_line as checkbox DEFAULT 'X',
             p_cell as checkbox DEFAULT 'X'.
 
 
at selection-screen output.
 
  perform get_data.
  perform fill_catalog.
 
  if w_docking_container is initial.
    perform create_objects.
  endif.
 
*&--------------------------------------------------------------*
*& Form create_objects
*&--------------------------------------------------------------*
form create_objects.
 
  create object w_docking_container
  exporting
  ratio = 40
  exceptions
  cntl_error = 1
  cntl_system_error = 2
  create_error = 3
  lifetime_error = 4
  lifetime_dynpro_dynpro_link = 5
  others = 6.
 
  create object w_alv_grid
  exporting
  i_parent = w_docking_container.
 
* Field that identify color line in internal table
  move 'COLOR_LINE' to is_layout-info_fname.
 
* Field that identify cell color in inetrnal table
  move 'COLOR_CELL' to is_layout-ctab_fname.
 
  call method w_alv_grid->set_table_for_first_display
  exporting
  is_layout = is_layout
  changing
  it_outtab = it_BKPF
  it_fieldcatalog = it_fieldcat
  exceptions
  invalid_parameter_combination = 1
  program_error = 2
  too_many_lines = 3
  others = 4.
 
endform.
*&--------------------------------------------------------------*
*& Form get_data
*&--------------------------------------------------------------*
form get_data.
 "Check this code for colour
  select * from BKPF up to 50 rows.
    clear : wa_BKPF-color_line, wa_BKPF-color_cell.
 
    move-corresponding BKPF to wa_BKPF.
    add 1 to wa_BKPF-counter.
 
    if wa_BKPF-COUNTER = '2' AND p_line = 'X'.
* Color line
      move 'C610' to wa_BKPF-color_line.
    elseif wa_BKPF-counter = '0004'
    and p_cell = 'X'.
* Color cell
      move 'BELNR' to wa_color-fname.
      move '1' to wa_color-color-col.
      move '1' to wa_color-color-int.
      move '1' to wa_color-color-inv.
      append wa_color to it_color.
      wa_BKPF-color_cell[] = it_color[].
    endif.
 
    append wa_BKPF to it_BKPF.
  endselect.
 
endform.
*&--------------------------------------------------------------*
*& Form fill_catalog
*&--------------------------------------------------------------*
form fill_catalog.
 
*****************************************************************
* Colour code : *
* Colour is a 4-char field where : *
* - 1st char = C (color property) *
* - 2nd char = color code (from 0 to 7) *
* 0 = background color *
* 1 = blue *
* 2 = gray *
* 3 = yellow *
* 4 = blue/gray *
* 5 = green *
* 6 = red *
* 7 = orange *
* - 3rd char = intensified (0=off, 1=on) *
* - 4th char = inverse display (0=off, 1=on) *
* *
* Colour overwriting priority : *
* 1. Line *
* 2. Cell *
* 3. Column *
*****************************************************************
  data : w_position type i value '1'.
 "In field catalog filling coulour
  clear wa_fieldcat.
  move w_position to wa_fieldcat-col_pos.
  move 'BELNR' to wa_fieldcat-fieldname.
  move 'BKPF' to wa_fieldcat-ref_table.
  move 'BELNR' to wa_fieldcat-ref_field.
  append wa_fieldcat to it_fieldcat.
 
  add 1 to w_position.
 
  clear wa_fieldcat.
  move w_position to wa_fieldcat-col_pos.
  move 'BUKRS' to wa_fieldcat-fieldname.
  move 'BKPF' to wa_fieldcat-ref_table.
  move 'BUKRS' to wa_fieldcat-ref_field.
* Color column
  if p_column = 'X'.
    move 'C510' to wa_fieldcat-emphasize.
  endif.
  append wa_fieldcat to it_fieldcat.
 
  add 1 to w_position.
 
  clear wa_fieldcat.
  move w_position to wa_fieldcat-col_pos.
  move 'COUNTER' to wa_fieldcat-fieldname.
  move 'N' to wa_fieldcat-inttype.
  move '4' to wa_fieldcat-intlen.
  move 'Counter' to wa_fieldcat-coltext.
  append wa_fieldcat to it_fieldcat.
 
  add 1 to w_position.
 
*  clear wa_fieldcat.
*  move w_position to wa_fieldcat-col_pos.
*  move 'FREE_TEXT' to wa_fieldcat-fieldname.
*  move 'C' to wa_fieldcat-inttype.
*  move '20' to wa_fieldcat-intlen.
*  move 'Text' to wa_fieldcat-coltext.
*  append wa_fieldcat to it_fieldcat.
ENDFORM.