Application Development 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: 

can we call ALV grid in a method?

Former Member
0 Kudos

hello everybody,

Is it possible to display the output in ALV from a <b>class-method(Global class)</b>?

in a class-method wee cannot call a screen, so is there any other possibility to display the grid??

thanx in advance,

abhilash.

1 ACCEPTED SOLUTION

0 Kudos

Hi,

Try calling one of those REUSE_ALV_XX methods.

May be they can be called from a method of a class.

Regards,

Sesh

5 REPLIES 5

0 Kudos

Hi,

Try calling one of those REUSE_ALV_XX methods.

May be they can be called from a method of a class.

Regards,

Sesh

0 Kudos

Don't USE ALV Function modules wherever possible. Go OO --it's simple once you get the hang of it.

Of course you can do it.

Here's a general class including an event handler in the SAME class. This can be used for DYNAMIC tables, DYNAMIC FCATS and custom events. The method DISPLAY_GRID here will do what you want.

Your structure list and fcat data are in the field-symbols <fs1> and <fs2>

Here's the code. This program just reads 200 lines from KNA1 and displays some data in a GRID with a toolbar. Press the EXIT arror to quit the program.

You need to define a blank screen (SE51) with a custom control on it called CCONTAINER1.

The screen logic as follows

PBO.

MODULE status_0100 OUTPUT.

You don't need a PAI as we are using the toolbar with events.

OK here goes.

  • The CLASS bit first

FIELD-SYMBOLS :

<fs1> TYPE ANY,

<fs2> TYPE STANDARD TABLE,

<dyn_table> TYPE STANDARD TABLE,

<dyn_field>,

<dyn_wa>.

CLASS zcl_dog DEFINITION.

PUBLIC SECTION.

METHODS:

constructor

IMPORTING z_object type ref to zcl_dog,

return_structure,

create_dynamic_fcat

EXPORTING it_fldcat TYPE lvc_t_fcat,

display_grid

CHANGING it_fldcat type lvc_t_fcat,

on_user_command FOR EVENT before_user_command OF cl_gui_alv_grid

IMPORTING e_ucomm

sender,

on_toolbar FOR EVENT toolbar OF cl_gui_alv_grid

IMPORTING e_object

e_interactive.

PRIVATE SECTION.

DATA:

lr_rtti_struc TYPE REF TO cl_abap_structdescr, "RTTI

zog LIKE LINE OF lr_rtti_struc->components, "RTTI

wa_it_fldcat TYPE lvc_s_fcat,

grid_container1 TYPE REF TO cl_gui_custom_container,

grid1 TYPE REF TO cl_gui_alv_grid.

TYPES:

struc LIKE zog.

DATA:

zogt TYPE TABLE OF struc.

ENDCLASS.

CLASS zcl_dog IMPLEMENTATION.

METHOD constructor.

CREATE OBJECT grid_container1

EXPORTING

container_name = 'CCONTAINER1'.

CREATE OBJECT grid1

EXPORTING i_parent = grid_container1.

SET HANDLER z_object->on_user_command for grid1.

SET HANDLER z_object->on_toolbar for grid1.

ENDMETHOD.

METHOD return_structure.

lr_rtti_struc ?= cl_abap_structdescr=>DESCRIBE_BY_DATA( <fs1> ).

zogt[] = lr_rtti_struc->components.

ASSIGN zogt[] TO <fs2>.

ENDMETHOD.

METHOD create_dynamic_fcat.

LOOP AT <fs2> INTO zog.

CLEAR wa_it_fldcat.

wa_it_fldcat-fieldname = zog-name .

wa_it_fldcat-datatype = zog-type_kind.

wa_it_fldcat-inttype = zog-type_kind.

wa_it_fldcat-intlen = zog-length.

wa_it_fldcat-decimals = zog-decimals.

wa_it_fldcat-coltext = zog-name.

wa_it_fldcat-lowercase = 'X'.

APPEND wa_it_fldcat TO it_fldcat .

ENDLOOP.

ENDMETHOD.

METHOD display_grid.

CALL METHOD grid1->set_table_for_first_display

CHANGING

it_outtab = <dyn_table>

it_fieldcatalog = it_fldcat.

ENDMETHOD.

METHOD on_user_command.

CASE e_ucomm.

WHEN 'EXIT'.

LEAVE PROGRAM.

WHEN 'SAVE'.

ENDCASE.

ENDMETHOD. "on_user_command

METHOD on_toolbar.

DATA: ls_toolbar TYPE stb_button.

CLEAR ls_toolbar.

MOVE 0 TO ls_toolbar-butn_type.

MOVE 'EXIT' TO ls_toolbar-function.

MOVE SPACE TO ls_toolbar-disabled.

MOVE icon_system_end TO ls_toolbar-icon.

MOVE 'Click2Exit' TO ls_toolbar-quickinfo.

APPEND ls_toolbar TO e_object->mt_toolbar.

CLEAR ls_toolbar.

MOVE 0 TO ls_toolbar-butn_type.

MOVE 'SAVE' TO ls_toolbar-function.

MOVE SPACE TO ls_toolbar-disabled.

MOVE icon_system_save TO ls_toolbar-icon.

MOVE 'Save data' TO ls_toolbar-quickinfo.

APPEND ls_toolbar TO e_object->mt_toolbar.

ENDMETHOD.

ENDCLASS.

Now the program is incredibly simple and will work for almost ANY table with just a call to the methods in the class . Saves Zillions of coding different ABAPS.

                            • start of program data to demo use of above class ************

INCLUDE <icon>.

TABLES : kna1.

TYPES: BEGIN OF s_elements,

kunnr TYPE kna1-kunnr,

name1 TYPE kna1-name1,

stras TYPE kna1-stras,

telf1 TYPE kna1-telf1,

ort01 TYPE kna1-ort01,

pstlz TYPE kna1-pstlz,

END OF s_elements.

DATA: z_object type ref to zcl_dog, "Instantiate our class

t_elements TYPE TABLE OF s_elements, "refers to our ITAB

wa_elements TYPE s_elements,

wa_dyn_table_line TYPE REF TO DATA,

it_fldcat TYPE lvc_t_fcat,

new_table TYPE REF TO DATA,

dy_table TYPE REF TO data,

dy_line TYPE REF TO data.

START-OF-SELECTION.

CALL SCREEN 100.

END-OF-SELECTION..

MODULE status_0100 OUTPUT.

ASSIGN wa_elements TO <fs1>.

*Instantiate our zcl_dog class

CREATE OBJECT

z_object

EXPORTING

z_object = z_object.

  • Get structure of your ITAB

CALL METHOD z_object->return_structure.

  • build fcat.

CALL METHOD z_object->create_dynamic_fcat

IMPORTING

it_fldcat = it_fldcat.

  • build dynamic table

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = it_fldcat

IMPORTING

ep_table = dy_table.

ASSIGN dy_table->* TO <dyn_table>.

  • Create dynamic work area and assign to FS

CREATE DATA dy_line LIKE LINE OF <dyn_table>.

ASSIGN dy_line->* TO <dyn_wa>.

  • fill it

PERFORM populate_dynamic_itab.

  • Display ALV grid with our EXIT tool button

CALL METHOD z_object->display_grid

CHANGING it_fldcat = it_fldcat.

ENDMODULE.

MODULE user_command_0100 INPUT.

  • PAI not needed as we are using EVENTS

ENDMODULE.

FORM populate_dynamic_itab.

*

  • just read 200 lines as a demo from KNA1

  • assign dynamic table created to <field symbol> so you can fill it

  • with data. Table also now acessible to methods in the classes

  • defined in this program.

SELECT kunnr name1 stras telf1 ort01 pstlz

UP TO 200 rows

FROM kna1

INTO CORRESPONDING FIELDS OF TABLE <dyn_table>.

ENDFORM.

If you save the class with SE24 you can see all you need to do in the program is

1) Define your data

2) Instantiate the class

CREATE OBJECT

z_object

EXPORTING

z_object = z_object.

3) Get structure of your ITAB (can be ANY itab including deep structures)

CALL METHOD z_object->return_structure.

4) build your FCAT automatically using the structure returned from 3).

CALL METHOD z_object->create_dynamic_fcat

IMPORTING

it_fldcat = it_fldcat.

5) build dynamic table

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = it_fldcat

IMPORTING

ep_table = dy_table.

6) * Create dynamic work area and assign to FS

ASSIGN dy_table->* TO <dyn_table>.

CREATE DATA dy_line LIKE LINE OF <dyn_table>.

ASSIGN dy_line->* TO <dyn_wa>.

7) Fill it

PERFORM populate_dynamic_itab.

😎 Now display Display ALV grid with our EXIT tool button

CALL METHOD z_object->display_grid

CHANGING it_fldcat = it_fldcat.

If you look in the method display_grid you'll see that the method can call ALV classes and functions.

Isn't the above much easier than messing around with obsolete function modules --and you can write virtually the same code for ANY table. You can also in the events add eitable functions etc.

In fact we could even make it simpler.

I'll try and post the code later but in fact we could get the CLASS to do almost the entire thing so the only statements you would actually need in your ABAP would be

Your Data

Instantiate the class.

Call the class with a new method to do all the coding up to generating the ITAB.

Populate your Itab

call the method to display your ITAB.

So your new abap would basically just have DATA, your ITAB structure, 2 method calls and a proceedure to fill your ITAB.

(Now your Boss won't take any excuses if it takes you longer than 20 Mins to code an ABAP containing even a complex table display. !!!!).

Cheers

Jimbo

jrgkraus
Active Contributor
0 Kudos

Just call the function module "REUSE_ALV_GRID_DISPLAY". It provides its own dynpro and works fine even from a class method.

Regards

Jörg

Former Member
0 Kudos

Hi Jorg

I think you've missed the essential point here

By Instantiating the Class we don't EVER have to do much more coding for whatever the table so you can use this as a model WHENEVER you want to display (or manipulate) a table. Once the class has been written it can be called by any number of instances without any extra code being needed for your new ABAPs.

For example

I've modified the above class so the ABAP Program in the previous post by me in this thread just now does the following.

FIELD-SYMBOLS :

<fs1> TYPE ANY,

<fs2> TYPE STANDARD TABLE,

<dyn_table> TYPE STANDARD TABLE,

<dyn_field>,

<dyn_wa>.

INCLUDE <icon>.

TABLES : kna1.

TYPES: BEGIN OF s_elements,

kunnr TYPE kna1-kunnr,

name1 TYPE kna1-name1,

stras TYPE kna1-stras,

telf1 TYPE kna1-telf1,

ort01 TYPE kna1-ort01,

pstlz TYPE kna1-pstlz,

END OF s_elements.

DATA: z_object type ref to zcl_dog, "Instantiate our class

t_elements TYPE TABLE OF s_elements, "refers to our ITAB

wa_elements TYPE s_elements,

wa_dyn_table_line TYPE REF TO DATA,

it_fldcat TYPE lvc_t_fcat,

new_table TYPE REF TO DATA,

dy_table TYPE REF TO data,

dy_line TYPE REF TO data.

START-OF-SELECTION.

CALL SCREEN 100.

END-OF-SELECTION..

MODULE status_0100 OUTPUT.

ASSIGN wa_elements TO <fs1>.

*Instantiate our zcl_dog class

CREATE OBJECT

z_object

EXPORTING

z_object = z_object.

CALL METHOD z_object->build_dynamic_structures

CHANGING it_fldcat = it_fldcat.

PERFORM populate_dynamic_itab.

  • Display ALV grid with our EXIT tool button

CALL METHOD z_object->display_grid

CHANGING it_fldcat = it_fldcat.

ENDMODULE.

MODULE user_command_0100 INPUT.

  • PAI not needed as we are using EVENTS

ENDMODULE.

FORM populate_dynamic_itab.

SELECT kunnr name1 stras telf1 ort01 pstlz

UP TO 200 rows

FROM kna1

INTO CORRESPONDING FIELDS OF TABLE <dyn_table>.

ENDFORM.

Really EASY isn't it.

All you have to do now is just fill the ITAB bit up as shown, define your data and structure (doesn't have to be in DDIC) and code the other 5 or 6 lines and you've got a display of whatever table you want.

Just coding the Function Module alone will take more lines than my entire program and you don't have to build or merge any FCATS either.

Here's the modified class to get the above program to work.

*

CLASS zcl_dog DEFINITION.

PUBLIC SECTION.

METHODS:

constructor

IMPORTING z_object type ref to zcl_dog,

display_grid

CHANGING it_fldcat type lvc_t_fcat,

build_dynamic_structures

CHANGING it_fldcat TYPE lvc_t_fcat.

PRIVATE SECTION.

METHODS:

on_user_command FOR EVENT before_user_command OF cl_gui_alv_grid

IMPORTING e_ucomm

sender,

on_toolbar FOR EVENT toolbar OF cl_gui_alv_grid

IMPORTING e_object

e_interactive,

return_structure,

create_dynamic_fcat

EXPORTING it_fldcat TYPE lvc_t_fcat,

create_dynamic_table

IMPORTING it_fldcat TYPE lvc_t_fcat

EXPORTING dy_table TYPE REF TO DATA.

DATA:

lr_rtti_struc TYPE REF TO cl_abap_structdescr, "RTTI

zog LIKE LINE OF lr_rtti_struc->components, "RTTI

wa_it_fldcat TYPE lvc_s_fcat,

it_fldcat TYPE lvc_t_fcat,

dy_table TYPE REF TO data,

dy_line TYPE REF TO data,

grid_container1 TYPE REF TO cl_gui_custom_container,

grid1 TYPE REF TO cl_gui_alv_grid.

TYPES:

struc LIKE zog.

DATA:

zogt TYPE TABLE OF struc.

ENDCLASS.

CLASS zcl_dog IMPLEMENTATION.

METHOD constructor.

CREATE OBJECT grid_container1

EXPORTING

container_name = 'CCONTAINER1'.

CREATE OBJECT grid1

EXPORTING

i_parent = grid_container1.

SET HANDLER z_object->on_user_command for grid1.

SET HANDLER z_object->on_toolbar for grid1.

ENDMETHOD.

METHOD return_structure.

lr_rtti_struc ?= cl_abap_structdescr=>DESCRIBE_BY_DATA( <fs1> ).

zogt[] = lr_rtti_struc->components.

ASSIGN zogt[] TO <fs2>.

ENDMETHOD.

METHOD create_dynamic_fcat.

LOOP AT <fs2> INTO zog.

CLEAR wa_it_fldcat.

wa_it_fldcat-fieldname = zog-name .

wa_it_fldcat-datatype = zog-type_kind.

wa_it_fldcat-inttype = zog-type_kind.

wa_it_fldcat-intlen = zog-length.

wa_it_fldcat-decimals = zog-decimals.

wa_it_fldcat-coltext = zog-name.

wa_it_fldcat-lowercase = 'X'.

APPEND wa_it_fldcat TO it_fldcat .

ENDLOOP.

ENDMETHOD.

METHOD create_dynamic_table.

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = it_fldcat

IMPORTING

ep_table = dy_table.

ENDMETHOD.

METHOD build_dynamic_structures.

CALL METHOD me->return_structure.

CALL METHOD me->create_dynamic_fcat

IMPORTING

it_fldcat = it_fldcat.

CALL METHOD me->create_dynamic_table

EXPORTING

it_fldcat = it_fldcat

IMPORTING

dy_table = dy_table.

ASSIGN dy_table->* TO <dyn_table>.

CREATE DATA dy_line LIKE LINE OF <dyn_table>.

ASSIGN dy_line->* TO <dyn_wa>.

ENDMETHOD.

METHOD display_grid.

CALL METHOD grid1->set_table_for_first_display

CHANGING

it_outtab = <dyn_table>

it_fieldcatalog = it_fldcat.

ENDMETHOD.

METHOD on_user_command.

CASE e_ucomm.

WHEN 'EXIT'.

LEAVE PROGRAM.

WHEN 'SAVE'.

ENDCASE.

ENDMETHOD. "on_user_command

METHOD on_toolbar.

DATA: ls_toolbar TYPE stb_button.

CLEAR ls_toolbar.

MOVE 0 TO ls_toolbar-butn_type.

MOVE 'EXIT' TO ls_toolbar-function.

MOVE SPACE TO ls_toolbar-disabled.

MOVE icon_system_end TO ls_toolbar-icon.

MOVE 'Click2Exit' TO ls_toolbar-quickinfo.

APPEND ls_toolbar TO e_object->mt_toolbar.

CLEAR ls_toolbar.

MOVE 0 TO ls_toolbar-butn_type.

MOVE 'SAVE' TO ls_toolbar-function.

MOVE SPACE TO ls_toolbar-disabled.

MOVE icon_system_save TO ls_toolbar-icon.

MOVE 'Save data' TO ls_toolbar-quickinfo.

APPEND ls_toolbar TO e_object->mt_toolbar.

ENDMETHOD.

ENDCLASS.

Cheers

Jimbo

marcelo_ramos
Active Contributor
0 Kudos

Hi,

I agree with James, you can prevent to use Function Module. Using oo you can create alv more flexible and more cleaner.

Good Luck,

Marcelo.