Showing posts with label ALV. Show all posts
Showing posts with label ALV. Show all posts

Tuesday, January 24, 2012

ABAP: Display ALV without creating screens/containers


In traditional approach we always define a container areas and then pout the ALV in this container.
But the same can also be achieved by making use of static methods of container classes.
e.g
Data lr_grid type ref to cl_gui_alv_grid.

  CREATE OBJECT lr_grid
      EXPORTING
           i_parent = cl_gui_custom_container=>screen0.
                        or cl_gui_container=>default_screen.
 " instead of creating custom container you can use standard screens

lr_grid->set_table_for_first_display( .... ).

This is a simple example and you can do lot more stuff with standard functions or think of using SALV.

Cheers,
Sandeep

Saturday, December 11, 2010

ALV : Drag and Drop on rows


REPORT zalv_dragndrop.


*Structure declaration for T016T


TYPES : BEGIN OF ty_t016t,


brsch TYPE brsch,


brtxt TYPE text1_016t,


spras TYPE spras,


END OF ty_t016t.


*Work area and internal table for T016T


DATA : it_t016t TYPE STANDARD TABLE OF ty_t016t,


wa_t016t TYPE ty_t016t.


*class declaration


CLASS lcl_objdragdropapp DEFINITION DEFERRED.


*data declarations for alv


DATA: c_dragdropapp TYPE REF TO lcl_objdragdropapp,


c_dockingcont TYPE REF TO cl_gui_docking_container,


c_alv TYPE REF TO cl_gui_alv_grid,


* reference variable to CL_DRAGDROP:


c_dragdropalv TYPE REF TO cl_dragdrop,


it_layout TYPE lvc_s_layo,


it_fcat TYPE lvc_t_fcat. "Field catalogue


*declarations for handle event


DATA: effect TYPE i,


handle_alv TYPE i.


*start of selection event


START-OF-SELECTION.


*select data


PERFORM fetch_data.


*ALV output


PERFORM alv_output.


* Class definitions and method implementation for drag and drop


CLASS lcl_dragdrop DEFINITION.


PUBLIC SECTION.


DATA: wa TYPE ty_t016t,


index TYPE i. "Index of Line to be moved


ENDCLASS. "LCL_DRAGDROP DEFINITION


*Application class definition


CLASS lcl_objdragdropapp DEFINITION.


PUBLIC SECTION.


METHODS:


*Handling Event Drag


handle_alv_drag


FOR EVENT ondrag


OF cl_gui_alv_grid


IMPORTING e_row e_column e_dragdropobj,


*Handling event DROP


handle_alv_drop


FOR EVENT ondrop


OF cl_gui_alv_grid


IMPORTING e_row e_column e_dragdropobj.


ENDCLASS. "LCL_objdragdropapp DEFINITION


*Application class implementation


CLASS lcl_objdragdropapp IMPLEMENTATION.


* OnDrag event is used to 'fetch' information from the drag source.


METHOD handle_alv_drag.


DATA: dataobj TYPE REF TO lcl_dragdrop,


line TYPE ty_t016t.


* Read dragged row


READ TABLE it_t016t INDEX e_row-index INTO line.


* create and fill dataobject for events ONDROP


CREATE OBJECT dataobj.


* Remembering row index to move a line


MOVE e_row-index TO dataobj->index.


* store the dragged line.


READ TABLE it_t016t INTO dataobj->wa INDEX e_row-index.


* Assigning data object to the refering event parameter


e_dragdropobj->object = dataobj.


ENDMETHOD. "HANDLE_ALV_DRAG


*Event handler for event 'OnDrop'. This event is used


*to use your dragged information in combination with your drop source.


METHOD handle_alv_drop.


DATA: dataobj TYPE REF TO lcl_dragdrop,


drop_index TYPE i,


stable TYPE lvc_s_stbl.


* Refresh Alv Grid Control without scrolling


stable-row = 'X'.


stable-col = 'X'.


* Catch-Statement to ensure the drag&drop-Operation is aborted properly.


CATCH SYSTEM-EXCEPTIONS move_cast_error = 1.


dataobj ?= e_dragdropobj->object.


DELETE it_t016t INDEX dataobj->index.


INSERT dataobj->wa INTO it_t016t INDEX e_row-index.


*Refreshing the ALV


CALL METHOD c_alv->refresh_table_display


EXPORTING


i_soft_refresh = 'X'


is_stable = stable.


ENDCATCH.


IF sy-subrc <> 0.


* If anything went wrong aborting the drag and drop operation:


CALL METHOD e_dragdropobj->abort.


ENDIF.


ENDMETHOD. "HANDLE_ALV_DROP


ENDCLASS. "LCL_objdragdropapp IMPLEMENTATION


*&---------------------------------------------------------------------*


*& Form alv_output


*&---------------------------------------------------------------------*


FORM alv_output .


CALL SCREEN 600.


ENDFORM. " alv_output


** Calling the ALV screen with custom container


*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


*&---------------------------------------------------------------------*
MODULE status_0600 OUTPUT.


* SET PF-STATUS 'xxxxxxxx'.


* SET TITLEBAR 'xxx'.


IF c_alv IS INITIAL.


PERFORM alv_controls.


ENDIF.


ENDMODULE. " STATUS_0600 OUTPUT


*&---------------------------------------------------------------------*


*& Form alv_CONTROLS


*&---------------------------------------------------------------------*


FORM alv_controls.


* create docking container for alv control


CREATE OBJECT c_dockingcont


EXPORTING


dynnr = '600'


extension = 300


side = cl_gui_docking_container=>dock_at_top.


* create alv control


CREATE OBJECT c_alv


EXPORTING i_parent = c_dockingcont.


* create the application object to handle the ABAP Objects Events


CREATE OBJECT c_dragdropapp.


* Events alv control


*For Dragging


SET HANDLER c_dragdropapp->handle_alv_drag FOR c_alv.


*For Dropping


SET HANDLER c_dragdropapp->handle_alv_drop FOR c_alv.


* build tree nodes for drag&drop


PERFORM build_handle.


* Fieldcatalogue for ALV


PERFORM alv_build_fieldcat.


* ALV attributes FOR LAYOUT


PERFORM alv_report_layout.


* Call ALV GRID


CALL METHOD c_alv->set_table_for_first_display


EXPORTING


is_layout = it_layout


CHANGING


it_outtab = it_t016t


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.


ENDFORM. "ALV_CONTROLS


*&---------------------------------------------------------------------*


*& Form build_handle


*&---------------------------------------------------------------------*
FORM build_handle.


* define a drag & Drop behaviour for the whole grid


CREATE OBJECT c_dragdropalv.


effect = cl_dragdrop=>move + cl_dragdrop=>copy.


CALL METHOD c_dragdropalv->add


EXPORTING


flavor = 'Line'


dragsrc = 'X'


droptarget = 'X'


effect = effect.


*getting the handle for drag and drop


CALL METHOD c_dragdropalv->get_handle


IMPORTING


handle = handle_alv.


ENDFORM. " build_handle


*&---------------------------------------------------------------------*


*& Form fetch_data


*&---------------------------------------------------------------------*
FORM fetch_data .


* select and display data from t016


SELECT brtxt brsch spras FROM t016t INTO CORRESPONDING FIELDS OF TABLE it_t016t


WHERE spras = 'EN'.


ENDFORM. " fetch_data


*&---------------------------------------------------------------------*


*& Form alv_report_layout


*&---------------------------------------------------------------------*
FORM alv_report_layout .


it_layout-grid_title = 'ALV Drag Drop'.


* provide handle to alv control to all rows for same drag & drop behaviour


it_layout-s_dragdrop-row_ddid = handle_alv.


ENDFORM. " alv_report_layout


*&---------------------------------------------------------------------*


*& Form alv_build_fieldcat


*&---------------------------------------------------------------------*
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 = 'BRSCH'.


lv_fldcat-tabname = 'IT_T016T'.


lv_fldcat-outputlen = 8.


lv_fldcat-scrtext_m = 'Industry'.


APPEND lv_fldcat TO it_fcat.


CLEAR lv_fldcat.


lv_fldcat-row_pos = '1'.


lv_fldcat-col_pos = '2'.


lv_fldcat-fieldname = 'BRTXT'.


lv_fldcat-tabname = 'IT_T016T'.


lv_fldcat-outputlen = 15.


lv_fldcat-scrtext_m = 'Description'.


APPEND lv_fldcat TO it_fcat.


CLEAR lv_fldcat.


ENDFORM. " alv_build_fieldcat


* 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


*&---------------------------------------------------------------------*

MODULE user_command_0600 INPUT.


ENDMODULE. " USER_COMMAND_0600 INPUT






Sunday, December 5, 2010

ABAP: Convert Spool List to ALV Grid

Hello,
At times we need to submit programs in background mode(batch jobs) , but the output needs to be displayed in the form of ALVs.
Here is sample code for the same.

Convert Spool List to ALV Grid



FUNCTION ztest_conv_spool_list_alv.


*"----------------------------------------------------------------------


*"*"Local Interface:


*" IMPORTING


*" REFERENCE(I_SPOOLN) TYPE CHAR10


*" REFERENCE(I_KEEP_SUM_LINE) TYPE BOOLEAN DEFAULT 'X'


*" REFERENCE(I_START_TABLE) TYPE INT4 DEFAULT 1


*"----------------------------------------------------------------------


TYPE-POOLS: slis.


TABLES: tsp01.


DATA: BEGIN OF gtbl_data OCCURS 0,


data(2048) TYPE c,


END OF gtbl_data.


DATA: BEGIN OF gtbl_col OCCURS 0,


col(255) TYPE c,


END OF gtbl_col.


DATA gv_line TYPE i.


DATA gv_length TYPE i.


DATA gv_cnt TYPE i.


DATA gv_dec TYPE i.


DATA gv_row(2048) TYPE c. "string.


DATA gv_row_d(2048) TYPE c. "string.


DATA gv_spoolnum TYPE tsp01-rqident.


DATA gtbl_fieldcat TYPE slis_t_fieldcat_alv.


DATA gstr_fieldcat LIKE LINE OF gtbl_fieldcat.


DATA gstr_data LIKE LINE OF gtbl_data.


DATA gstr_data_d LIKE LINE OF gtbl_data.


DATA gtbl_match TYPE match_result_tab.


DATA gtbl_match_last TYPE match_result_tab.


DATA gtbl_fcat TYPE lvc_t_fcat.


DATA gstr_fcat LIKE LINE OF gtbl_fcat.

DATA gref_data TYPE REF TO data.


DATA gref_new_line TYPE REF TO data.


FIELD-SYMBOLS: <fs_data> TYPE REF TO data.


FIELD-SYMBOLS: <fs_dyntable> TYPE STANDARD TABLE.


FIELD-SYMBOLS: <fs_match> LIKE LINE OF gtbl_match.


FIELD-SYMBOLS: <fs_dynline> TYPE ANY.


FIELD-SYMBOLS: <fs_dynstruct> TYPE ANY.


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


" Read the spool


gv_spoolnum = i_spooln.


CALL FUNCTION 'RSPO_RETURN_ABAP_SPOOLJOB'


EXPORTING


rqident = gv_spoolnum


TABLES


buffer = gtbl_data.


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


" Pre-check to make sure we have a list


" simply checking the first char of the


" first and the last line


READ TABLE gtbl_data INTO gstr_data INDEX 1.


IF sy-subrc = 0.


IF gstr_data-data(1) <> '-'.


MESSAGE 'Spool does not contain any ALV list' TYPE 'E'.

ELSE.


" Number of rows


gv_line = LINES( gtbl_data ).


" Read the last line


READ TABLE gtbl_data INTO gstr_data INDEX gv_line.


IF sy-subrc = 0.


IF gstr_data-data(1) <> '-'.


MESSAGE 'Spool does not contain any ALV list' TYPE 'E'.


ENDIF.


ENDIF.


ENDIF.


ENDIF.


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


" Start with the N table in the spool.


IF i_start_table > 1.


FREE gv_line.


gv_dec = i_start_table.


SUBTRACT 1 FROM gv_dec.


gv_dec = gv_dec * 2.


" Find the end of the table N - 1


LOOP AT gtbl_data INTO gstr_data WHERE data(1) = '-'.


ADD 1 TO gv_line.


IF gv_line = gv_dec.


gv_dec = sy-tabix.


EXIT.


ENDIF.


ENDLOOP.


" Delete the rows up table N


DELETE gtbl_data FROM 1 TO gv_dec.


ENDIF.


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


" Check how many ALV list are in the spool


" and make sure if more than one the # of columns are matching


FREE gv_line .


LOOP AT gtbl_data INTO gstr_data WHERE data(1) = '-'.


ADD 1 TO gv_line.


gv_cnt = gv_line MOD 2.


" The column headings are on odd number in our find counter


IF gv_cnt <> 0.


" Save the find counter value


gv_cnt = gv_line.


" Update index to point to column heading row and read


gv_line = sy-tabix + 1.


READ TABLE gtbl_data INDEX gv_line INTO gstr_data.


gv_row = gstr_data-data.


" Find the columns: position and length


FIND ALL OCCURRENCES OF '
' IN gv_row RESULTS gtbl_match.


" Compare the previous heading w/ current


IF gtbl_match[] <> gtbl_match_last[] AND sy-tabix > 2.


MESSAGE 'Spool contains more than one ALV list where column headings are different.' TYPE 'E'.


ENDIF.


FREE: gtbl_match_last, gv_row.


gtbl_match_last[] = gtbl_match[].


FREE gtbl_match.


" Get back the find counter value


gv_line = gv_cnt.


ENDIF.


ENDLOOP.


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


" Read column heading row


READ TABLE gtbl_data INDEX 2 INTO gstr_data.


gv_row = gstr_data-data.


" Read also the first data row


" Here we are assuming tha the first row all fields


" filled in; will use this to determine mumeric or char


READ TABLE gtbl_data INDEX 4 INTO gstr_data_d.


gv_row_d = gstr_data_d-data.


" Find out the columns


FIND ALL OCCURRENCES OF '
' IN gv_row RESULTS gtbl_match.


FREE: gv_cnt, gv_line.


" Setup the field catalog for itab


LOOP AT gtbl_match ASSIGNING <fs_match>.


IF sy-tabix > 1.


" Field length


gv_length = <fs_match>offset gv_line + 1.


" Update counter used for column heading


ADD 1 TO gv_cnt.


" Used for dynamic itab


gstr_fcat-datatype = 'C'.


gstr_fcat-fieldname = gv_cnt.


gstr_fcat-intlen = gv_length.


gstr_fcat-tabname = ''.


" Debug and you will see why...


SUBTRACT 1 FROM gv_length.


" Used for ALV grid


gstr_fieldcat-reptext_ddic = gv_row+gv_line(gv_length).


gstr_fieldcat-tabname = ''.


gstr_fieldcat-fieldname = gv_cnt.


gstr_fieldcat-just = 'L'.


gstr_fieldcat-outputlen = gv_length.


APPEND gstr_fcat TO gtbl_fcat.


APPEND gstr_fieldcat TO gtbl_fieldcat.


FREE: gstr_fcat, gstr_fieldcat, gv_dec.


ENDIF.


" Start position of next column


gv_line = <fs_match>-offset + 1.


ENDLOOP.


ASSIGN gref_data TO <fs_data>.


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


" Create a dynamic table based on the number of columns above


CALL METHOD cl_alv_table_create=>create_dynamic_table


EXPORTING


it_fieldcatalog = gtbl_fcat


IMPORTING


ep_table = <fs_data>


EXCEPTIONS


generate_subpool_dir_full = 1


OTHERS = 2.


ASSIGN <fs_data>->* TO <fs_dyntable>.


" Create a new mem area


CREATE DATA gref_new_line LIKE LINE OF <fs_dyntable>.


" Now point our <FS_*> to the mem area


ASSIGN gref_new_line->* TO <fs_dynstruct>.


ASSIGN gref_new_line->* TO <fs_dynline>.


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


" Remove column headings that appears in the middle


" which are caused due to spool page-break


LOOP AT gtbl_data INTO gstr_data_d FROM 4 WHERE data = gstr_data-data.


DELETE gtbl_data.


ENDLOOP.


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


" Push data to itab


LOOP AT gtbl_data INTO gstr_data.


" The first 3 rows are col heading related


IF sy-tabix > 3 AND (


gstr_data-data(2) <> '
-' AND " Column heading row


gstr_data-data(2) <> '--' " End of list row


).


REPLACE FIRST OCCURRENCE OF '
' IN gstr_data-data WITH space.


SPLIT gstr_data AT '
' INTO TABLE gtbl_col.


gv_cnt = 0.


" Split above makes each column to a row


" Get each column


LOOP AT gtbl_col.


ADD 1 TO gv_cnt.


ASSIGN COMPONENT gv_cnt OF STRUCTURE <fs_dynstruct> TO <fs_dynline>.


" Remove space front/end


CONDENSE gtbl_col-col.


MOVE gtbl_col-col TO <fs_dynline>.


ENDLOOP.


APPEND <fs_dynstruct> TO <fs_dyntable>.


gv_cnt = 0.


FREE: gtbl_col.


ENDIF.


FREE gstr_data.


ENDLOOP.


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


" Sum line flag, keep or delete


IF i_keep_sum_line IS INITIAL.


LOOP AT <fs_dyntable> ASSIGNING <fs_dynline>.


IF <fs_dynline>(1) = '*'.


DELETE <fs_dyntable>.


ENDIF.


ENDLOOP.


ENDIF.


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


" Display


CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'


EXPORTING


it_fieldcat = gtbl_fieldcat


TABLES


t_outtab = <fs_dyntable>.


ENDFUNCTION.