2007 Aug 04 10:25 AM
hi all,
Cud any let me know how to build field catalog dynamically? using field symbols?
thanx
jack
2007 Aug 04 10:37 AM
Hi
If you follow the steps you should be able to code very quickly a decent useable ALV OO program which can retrieve and manipulate data very easily. The events are (hopefully) well documented as are all the steps.
Once you understand the basics you can add more functionality like colouring Cells, adding hyperlinks etc etc.
I've always found that stupid SEAT / AIRLINE application SAP uses for its examples far too overblown and in reality who would ever use a SAP system for Airline reservations anyway.
The class here describes a much simpler application which CLEARLY (I hope) explains how the whole thing works.
Jimbo's generic class for using the OO ALV GRID Class CL_GUI_ALV_GRID
from an application program to display and manipulate ANY table
with minimal coding needed in the Calling application program.
*
Handles the following EVENTS
1) TOOLBAR BUTTONS
(you can add more to the toolbar method
if you need even more functionality).
2) DOUBLE CLICK
3) ENTER KEY PRESSED
4) DATA CHANGED
5) DATA CHANGED FINISHED
Methods available
PUBLIC METHODS ( Can be called directly from the application program).
1) display_grid displays grid with toolbar
Table and FCAT are built dynamically - user only needs to
define the table structure (can be DDIC or User fields)
2) change_title - changes title at the top of the Grid
3) refresh_grid - refreshes grid after table updated etc.
4) build_dynamic_structures - this method creates a dynamic table and a dynamic FCAT
using the structure defined in the calling application program
PRIVATE METHODS (Internal Methods used within the class)
1) verwerk - returns to FORM VERWERK in calling application program
(Via Toolbar). The application program can then do any special processing
at this point e.g update SAP tables etc.
2) download_to_excel (via Toolbar). This creates an EXCEL spreadsheet
directly which can be downloaded / saved to a file if required,
3) return_structure - internal method returns the structure
of the table defined in the calling application program. This is needed in order
to build the dynamic table and Field Catalog.
4) create_dynamic_table - creates a dynamic table from the structure defined in
the calling application program
5) create_dynamic_fcat - creates the dynamic field catalog from the structure
defined in the application program.
6) dubbleklik entered when user double clicks a cell
activated by EVENT DOUBLE_CLICK.
returns to FORM DUBBELKLIK in the
calling application program
COOKBOOK STEP BY STEP instructions on how to use this class
in your application program.
In the application program :
*
1) define a blank screen 100 - SE51 with a custom container
on it called CCONTAINER1.
*
you need the following logic in the screen
The PAI is only used if you have defined a STATUS with SE41 and you exit the
application program via the standard SAP buttons on the
top of the Screen (NOT the GRID toolbar).
PROCESS BEFORE OUTPUT.
MODULE STATUS_0100.
*
PROCESS AFTER INPUT.
MODULE USER_COMMAND_0100.
2) (optional) define a STATUS with a titlebar - SE41
you only need this if you want the standard EXIT and menu buttons on the
top line of the screen
*
3) If you want to have your OWN colum names on the grid
add the following macro to the start of your program
*
DEFINE col_name.
read table it_fldcat into wa_it_fldcat index &1.
wa_it_fldcat-coltext = &2.
modify it_fldcat from wa_it_fldcat index &1.
END-OF-DEFINITION.
*
4) Define the following Field symbols
<fs1> TYPE ANY,
<fs2> TYPE STANDARD TABLE,
<fs3> TYPE ANY,
<field_catalog> TYPE STANDARD TABLE,
<dyn_table> TYPE STANDARD TABLE,
<orig_table> TYPE STANDARD TABLE,
<dyn_field>,
<dyn_wa>.
*
5) After the field-symbols add the code in this class
as an INCLUDE
e.g INCLUDE ZZJIMBOXX_INCL.
6) define your Internal table as follows
TYPES: BEGIN OF s_elements,
Your structure
your structure etc.
END OF s_elements.
For example
INCLUDE <icon>.
TABLES: VAPMA.
*TYPES: BEGIN OF s_elements,
vbeln TYPE vapma-vbeln,
posnr TYPE vapma-posnr,
matnr TYPE vapma-matnr,
kunnr TYPE vapma-kunnr,
werks TYPE vapma-werks,
vkorg TYPE vapma-vkorg,
vkbur TYPE vapma-vkbur,
status TYPE c,
*END OF s_elements.
7) Define the following data IN YOUR APPLICATION PROGRAM
(note here only data is described
that relates to using THIS CLASS. Data purely used internally
in the application program is NOT shown here).
DATA: z_object TYPE REF TO zcl_dog, "Instantiate our class
grid_container1 TYPE REF TO cl_gui_custom_container,
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,
i_gridtitle TYPE lvc_title,
wa_it_fldcat TYPE lvc_s_fcat,
new_table TYPE REF TO DATA,
dy_table TYPE REF TO data,
dy_line TYPE REF TO data,
row_id TYPE sy-index.
😎 insert the following code at the start of the application program.
*START-OF-SELECTION.
*CALL SCREEN 100.
*END-OF-SELECTION.
*MODULE status_0100 OUTPUT.
*ASSIGN wa_elements TO <fs1>.
*CREATE OBJECT z_object EXPORTING z_object = z_object. "Instantiate the class
*CALL METHOD z_object->build_dynamic_structures
CHANGING it_fldcat = it_fldcat.
*
9) if you inserted the macro in step 3) then
define your own column names as follws
col_name 1 'Name1'.
col_name 2 'Name2'.
etc. The number is the colum number you want and the name is
the name you want to assign to the column.
for example using the table shown above
col_name 1 'Order Nr'.
col_name 2 'Item'.
col_name 3 'Material'.
col_name 4 'Customer'.
col_name 5 'Plant'.
col_name 6 'Sales Org'.
col_name 7 'Sales Office'.
col_name 8 'Status'.
10) perform a routine that fills your dynamic table and
display the GRID. If you created a status with SE41 you can set
a title etc. Further processing is dependent on the users action
after the GRID is displayed for example if a Cell is double clicked,
dat is entered, a toolbar button is pressed or a SAP ICON on top of the screen is pressed.
PERFORM populate_dynamic_itab.
CALL METHOD z_object->display_grid
CHANGING it_fldcat = it_fldcat.
SET PF-STATUS '0001'.
SET TITLEBAR '000'.
ENDMODULE.
11) If you added a STATUS via SE41 you can exit the program via the
standard SAP buttons at the top of the screen
otherwise exit via the exit button on the toolbar.
*
You only need this piece of code if you defined a STATUS in the application program
MODULE user_command_0100 INPUT.
CASE sy-ucomm.
WHEN 'BACK'.
LEAVE PROGRAM.
WHEN 'EXIT'.
LEAVE PROGRAM.
WHEN 'RETURN'.
LEAVE PROGRAM.
WHEN OTHERS.
ENDCASE.
12) to populate the dynamic table you only need to code something like this
remember the class has already created and structured the field-symbol <dyn_table>
so you don't have to do anything other than just select the fields you want
filled and from what data source(es).
*FORM populate_dynamic_itab.
*SELECT vbeln posnr matnr kunnr werks vkorg vkbur
UP TO 200 rows
FROM vapma
INTO CORRESPONDING FIELDS OF TABLE <dyn_table>.
if you want to keep the original table before making any changes etc code
the following
create 2nd Dyn table to hold original data. We can use
the same field catalog as for the original table
as we are just creating an identical copy here.
*CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = it_fldcat
IMPORTING
ep_table = dy_table.
ASSIGN dy_table->* TO <orig_table>.
CREATE DATA dy_line LIKE LINE OF <orig_table>.
ASSIGN dy_line->* TO <dyn_wa>.
<orig_table> = <dyn_table>.
ENDFORM.
13) you need these 2 processing routines in your application program.
FORM VERWERK. "Entered from VERW on toolbar
*break-point 1.
Orig table is in dynamic table <orig_table>
ALV GRID changed table is in <dyn_table>.
*Loop at <orig_table> into <dyn_wa>.
Do what you want here
end
endloop.
*
ENDFORM.
*form dubbleklik using "Entered when a cell is double clicked
e_row type LVC_S_ROW
e_column type LVC_S_col
es_row_no type lvc_s_roid.
break-point 1.
Get Row id into a variable for this program.
row_id = e_row.
SET TITLEBAR '001'. "If you defined a status in SE41
i_gridtitle = 'Grid Title Changed'.
CALL METHOD z_object->change_title
EXPORTING i_gridtitle = i_gridtitle.
PERFORM refresh.
endform.
The REFRESH routine is optional but after a double click I assume
you want to do some processing
and re-display the data
so as a sample code something like
*FORM refresh.
data: ord_nr TYPE vapma-vbeln. "Your data
*READ TABLE <dyn_table> index row_id into wa_elements.
ord_nr = wa_elements-vbeln.
You've now got the Row double clicked so pick out the data element(s)
you wnat to process and do your processing
*set parameter id 'AUN' field ord_nr.
*CALL TRANSACTION 'VA02' AND SKIP FIRST SCREEN.
You can update the dynamic table for example
*wa_elements-status = 'C'.
*modify <dyn_table> from wa_elements index row_id.
now redisplay the updated grid.
*CALL METHOD z_object->refresh_grid.
*ENDFORM.
*************Class ZCL_DOG*************
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,
change_title
IMPORTING
i_gridtitle TYPE lvc_title,
refresh_grid.
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,
on_dubbelklik FOR EVENT double_click OF cl_gui_alv_grid
IMPORTING e_row
e_column
es_row_no,
handle_data_changed
FOR EVENT data_changed OF cl_gui_alv_grid
IMPORTING er_data_changed,
handle_data_changed_finished
FOR EVENT data_changed_finished OF cl_gui_alv_grid
IMPORTING e_modified
et_good_cells,
verwerk
IMPORTING program TYPE sy-repid,
download_to_excel,
dubbleklik
IMPORTING
e_row type LVC_S_ROW
e_column TYPE LVC_S_COL
es_row_no type lvc_s_ROID
program type sy-repid,
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,
struct_grid_lset TYPE lvc_s_layo,
e_row TYPE LVC_S_ROW,
e_column TYPE lvc_s_col,
es_rowid TYPE lvc_s_roid,
grid_container1 TYPE REF TO cl_gui_custom_container,
grid1 TYPE REF TO cl_gui_alv_grid,
ls_layout TYPE kkblo_layout,
lt_fieldcat_wa TYPE kkblo_fieldcat,
l_mode TYPE raw4,
celltab TYPE LVC_T_STYL,
wa_celltab TYPE lvc_s_styl,
lt_fieldcat TYPE kkblo_t_fieldcat,
l_tabname TYPE slis_tabname.
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.
SET HANDLER Z_OBJECT->handle_data_changed_finished FOR grid1.
SET HANDLER Z_OBJECT->on_dubbelklik FOR grid1.
CALL METHOD grid1->register_edit_event
EXPORTING
i_event_id = cl_gui_alv_grid=>mc_evt_enter.
ENDMETHOD.
METHOD refresh_grid.
CALL METHOD cl_gui_cfw=>flush.
CALL METHOD grid1->refresh_table_display.
ENDMETHOD.
METHOD on_dubbelklik.
CALL METHOD me->dubbleklik
EXPORTING
e_row = e_row
e_column = e_column
es_row_no = es_row_no
program = sy-repid.
break-point 1.
ENDMETHOD.
METHOD handle_data_changed.
Insert user code here if required
this method is entered if user ENTERS DATA.
ENDMETHOD.
METHOD handle_data_changed_finished.
Insert user code here if required
Method entered here after data entry has finished.
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 .
ASSIGN it_fldcat[] TO <field_catalog>.
ENDLOOP.
ASSIGN it_fldcat[] TO <field_catalog>.
ENDMETHOD.
METHOD download_to_excel.
break-point 5.
CALL FUNCTION 'LVC_TRANSFER_TO_KKBLO'
EXPORTING
it_fieldcat_lvc = <field_catalog>
is_layout_lvc = m_cl_variant->ms_layout
is_tech_complete = ' '
IMPORTING
es_layout_kkblo = ls_layout
et_fieldcat_kkblo = lt_fieldcat.
LOOP AT lt_fieldcat INTO lt_fieldcat_wa.
CLEAR lt_fieldcat_wa-tech_complete.
IF lt_fieldcat_wa-tabname IS initial.
lt_fieldcat_wa-tabname = '1'.
MODIFY lt_fieldcat FROM lt_fieldcat_wa.
ENDIF.
l_tabname = lt_fieldcat_wa-tabname.
ENDLOOP.
CALL FUNCTION 'ALV_XXL_CALL'
EXPORTING
i_tabname = l_tabname
is_layout = ls_layout
it_fieldcat = lt_fieldcat
i_title = sy-title
TABLES
it_outtab = <dyn_table>
EXCEPTIONS
fatal_error = 1
no_display_possible = 2
others = 3.
IF sy-subrc <> 0.
message id sy-msgid type 'S' number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
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.
struct_grid_lset-edit = 'X'. "To enable editing in ALV
struct_grid_lset-grid_title = 'Bulkwijzigingen inkoopprijzen'.
struct_grid_lset-ctab_fname = 'T_CELLCOLORS'.
struct_grid_lset-stylefname = 'CELLTAB'.
CALL METHOD grid1->set_ready_for_input
EXPORTING
i_ready_for_input = '1'.
CALL METHOD grid1->set_table_for_first_display
EXPORTING
is_layout = struct_grid_lset
CHANGING
it_outtab = <dyn_table>
it_fieldcatalog = it_fldcat.
ENDMETHOD.
METHOD change_title.
CALL METHOD grid1->set_gridtitle
EXPORTING
i_gridtitle = i_gridtitle.
ENDMETHOD.
METHOD on_user_command.
CASE e_ucomm.
WHEN 'EXIT'.
LEAVE PROGRAM.
WHEN 'EXCEL'.
CALL METHOD me->download_to_excel.
WHEN 'SAVE'.
WHEN 'VERW'.
CALL METHOD me->verwerk
EXPORTING
PROGRAM = SY-REPID.
ENDCASE.
ENDMETHOD. "on_user_command
METHOD on_toolbar.
User can add extra functionality by adding extra
buttons if required. Functionality can also be simplified by removing buttons.
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.
CLEAR ls_toolbar.
MOVE 0 TO ls_toolbar-butn_type.
MOVE 'EDIT' TO ls_toolbar-function.
MOVE SPACE TO ls_toolbar-disabled.
MOVE icon_toggle_display_change TO ls_toolbar-icon.
MOVE 'Edit data' TO ls_toolbar-quickinfo.
MOVE 'EDIT' TO ls_toolbar-text.
APPEND ls_toolbar TO e_object->mt_toolbar.
CLEAR ls_toolbar.
MOVE 0 TO ls_toolbar-butn_type.
MOVE 'VERW' TO ls_toolbar-function.
MOVE SPACE TO ls_toolbar-disabled.
MOVE icon_businav_process to ls_toolbar-icon.
MOVE 'Verw.' TO ls_toolbar-quickinfo.
MOVE 'VERW' TO ls_toolbar-text.
APPEND ls_toolbar TO e_object->mt_toolbar.
CLEAR ls_toolbar.
MOVE 0 TO ls_toolbar-butn_type.
MOVE 'EXCEL' TO ls_toolbar-function.
MOVE SPACE TO ls_toolbar-disabled.
MOVE icon_xxl TO ls_toolbar-icon.
MOVE 'Excel' TO ls_toolbar-quickinfo.
MOVE 'EXCEL' TO ls_toolbar-text.
APPEND ls_toolbar TO e_object->mt_toolbar.
ENDMETHOD.
METHOD verwerk.
PERFORM verwerk IN PROGRAM (program).
LEAVE PROGRAM.
ENDMETHOD.
METHOD dubbleklik.
PERFORM dubbleklik IN PROGRAM (program)
USING
e_row
e_column
es_row_no.
ENDMETHOD.
ENDCLASS.
Reward all helpfull answers
Regards
Pavan
2007 Aug 04 11:14 AM
hi pavan,
thanxalot for ur reply
cud u pls let me know how to create an alv interactive using classes,,,,i.e,, using the procedure which u have given for creating field catalog dynamically..
pls pls help me
its really urgent
regards,
jack
2007 Aug 04 12:16 PM
2007 Aug 04 12:32 PM
hi pavan,
pls give me ur mail id..ill send my code as attachment
the requirement is im converting an existing normal interactive report into alv interactive report..but im getting short dump at reuse_alv_list_display//
pls give me ur mail-id
so that i can send my code..
my email id: happy_sdn@yahoo.co.in..give a test mail.
regards,
jack