‎2007 Jun 28 5:52 AM
Hi friends,
Can any one explain me how can we write the code for the ALV . I want to get an ALV output which come in an editable mode where i can edit in the output and if i click a userdefined pushbutton i should get that data updated in database and should come and get visible. How can we do this. Please tell me briefly.
Points will be granted for all the answers.
‎2007 Jun 28 6:03 AM
HI,
There are lot of example programs in the package SLIS that will be there in all R/3 systems.
Check all the programs that end with EDITXX
Regards,
Sesh
‎2007 Jun 28 6:03 AM
HI,
There are lot of example programs in the package SLIS that will be there in all R/3 systems.
Check all the programs that end with EDITXX
Regards,
Sesh
‎2007 Jun 28 6:08 AM
hi,
editable alv:
chk this link.
http://www.sapdevelopment.co.uk/reporting/alv/alvgrid_editable.htm
then u can capture the events in USER COMMAND.
http://www.sapdevelopment.co.uk/reporting/alv/alvgrid_ucomm.htm
See these threads
&----
*& Report ZLAXMI_REPORT6 *
*& *
&----
*& *
*& *
&----
REPORT ZLAXMI_REPORT6 NO STANDARD PAGE HEADING
MESSAGE-ID ZZ
LINE-SIZE 132
LINE-COUNT 65 .
TABLES: MARA.
TYPE-POOLS: SLIS.
TYPES: BEGIN OF T_MARA,
MATNR TYPE MARA-MATNR, "Material Number
ERSDA TYPE MARA-ERSDA, "Creation date
BRGEW TYPE MARA-BRGEW, "Gross weight
NTGEW TYPE MARA-NTGEW, "Net weight
MTART TYPE MARA-MTART, "Material type
MBRSH TYPE MARA-MBRSH, "Industry Sector
REC_SEL TYPE C , "checkbox
END OF T_MARA.
----
V A R I A B L E S
----
DATA: V_REPID LIKE SY-REPID,
V_FLAG(1) TYPE C.
CONSTANTS :
C_X(1) TYPE C VALUE 'X',
C_PF_STATUS TYPE SLIS_FORMNAME VALUE 'F_SET_PF_STATUS',
C_FC_DELETE(6) TYPE C VALUE 'DELETE',
C_FC_MODIFY(6) TYPE C VALUE 'MODIFY',
C_USER_COMMAND TYPE SLIS_FORMNAME VALUE 'F_USER_COMMAND',
C_USER_COMMAND_MODIFY TYPE SLIS_FORMNAME
VALUE 'F_USER_COMMAND_MODIFY',
C_ICON_DELETE TYPE ICON-NAME VALUE 'ICON_DELETE', " Icon,Delete'
C_ICON_CANCEL TYPE ICON-NAME VALUE 'ICON_CANCEL', " Icon,Cancel'
C_FC_SAVE(4) TYPE C VALUE 'SAVE'.
*internal table declarations.
DATA: IT_MARA TYPE STANDARD TABLE OF T_MARA WITH HEADER LINE,
IT_MODIFY TYPE STANDARD TABLE OF T_MARA WITH HEADER LINE,
IT_TEMP TYPE STANDARD TABLE OF MARA WITH HEADER LINE,
*-ALV Internal Tables.
*--Field Catalog
IT_FIELDCAT TYPE STANDARD TABLE OF
SLIS_FIELDCAT_ALV WITH HEADER LINE,
*--Layout
WA_LAYOUT TYPE SLIS_LAYOUT_ALV,
*--Sort
IT_SORT TYPE SLIS_T_SORTINFO_ALV,
WA_SORT TYPE SLIS_SORTINFO_ALV ,
**-Structure for excluding function codes
WA_EXTAB TYPE SLIS_EXTAB,
**-To hold function codes to be excluded in ALV toolbar
IT_EXTAB TYPE SLIS_T_EXTAB.
*selection screen.
SELECTION-SCREEN: BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-001.
SELECT-OPTIONS: S_MATNR FOR MARA-MATNR,
S_ERSDA FOR MARA-ERSDA.
SELECTION-SCREEN: END OF BLOCK B1.
*--Radio buttons to select either Display/Delete/Modify
SELECTION-SCREEN BEGIN OF BLOCK B2 WITH FRAME
TITLE TEXT-002.
SELECTION-SCREEN : BEGIN OF LINE.
SELECTION-SCREEN COMMENT 1(20) TEXT-003.
PARAMETERS: P_DISP RADIOBUTTON GROUP RAD1 DEFAULT 'X'.
SELECTION-SCREEN : END OF LINE.
SELECTION-SCREEN : BEGIN OF LINE.
SELECTION-SCREEN COMMENT 1(20) TEXT-005.
PARAMETERS: P_UPD RADIOBUTTON GROUP RAD1.
SELECTION-SCREEN : END OF LINE.
SELECTION-SCREEN : BEGIN OF LINE.
SELECTION-SCREEN COMMENT 1(20) TEXT-004.
PARAMETERS: P_DEL RADIOBUTTON GROUP RAD1.
SELECTION-SCREEN : END OF LINE.
SELECTION-SCREEN END OF BLOCK B2 .
AT SELECTION-SCREEN.
PERFORM VALIDATE_SCREEN.
*start of selection
START-OF-SELECTION.
*clear the internal tables to be used.
CLEAR: IT_MARA,IT_MARA[],
V_FLAG.
*get the data
PERFORM GET_DATA.
*end of selection
END-OF-SELECTION.
IF IT_MARA[] IS INITIAL.
MESSAGE I000 WITH
'No Records found for the given Selection Criteria'(012).
ELSE.
*do alv process
V_REPID = SY-REPID.
*--Sort the Output Fields
PERFORM SORT_FIELDS.
*--Build Field catalog for the Output fields
PERFORM BUILD_FIELDCAT.
*--Set the Layout for ALV
PERFORM SET_LAYOUT.
IF P_DISP = C_X.
*--Exclude any Buttons on the Appn tool bar
perform change_default_pf_status.
MOVE C_FC_DELETE TO WA_EXTAB-FCODE. " DELETE button on ALV
APPEND WA_EXTAB TO IT_EXTAB.
MOVE C_FC_MODIFY TO WA_EXTAB-FCODE. " MODIFY button on ALV
APPEND WA_EXTAB TO IT_EXTAB.
ELSEIF P_UPD = C_X.
*--Exclude DELETE Button on the appn tool bar
MOVE C_FC_DELETE TO WA_EXTAB-FCODE. " DELETE button on ALV
APPEND WA_EXTAB TO IT_EXTAB.
ELSEIF P_DEL = C_X.
*--Exclude MODIFY button on appn tool bar
MOVE C_FC_MODIFY TO WA_EXTAB-FCODE. " MODIFY button on ALV
APPEND WA_EXTAB TO IT_EXTAB.
ENDIF.
*--Exclude SAVE button for all options
MOVE 'SAVE' TO WA_EXTAB-FCODE. " SAVE button on ALV
APPEND WA_EXTAB TO IT_EXTAB.
*--Display ALV output
PERFORM LIST_DISPLAY TABLES IT_MARA
USING C_USER_COMMAND.
ENDIF.
&----
*& Form get_data
&----
text
----
--> p1 text
<-- p2 text
----
FORM GET_DATA .
SELECT MATNR
ERSDA
BRGEW
NTGEW
MTART
MBRSH
FROM MARA
INTO TABLE IT_MARA
WHERE MATNR IN S_MATNR
AND ERSDA IN S_ERSDA.
IF SY-SUBRC <> 0.
*no records selected leave processing
STOP.
ENDIF.
SORT IT_MARA.
ENDFORM. " get_data
&----
*& Form sort_fields
&----
text
----
--> p1 text
<-- p2 text
----
FORM SORT_FIELDS .
CLEAR WA_SORT.
WA_SORT-FIELDNAME = 'MATNR'.
WA_SORT-SPOS = '1'.
WA_SORT-UP = 'X'.
APPEND WA_SORT TO IT_SORT.
CLEAR WA_SORT.
WA_SORT-FIELDNAME = 'ERSDA'.
WA_SORT-SPOS = '2'.
WA_SORT-UP = 'X'.
APPEND WA_SORT TO IT_SORT.
ENDFORM. " sort_fields
&----
*& Form build_fieldcat
&----
text
----
--> p1 text
<-- p2 text
----
FORM BUILD_FIELDCAT .
IT_FIELDCAT-COL_POS = '1'.
IT_FIELDCAT-FIELDNAME = 'MATNR'.
IT_FIELDCAT-KEY = 'X'.
IT_FIELDCAT-OUTPUTLEN = '15'.
IT_FIELDCAT-SELTEXT_L = 'Material number'(022).
APPEND IT_FIELDCAT.
CLEAR IT_FIELDCAT.
IT_FIELDCAT-COL_POS = '2'.
IT_FIELDCAT-FIELDNAME = 'ERSDA'.
IT_FIELDCAT-KEY = 'X'.
IT_FIELDCAT-OUTPUTLEN = '10'.
IT_FIELDCAT-SELTEXT_L = 'Created on'(023).
APPEND IT_FIELDCAT.
CLEAR IT_FIELDCAT.
IT_FIELDCAT-COL_POS = '3'.
IT_FIELDCAT-FIELDNAME = 'BRGEW'.
IT_FIELDCAT-OUTPUTLEN = '10'.
IT_FIELDCAT-SELTEXT_L = 'GROSS WEIGHT'(024).
APPEND IT_FIELDCAT.
CLEAR IT_FIELDCAT.
IT_FIELDCAT-COL_POS = '4'.
IT_FIELDCAT-FIELDNAME = 'NTGEW'.
IT_FIELDCAT-OUTPUTLEN = '15'.
IT_FIELDCAT-SELTEXT_L = 'NET WEIGHT'(025).
APPEND IT_FIELDCAT.
CLEAR IT_FIELDCAT.
IT_FIELDCAT-COL_POS = '5'.
IT_FIELDCAT-FIELDNAME = 'MTART'.
IT_FIELDCAT-OUTPUTLEN = '3'.
IT_FIELDCAT-SELTEXT_L = 'Material type'(026).
APPEND IT_FIELDCAT.
CLEAR IT_FIELDCAT.
IT_FIELDCAT-COL_POS = '6'.
IT_FIELDCAT-FIELDNAME = 'MBRSH'.
IT_FIELDCAT-OUTPUTLEN = '25'.
IT_FIELDCAT-SELTEXT_L = 'Industry sector'(027).
APPEND IT_FIELDCAT.
CLEAR IT_FIELDCAT.
IT_FIELDCAT-FIELDNAME = 'REC_SEL'.
IT_FIELDCAT-NO_OUT = C_X.
APPEND IT_FIELDCAT.
CLEAR IT_FIELDCAT.
ENDFORM. " build_fieldcat
&----
*& Form list_display
&----
text
----
-->P_IT_MARA text
-->P_C_USER_COMMAND text
----
FORM LIST_DISPLAY TABLES P_IT_MARA
USING P_USER_COMMAND TYPE SLIS_FORMNAME.
CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
EXPORTING
I_CALLBACK_PROGRAM = V_REPID
I_CALLBACK_PF_STATUS_SET = C_PF_STATUS
I_CALLBACK_USER_COMMAND = P_USER_COMMAND
IS_LAYOUT = WA_LAYOUT
IT_FIELDCAT = IT_FIELDCAT[]
IT_EXCLUDING = IT_EXTAB[]
IT_SORT = IT_SORT[]
TABLES
T_OUTTAB = P_IT_MARA
EXCEPTIONS
PROGRAM_ERROR = 1
OTHERS = 2.
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. " list_display
&----
*& Form F_SET_PF_STATUS
&----
Set PF_STATUS STANDARD modifying the standard toolbar
by excluding some buttons
----
-->P_IT_EXTAB -- TABLE OF EXCLUDING FUNCTIONS
----
FORM F_SET_PF_STATUS USING RT_EXTAB TYPE SLIS_T_EXTAB.
CLEAR : WA_EXTAB,
IT_EXTAB.
*--Set the Modified PF status for the ALV.
SET PF-STATUS 'ALV_STATUS_01' EXCLUDING RT_EXTAB.
ENDFORM. " SET_PF_STATUS
&----
*& Form f_user_command
&----
Handle user action on ALV toolbar
----
FORM F_USER_COMMAND USING R_UCOMM LIKE SY-UCOMM
RS_SELFIELD TYPE SLIS_SELFIELD.
RS_SELFIELD-REFRESH = C_X.
IF R_UCOMM = C_FC_DELETE.
*--User Selected DELETE button.
PERFORM DELETE_SELECTED_RECORDS.
ELSEIF R_UCOMM = C_FC_MODIFY.
*--If user selects MODIFY button.
PERFORM CHANGE_RECORDS.
ENDIF.
ENDFORM. "F_USER_COMMAND
&----
*& Form POP_UP_CONFIRMATION
&----
text
----
<--P_ANSWER text
----
FORM POP_UP_CONFIRMATION
CHANGING P_ANSWER TYPE C.
DATA:
L_TITLE(14) TYPE C, " Title of pop-up
L_TXT_QUESTION(52) TYPE C, " Text displayed in pop-up
L_DISP_CANCEL TYPE C, " Display 'Cancel' button?
L_BTN1 TYPE ICON-NAME, " Icon on button 1
L_BTN2 TYPE ICON-NAME. " Icon on button 2
L_TITLE = 'Delete'(017). " delete
L_TXT_QUESTION = 'Are you sure to delete?'(018).
L_BTN1 = C_ICON_DELETE.
L_BTN2 = C_ICON_CANCEL.
**-Display pop-up asking user for confirmation
CALL FUNCTION 'POPUP_TO_CONFIRM'
EXPORTING
TITLEBAR = L_TITLE
TEXT_QUESTION = L_TXT_QUESTION
TEXT_BUTTON_1 = 'Yes'
ICON_BUTTON_1 = L_BTN1
TEXT_BUTTON_2 = 'No'
ICON_BUTTON_2 = L_BTN2
DEFAULT_BUTTON = '2'
DISPLAY_CANCEL_BUTTON = 'X'
IMPORTING
ANSWER = P_ANSWER
EXCEPTIONS
TEXT_NOT_FOUND = 1
OTHERS = 2.
IF SY-SUBRC <> 0.
MESSAGE E000 WITH 'Error executing function module:'(019)
'POPUP_TO_CONFIRM'.
ENDIF.
ENDFORM. "pop_up_confirmation
&----
*& Form set_layout
&----
text
----
--> p1 text
<-- p2 text
----
FORM SET_LAYOUT .
IF P_DEL = C_X OR P_UPD = C_X.
*--Allow Input only if user choose 'UPDATE'/ 'DELETE' radio buttons
WA_LAYOUT-BOX_FIELDNAME = 'REC_SEL'.
WA_LAYOUT-BOX_TABNAME = 'IT_MARA'.
ENDIF.
*--Display Header based on the user selection
IF P_DISP = C_X.
WA_LAYOUT-WINDOW_TITLEBAR =
'Display '(036).
ELSEIF P_DEL = C_X.
WA_LAYOUT-WINDOW_TITLEBAR =
'Delete '(037).
ELSEIF P_UPD = C_X.
WA_LAYOUT-WINDOW_TITLEBAR =
'Change '(038).
ENDIF.
ENDFORM. " set_layout
&----
*& Form change_default_pf_status
&----
text
----
--> p1 text
<-- p2 text
----
FORM CHANGE_DEFAULT_PF_STATUS .
MOVE C_FC_DELETE TO WA_EXTAB-FCODE. " DELETE button on ALV
APPEND WA_EXTAB TO IT_EXTAB.
MOVE C_FC_MODIFY TO WA_EXTAB-FCODE. " MODIFY button on ALV
APPEND WA_EXTAB TO IT_EXTAB.
ENDFORM. " change_default_pf_status
&----
*& Form f_user_command
&----
Handle user action on ALV toolbar
----
FORM F_USER_COMMAND_MODIFY USING R_UCOMM LIKE SY-UCOMM "#EC *
RS_SELFIELD TYPE SLIS_SELFIELD.
DATA : L_ANSWER TYPE C. "#EC *
IF R_UCOMM = C_FC_SAVE.
*user selected save button
READ TABLE IT_MODIFY WITH KEY REC_SEL = 'X'.
*check if user selected atleast one line.
IF SY-SUBRC <> 0.
MESSAGE I000 WITH 'No record(s) Selected to Modify'(016).
EXIT.
ENDIF.
LOOP AT IT_MODIFY WHERE REC_SEL = 'X'.
*--Check the entered values are valid or not.
IF NOT ( it_modify-status = 'A' OR
it_modify-status = 'C' OR
it_modify-status = 'E' ).
*--User Entered invalid value for STATUS field,so Display Error Msg
CLEAR R_UCOMM.
MESSAGE e000 WITH 'Invalid value '''(031)
'' for Status in the Record # '(032)
sy-tabix.
ENDIF.
ENDLOOP.
CLEAR: IT_TEMP,
IT_TEMP[].
LOOP AT IT_MODIFY WHERE REC_SEL = 'X'.
MOVE-CORRESPONDING IT_MODIFY TO IT_TEMP.
APPEND IT_TEMP.
CLEAR IT_TEMP.
ENDLOOP.
*--start new code
DATA : IT_TEMP2 LIKE STANDARD TABLE OF MARA WITH HEADER LINE.
SELECT * FROM MARA
INTO TABLE IT_TEMP2
FOR ALL ENTRIES IN IT_TEMP
WHERE MATNR = IT_TEMP-MATNR.
IF SY-SUBRC = 0.
LOOP AT IT_MODIFY WHERE REC_SEL = 'X'.
READ TABLE IT_TEMP2 WITH KEY MATNR = IT_MODIFY-MATNR.
IF SY-SUBRC = 0.
IT_TEMP2-NTGEW = IT_MODIFY-NTGEW.
IT_TEMP2-BRGEW = IT_MODIFY-BRGEW.
MODIFY IT_TEMP2 INDEX SY-TABIX.
ENDIF.
ENDLOOP.
ENDIF.
*modify mara table with the changed values
MODIFY MARA FROM TABLE IT_TEMP2 .
*--end new code
IF SY-SUBRC = 0.
COMMIT WORK AND WAIT.
*--Display message with Success in Updating database
MESSAGE I000 WITH SY-DBCNT
' Record(s) has been Updated'(020).
CLEAR :
IT_MARA,
IT_MARA[].
*get-data again from database.
PERFORM GET_DATA.
RS_SELFIELD-REFRESH = C_X.
ELSE.
*--Error occurred
MESSAGE I000 WITH 'Error occured in Modifying the database'(021).
ENDIF.
ENDIF.
ENDFORM. "f_user_command_modify
&----
*& Form validate_screen
&----
text
----
--> p1 text
<-- p2 text
----
FORM VALIDATE_SCREEN .
DATA : LV_MATNR LIKE MARA-MATNR.
*--validate product
IF NOT S_MATNR[] IS INITIAL.
SELECT MATNR
INTO MARA-MATNR
FROM MARA
WHERE MATNR IN S_MATNR.
ENDSELECT.
IF SY-SUBRC <> 0.
*--Error
MESSAGE E000 WITH 'Invalid Material'(034).
ENDIF.
ENDIF.
ENDFORM. " validate_screen
&----
*& Form delete_records
&----
text
----
--> p1 text
<-- p2 text
----
FORM DELETE_RECORDS .
LOOP AT IT_MARA.
MOVE-CORRESPONDING IT_MARA TO IT_TEMP.
APPEND IT_TEMP.
CLEAR IT_TEMP.
ENDLOOP.
DELETE MARA FROM TABLE IT_TEMP.
IF SY-SUBRC = 0.
*--Successfully selected records Deleted.
COMMIT WORK AND WAIT.
*--Display Success Message to the user
MESSAGE I000 WITH SY-DBCNT
' Record(s) deleted Successfully'(010).
ELSE.
*--Error occured in deletion
MESSAGE I000 WITH 'Error occured in Deleting the Record(s)'(011).
ENDIF.
CLEAR: IT_MARA,
IT_MARA[],
IT_TEMP,
IT_TEMP[].
V_FLAG = C_X.
STOP.
ENDFORM. " delete_records
&----
*& Form delete_selected_records
&----
text
----
--> p1 text
<-- p2 text
----
FORM DELETE_SELECTED_RECORDS .
DATA : L_ANSWER(1) TYPE C.
READ TABLE IT_MARA WITH KEY REC_SEL = C_X.
IF SY-SUBRC <> 0.
MESSAGE I000 WITH 'No record(s) Selected to Delete'(013).
EXIT.
ELSE.
*--Ask for delete confirmation
PERFORM POP_UP_CONFIRMATION
CHANGING L_ANSWER.
IF L_ANSWER = '1'. " 'Yes'
CLEAR: IT_TEMP,
IT_TEMP[].
LOOP AT IT_MARA WHERE REC_SEL = 'X'.
MOVE-CORRESPONDING IT_MARA TO IT_TEMP.
APPEND IT_TEMP.
CLEAR IT_TEMP.
ENDLOOP.
DELETE MARA FROM TABLE IT_TEMP.
IF SY-SUBRC = 0.
*--Successfully selected records Deleted.
COMMIT WORK AND WAIT.
*--Display completed work information to the user
MESSAGE I000 WITH SY-DBCNT
' Record(s) deleted Successfully'(014).
*--Clear the Internal tables
CLEAR: IT_MARA,
IT_MARA[].
*--Reselects entries from Database again & display in ALV
PERFORM GET_DATA.
ELSE.
*--Error occured
MESSAGE I000 WITH
'Error occured in Deleting the Record(s)'(015).
ENDIF.
ENDIF.
ENDIF.
ENDFORM. " delete_selected_records
&----
*& Form change_records
&----
text
----
--> p1 text
<-- p2 text
----
FORM CHANGE_RECORDS .
READ TABLE IT_MARA WITH KEY REC_SEL = 'X'.
*--check user selected at least 1 record to MODIFY or not
IF SY-SUBRC <> 0.
MESSAGE I000 WITH 'No record(s) Selected to Modify'(016).
EXIT.
ELSE.
CLEAR : IT_MODIFY,
IT_MODIFY[].
LOOP AT IT_MARA WHERE REC_SEL = 'X'.
IT_MODIFY = IT_MARA.
APPEND IT_MODIFY.
CLEAR IT_MODIFY.
ENDLOOP.
*--Change PF status for this new ALV list.
PERFORM CHANGE_PF_STATUS_AGAIN.
*--Change Field Catalog to make INPUT enabled.
READ TABLE IT_FIELDCAT WITH KEY FIELDNAME = 'NTGEW'.
IT_FIELDCAT-INPUT = C_X.
MODIFY IT_FIELDCAT INDEX SY-TABIX.
*--Call ALV LIST DISPLAY with this new values.
PERFORM LIST_DISPLAY TABLES IT_MODIFY
USING C_USER_COMMAND_MODIFY.
ENDIF.
ENDFORM. " change_records
&----
*& Form change_pf_status_again
&----
text
----
--> p1 text
<-- p2 text
----
FORM CHANGE_PF_STATUS_AGAIN .
MOVE C_FC_DELETE TO WA_EXTAB-FCODE. " DELETE button on ALV
APPEND WA_EXTAB TO IT_EXTAB.
MOVE C_FC_MODIFY TO WA_EXTAB-FCODE. " MODIFY button on ALV
APPEND WA_EXTAB TO IT_EXTAB.
ENDFORM. " change_pf_status_again
‎2007 Jun 28 7:41 AM
Thanks friends for sending me the answers. I need one more to ask. Can any one send me the material for ABAP'S OOPs concept. I need to learn from scratch. Sincei know nothing about ABAP OOps concept.Please send me [email protected].
Points will be rewarded.
Satish raju
‎2007 Jun 28 7:52 AM
for alv ..check alv robot.
There is a link wherein u can go, fill in the detials and get the ready ABAP code.
Just copy paste it in SE38 and the program is ready 4 u.
The link is www.ALVROBOT.com.ar.
for ooabap i will send u the gud doc ....
‎2007 Jun 28 8:30 AM
I've answered this type of post before
First FORGET ALL THE OLD FUNCTION MODULES.
Here's an OO program which does EXACTLY what you want and can be used as a model.
Has dynamic table, dynamic Fcat, access to protected methods of a class etc etc.
Use this as a template
It has pretty well every option you need for a grid including colouring cells and adding / changing / displaying data etc etc.
PROGRAM zdynfieldcat.
class zcltest definition deferred. "For field symbol reference.
The ultimate ALV program
this program demos the following
1) Build Dynamic ITAB with fields NOT defined in DDIC
2) Build Dynamic FieldCat. Table structure is obtained via
the new RTTI functionality
3) Add the CELL colour table as a deep structure to our dynamic table
so we can colour individual cells.
4) Inherit a class so protected attributes of class cl_gui_alv_grid
can be accessed
5) Add events so we can can program functionality when the user presses the ENTER key,
or selects an action from the toolbar.
6) Display one line of a grid from our dynamic table with some cells coloured.
6) Set the Grid editable.
7) if user enters data then colour cells of entered data.
😎 show contents of new table after all data has been entered.
To use program copy the source and
Create a blank screen 100 with a custom container called CCONTAINER1.
with the screen logic shown below
As we are using EVENTS we actually don't need a PAI module so it
doesn't contain any code.
*PROCESS BEFORE OUTPUT.
MODULE STATUS_0100.
*
*PROCESS AFTER INPUT.
MODULE USER_COMMAND_0100
*
James Hawthorne
Define field symbols as these can't be defined in classes
field-symbols: <dyn_table> type standard table,
<g2> type ref to zcltest,
<g1> type ref to cl_gui_custom_container,
<data_changed> type ref to cl_alv_changed_data_protocol,
<actual_tab> type standard table,
<outtab> type table,
<fs1> type ANY,
<FS2> TYPE TABLE,
<fs3> type table,
<fs4> type table,
<fs5> type table,
<wa_dyn_table> TYPE ANY,
<w_field1> type any,
<w_field2> type any,
<w_field3> type any,
<w_field4> type any,
<w_field5> type any,
<t_colours> type any,
<t_clrs> type line of lvc_t_scol,
<t_cellcolors> TYPE lvc_t_scol,
<t_cellcolors1> type lvc_t_scol.
class zcltest definition inheriting from cl_gui_alv_grid.
public section.
types: g4 type ref to cl_gui_custom_container.
types: g3 type ref to cl_alv_changed_data_protocol.
data: i_parent type g4,
lr_rtti_struc TYPE REF TO cl_abap_structdescr, "RTTI
zog like line of lr_rtti_struc->components. "RTTI
types: struc like zog.
types: struc1 type table of struc.
methods:
constructor
importing i_parent type g4,
disp_tab
importing p_er_data_changed type g3,
create_dynamic_fcat
importing zogt type struc1
exporting it_fldcat type lvc_t_fcat.
Protected section.
private section.
data: stab type ref to data,
wa_it_fldcat type lvc_s_fcat,
c_index type sy-index,
lt_good_cells type lvc_t_modi,
ls_good type lvc_s_modi,
ls_good_cells type lvc_s_modi,
et_good_cells type LVC_T_MODI,
e_row type i,
e_value type c,
e_col type i,
es_row_id type lvc_s_row,
es_col_id type lvc_s_col,
es_row_no type LVC_S_ROID,
wa_cellcolors TYPE LINE OF lvc_t_scol,
wa_modified_cells type line of LVC_T_MODI,
es_layout type LVC_S_LAYO.
endclass.
class zcltest implementation.
METHOD constructor.
CALL METHOD super->constructor
EXPORTING
i_appl_events = 'X'
i_parent = i_parent.
endmethod.
method disp_tab.
break-point 2.
mt_outtab is the data table held as a protected attribute
in class cl_gui_alv_grid.
assign me->mt_outtab->* TO <outtab>. "Original data
assign p_er_data_changed->mp_mod_rows TO <FS1>.
stab = p_er_data_changed->mp_mod_rows.
assign p_er_data_changed->mt_inserted_rows to <fs3>.
assign p_er_data_changed->mt_deleted_rows to <fs4>.
assign p_er_data_changed->mt_mod_cells to <fs5>.
assign stab->* TO <fs2>. "Updated Rows
some functions --chose what you want
This method gets the last (or current cell)
call method me->get_current_cell
importing e_row = e_row
e_value = e_value
e_col = e_col
es_row_id = es_row_id
es_col_id = es_col_id
es_row_no = es_row_no.
For an example let's change the colour of any data entered including
inserted rows.
*
Proceed as follows
*
read table <fs5> which gives us the row number and field name of
the modified cell(s) including inserted rows
*
For deleted rows we don't have to do anything
*
Update the cell's colour characteristic table
remember we added a colour table to the dynamic data table we built
note that it's a DEEP structure.
On REFRESH the alv will look at the updated colour table for the cells.
we don't actually have to modify the data table but just the colour
table
recapping
1) Create your Field catalog WITHOUT the colour table for the ALV grid display
2) Add the colour table to a 2nd field catalog which you use to build the actual data
dynamic table
3) add the name of the colour table to the layout
(struct_grid_lset-ctab_fname = 'T_CELLCOLORS')
Refresh Grid and new cell colours will work
We "sort of cheat" here
we can add / modify the cell colour table here
the actual data content will be managed by alv in data_changed_finished.
ALV will show updated data which is what we want
BUT THE UPDATED CELL COLOUR table wil also be RE-READ by
the layout manager on refresh display.
giving us the new cell colours.
The internal alv data table doesn't contain our deep structure
*
*
*
now let's change the colour of the selected cells.
.
ASSIGN COMPONENT 'T_CELLCOLORS'
OF STRUCTURE <wa_dyn_table> TO <t_cellcolors1>.
loop at <fs5> into wa_modified_cells.
read a line from our data table (original) with the index of
the row the modified cell is in
.
read table <dyn_table> into <wa_dyn_table> index wa_modified_cells-row_id.
case sy-subrc.
when 0.
an existing table line (data) has been modified
so we can just update the colour table for that cell
read table <t_cellcolors1> into wa_cellcolors
with key fname = wa_modified_cells-fieldname.
if sy-subrc = 0.
If there is no current colour entry for the cell create one.
if not wa_modified_cells-value is initial.
wa_cellcolors-fname = wa_modified_cells-fieldname.
wa_cellcolors-color-col = '5'.
MODIFY <t_cellcolors1> from wa_cellcolors index sy-tabix.
endif.
else.
if not wa_modified_cells-value is initial.
wa_cellcolors-fname = wa_modified_cells-fieldname.
wa_cellcolors-color-col = '5'.
append wa_cellcolors to <t_cellcolors1>.
endif.
endif.
when others.
append a row from <fs2> into our data table.
<fs2> contains the changed rows in the same structure as our original table.
loop at <fs2> into <wa_dyn_table>.
append <wa_dyn_table> to <dyn_table>.
endloop.
now update the colour table (or create it) for the relevant cell(s)
of our added row(s).
read table <dyn_table> into <wa_dyn_table> index wa_modified_cells-row_id.
clear wa_cellcolors.
if not wa_modified_cells-value is initial.
wa_cellcolors-fname = wa_modified_cells-fieldname.
wa_cellcolors-color-col = 5.
append wa_cellcolors to <t_cellcolors1>.
endif.
endcase.
MODIFY <dyn_table> FROM <wa_dyn_table> index wa_modified_cells-row_id.
endloop.
endmethod.
method create_dynamic_fcat.
loop at zogt into zog.
c_index = c_index + 1.
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. "create_dynamic_fcat
endclass. "zcltest IMPLEMENTATION
class lcl_grid_event_receiver definition.
public section.
data: data_c type ref to cl_alv_changed_data_protocol.
methods:
handle_data_changed
for event data_changed of zcltest
importing er_data_changed,
handle_data_changed_finished
for event data_changed_finished of zcltest
importing E_MODIFIED
ET_GOOD_CELLS,
toolbar
for event toolbar of zcltest
importing e_object
e_interactive,
user_command
for event user_command of zcltest
importing e_ucomm.
endclass.
class lcl_grid_event_receiver implementation.
<g2> is an instance of the zcltest class
this means we can now use methods in that class
when our event handle-data_changed occurs.
*we want to use those methods as that gives us all the data functionality
of the grid
method handle_data_changed.
call method <g2>->disp_tab
EXPORTING
p_er_data_changed = er_data_changed.
endmethod. "handle_data_changed
method handle_data_changed_finished.
break-point 1.
at this point our data dynamic table
<dyn_table> now contains the actual updated values from
the grid including the cell color info create in the on data changed
event method call AND IN THE CORRECT ORDER.
use this method to update say SAP data bases or other
data from the GRID entered data.
if rows have been deleted then our new <dyn_table> will not show those either
*
Data entry validation can be done here as well.
*
Enter your own code here.
.
endmethod.
method toolbar.
data : ls_toolbar type stb_button.
clear ls_toolbar.
move 0 to ls_toolbar-butn_type.
move 'EDIT' to ls_toolbar-function.
move space to ls_toolbar-disabled.
move 'Edit' to ls_toolbar-text.
move icon_change_text to ls_toolbar-icon.
move 'Click2Edit' to ls_toolbar-quickinfo.
append ls_toolbar to e_object->mt_toolbar.
clear ls_toolbar.
move 0 to ls_toolbar-butn_type.
move 'UPDA' to ls_toolbar-function.
move space to ls_toolbar-disabled.
move 'Update' to ls_toolbar-text.
move icon_system_save to ls_toolbar-icon.
move 'Click2Update' to ls_toolbar-quickinfo.
append ls_toolbar to e_object->mt_toolbar.
clear ls_toolbar.
move 0 to ls_toolbar-butn_type.
move 'EXIT' to ls_toolbar-function.
move space to ls_toolbar-disabled.
move 'Exit' to ls_toolbar-text.
move icon_system_end to ls_toolbar-icon.
move 'Click2Exit' to ls_toolbar-quickinfo.
append ls_toolbar to e_object->mt_toolbar.
endmethod. "toolbar
method user_command.
case e_ucomm .
when 'EDIT'. "From Tool bar
perform set_input.
perform refresh_disp.
when 'UPDA'. "From Tool bar
perform refresh_disp.
when 'EXIT'. "From Tool bar
leave program.
endcase.
endmethod. "user_command
endclass. "lcl_grid_event_receiver IMPLEMENTATION
program data
*
include <icon>.
define any old internal structure NOT in DDIC
types: begin of s_elements,
anyfield1(20) type c,
anyfield2(20) type c,
anyfield3(20) type c,
anyfield4(20) type c,
anyfield5(11) type n,
end of s_elements.
data: wa_element type s_elements,
wa_cellcolors TYPE LINE OF lvc_t_scol,
wa_data type s_elements,
wa_dyn_table TYPE REF TO data.
Note new RTTI functionality allows field detail retrieval
at runtime for dynamic tables.
data:
grid1 type ref to zcltest,
grid_handler type ref to lcl_grid_event_receiver,
c_dec2 type s_elements-anyfield5,
wa_it_fldcat type lvc_s_fcat,
it_fldcat type lvc_t_fcat,
it_fldcat1 type lvc_t_fcat,
lr_rtti_struc TYPE REF TO cl_abap_structdescr, "RTTI
lt_comp TYPE cl_abap_structdescr=>component_table,"RTTI
ls_comp LIKE LINE OF lt_comp, "RTTI
zog like line of lr_rtti_struc->components, "RTTI
struct_grid_lset type lvc_s_layo,
l_valid type c,
new_table type ref to data.
types: struc like zog,
col_tab type lvc_t_scol.
data: zogt type table of struc,
grid_container1 type ref to cl_gui_custom_container,
g_event_receiver type ref to lcl_grid_event_receiver,
ok_code like sy-ucomm,
t_colours type table of col_tab,
i4 type int4.
start-of-selection.
call screen 100.
module status_0100 output.
if grid_container1 is initial.
create object grid_container1
exporting
container_name = 'CCONTAINER1'.
assign grid_container1 to <g1>.
create object grid1
exporting i_parent = grid_container1.
we need reference to this instance so we can use
Methods etc of zcltest class and alv (superclass)
in our event receiver class.
assign grid1 to <g2>.
create object grid_handler.
set handler for events
set handler:
grid_handler->user_command for grid1,
grid_handler->toolbar for grid1,
grid_handler->handle_data_changed_finished for grid1.
perform register_enter_event. "For ENTER key press
Get the Internal table structure
lr_rtti_struc ?= cl_abap_structdescr=>describe_by_data( wa_data ).
Build field catalog just use basic data here
colour specific columns as well
zogt[] = lr_rtti_struc->components.
call method grid1->create_dynamic_fcat
EXPORTING
zogt = zogt
IMPORTING
it_fldcat = it_fldcat.
Save field cat without cell info deep structure for Grid
it_fldcat1[] = it_fldcat.
now add deep structure for cell colour to field catalog
for dynamic table creation
assign t_colours to <t_colours>.
wa_it_fldcat-fieldname = 'T_CELLCOLORS'.
wa_it_fldcat-ref_field = 'COLTAB'.
wa_it_fldcat-ref_table = 'CALENDAR_TYPE'.
use this table as it has a DDIC built in ref structure for cell colour info
APPEND wa_it_fldcat TO it_fldcat1.
Create dynamic internal table and assign to field symbol.
Use dynamic field catalog just built.
call method cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = it_fldcat1
IMPORTING
ep_table = new_table.
perform populate_dynamic_itab.
perform init_grid.
i4 = 0.
call method grid1->set_ready_for_input
EXPORTING
i_ready_for_input = i4.
endif.
.
perform refresh_disp.
endmodule. "status_0100 OUTPUT
module user_command_0100 input.
*
*PAI not needed in OO ALV anymore as User Commands are handled as events
*in method user_command.
*
*we can also get control if the Data entered and the ENTER is pressed by
*raising an event.
Control then returns to method handle_data_changed.
endmodule. "user_command_0100 INPUT
form populate_dynamic_itab.
load up a line of the dynamic table
c_dec2 = c_dec2 + 11.
assign new_table->* to <dyn_table>.
create data wa_dyn_table like line of <dyn_table>.
assign wa_dyn_table->* to <wa_dyn_table>.
assign component 'ANYFIELD1' of structure <wa_dyn_table> to <w_field1>
.
<w_field1> = 'Tabbies'.
assign component 'ANYFIELD2' of structure <wa_dyn_table> to
<w_field2>.
<w_field2> = 'Ger Shepards'.
assign component 'ANYFIELD3' of structure <wa_dyn_table> to <w_field3>.
<w_field3> = 'White Mice'.
assign component 'ANYFIELD4' of structure <wa_dyn_table> to <w_field4>.
<w_field4> = 'Any old Text'.
assign component 'ANYFIELD5' of structure <wa_dyn_table> to <w_field5>.
<w_field5> = c_dec2..
APPEND <wa_dyn_table> TO <dyn_table>.
loop at <dyn_table> into <wa_dyn_table>.
clear wa_cellcolors.
ASSIGN COMPONENT 'T_CELLCOLORS'
OF STRUCTURE <wa_dyn_table> TO <t_cellcolors>.
wa_cellcolors-fname = 'ANYFIELD2'.
wa_cellcolors-color-col = '1'.
APPEND wa_cellcolors TO <t_cellcolors>.
clear wa_cellcolors.
wa_cellcolors-fname = 'ANYFIELD3'.
wa_cellcolors-color-col = '2'.
APPEND wa_cellcolors TO <t_cellcolors>.
clear wa_cellcolors.
wa_cellcolors-fname = 'ANYFIELD4'.
wa_cellcolors-color-col = '6'.
APPEND wa_cellcolors TO <t_cellcolors>.
MODIFY <dyn_table> FROM <wa_dyn_table>.
endloop.
endform. "populate_dynamic_itab
form exit_program.
call method grid_container1->free.
call method cl_gui_cfw=>flush.
leave program.
endform. "exit_program
form refresh_disp.
call method grid1->refresh_table_display.
endform. "refresh_disp
form set_input.
i4 = 1.
call method grid1->set_ready_for_input
EXPORTING
i_ready_for_input = i4.
endform. "set_input
form switch_input.
if i4 = 1.
i4 = 0.
else.
i4 = 1.
endif.
call method grid1->set_ready_for_input
EXPORTING
i_ready_for_input = i4.
endform. "switch_input
form init_grid.
Enabling the grid to edit mode,
Set ALV controls for cell coloring table.
struct_grid_lset-edit = 'X'. "To enable editing in ALV
struct_grid_lset-grid_title = 'Jimbos Test'.
struct_grid_lset-ctab_fname = 'T_CELLCOLORS'.
struct_grid_lset-sel_mode = 'D'.
call method grid1->set_table_for_first_display
EXPORTING
is_layout = struct_grid_lset
CHANGING
it_outtab = <dyn_table>
it_fieldcatalog = it_fldcat.
endform. "init_grid
form register_enter_event.
call method grid1->register_edit_event
EXPORTING
i_event_id = cl_gui_alv_grid=>mc_evt_enter.
Instantiate the event or it won't work.
create object g_event_receiver.
set handler g_event_receiver->handle_data_changed for grid1.
endform. "register_enter_event
Cheers
Jimbo