Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

Making selected alv rows editable

Hi,

I want to make one whole row in my alv as editable, while the others need to be non editable based on some calculation logic. is it possible and if so please provide me pointers...

Regards,

Pankaj

Message was edited by: Pankaj S Dwivedi

Tags:
Former Member
Former Member replied

sample code

REPORT ZTEST1234 MESSAGE-ID ZZ .

DATA: G_GRID TYPE REF TO CL_GUI_ALV_GRID, "First

G_GRID1 TYPE REF TO CL_GUI_ALV_GRID. "Second

DATA: L_VALID TYPE C,

V_FLAG,

V_DATA_CHANGE,

V_ROW TYPE LVC_S_ROW,

V_COLUMN TYPE LVC_S_COL,

V_ROW_NUM TYPE LVC_S_ROID.

DATA: OK_CODE LIKE SY-UCOMM,

SAVE_OK LIKE SY-UCOMM,

G_CONTAINER1 TYPE SCRFNAME VALUE 'TEST', "First Container

G_CONTAINER2 TYPE SCRFNAME VALUE 'TEST1',"Second container

GS_LAYOUT TYPE LVC_S_LAYO.

DATA:BEGIN OF ITAB OCCURS 0,

VBELN LIKE LIKP-VBELN,

POSNR LIKE LIPS-POSNR,

LFDAT like lips-vfdat,

BOX(1),

HANDLE_STYLE TYPE LVC_T_STYL,

END OF ITAB.

----


  • CLASS lcl_event_handler DEFINITION

----


CLASS LCL_EVENT_HANDLER DEFINITION .

PUBLIC SECTION .

METHODS:

**Hot spot Handler

HANDLE_HOTSPOT_CLICK FOR EVENT HOTSPOT_CLICK OF CL_GUI_ALV_GRID

IMPORTING E_ROW_ID E_COLUMN_ID ES_ROW_NO,

**Handler to Check the Data Change

HANDLE_DATA_CHANGED FOR EVENT DATA_CHANGED

OF CL_GUI_ALV_GRID

IMPORTING ER_DATA_CHANGED

E_ONF4

E_ONF4_BEFORE

E_ONF4_AFTER,

**Double Click Handler

HANDLE_DOUBLE_CLICK FOR EVENT DOUBLE_CLICK OF CL_GUI_ALV_GRID

IMPORTING E_ROW E_COLUMN ES_ROW_NO.

ENDCLASS. "lcl_event_handler DEFINITION

----


  • CLASS lcl_event_handler IMPLEMENTATION

----


CLASS LCL_EVENT_HANDLER IMPLEMENTATION.

*Handle Hotspot Click

METHOD HANDLE_HOTSPOT_CLICK .

CLEAR: V_ROW,V_COLUMN,V_ROW_NUM.

V_ROW = E_ROW_ID.

V_COLUMN = E_COLUMN_ID.

V_ROW_NUM = ES_ROW_NO.

MESSAGE I000 WITH V_ROW 'clicked'.

ENDMETHOD. "lcl_event_handler

*Handle Double Click

METHOD HANDLE_DOUBLE_CLICK.

CLEAR: V_ROW,V_COLUMN,V_ROW_NUM.

V_ROW = E_ROW.

V_COLUMN = E_COLUMN.

V_ROW_NUM = ES_ROW_NO.

IF E_COLUMN = 'VBELN'.

SET PARAMETER ID 'VL' FIELD ITAB-VBELN.

CALL TRANSACTION 'VL03N' AND SKIP FIRST SCREEN.

ENDIF.

IF E_COLUMN = 'POSNR'.

MESSAGE I000 WITH 'Click on POSNR row number ' E_ROW.

"with this row num you can get the data

ENDIF.

ENDMETHOD. "handle_double_click

**Handle Data Change

METHOD HANDLE_DATA_CHANGED.

CALL METHOD G_GRID->REFRESH_TABLE_DISPLAY

EXCEPTIONS

FINISHED = 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.

ENDMETHOD. "HANDLE_DATA_CHANGED

ENDCLASS. "LCL_EVENT_HANDLER IMPLEMENTATION

&----


*& Global Definitions

&----


DATA: G_CUSTOM_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER,"Container1

G_HANDLER TYPE REF TO LCL_EVENT_HANDLER, "handler

G_CUSTOM_CONTAINER1 TYPE REF TO CL_GUI_CUSTOM_CONTAINER. "Container2

*- Fieldcatalog for First and second Report

DATA: IT_FIELDCAT TYPE LVC_T_FCAT,

X_FIELDCAT TYPE LVC_S_FCAT,

LS_VARI TYPE DISVARIANT.

*----


  • START-OF_SELECTION

*----


START-OF-SELECTION.

SELECT VBELN

POSNR

FROM LIPS

UP TO 20 ROWS

INTO CORRESPONDING FIELDS OF TABLE ITAB.

END-OF-SELECTION.

IF NOT ITAB[] IS INITIAL.

CALL SCREEN 100.

ELSE.

MESSAGE I002 WITH 'NO DATA FOR THE SELECTION'(004).

ENDIF.

&----


*& Form CREATE_AND_INIT_ALV

&----


  • text

----


FORM CREATE_AND_INIT_ALV .

DATA: LT_EXCLUDE TYPE UI_FUNCTIONS.

"First Grid

CREATE OBJECT G_CUSTOM_CONTAINER

EXPORTING CONTAINER_NAME = G_CONTAINER1.

CREATE OBJECT G_GRID

EXPORTING I_PARENT = G_CUSTOM_CONTAINER.

"Second Grid

CREATE OBJECT G_CUSTOM_CONTAINER1

EXPORTING CONTAINER_NAME = G_CONTAINER2.

CREATE OBJECT G_GRID1

EXPORTING I_PARENT = G_CUSTOM_CONTAINER1.

  • Set a titlebar for the grid control

CLEAR GS_LAYOUT.

GS_LAYOUT-GRID_TITLE = TEXT-003.

GS_LAYOUT-ZEBRA = SPACE.

GS_LAYOUT-CWIDTH_OPT = 'X'.

GS_LAYOUT-NO_ROWMARK = 'X'.

GS_LAYOUT-BOX_FNAME = 'BOX'.

GS_LAYOUT-CTAB_FNAME = 'CELLCOLOR'.

GS_LAYOUT-STYLEFNAME = 'HANDLE_STYLE'.

CALL METHOD G_GRID->REGISTER_EDIT_EVENT

EXPORTING

I_EVENT_ID = CL_GUI_ALV_GRID=>MC_EVT_MODIFIED.

CREATE OBJECT G_HANDLER.

SET HANDLER G_HANDLER->HANDLE_DOUBLE_CLICK FOR G_GRID.

  • SET HANDLER G_HANDLER->HANDLE_HOTSPOT_CLICK FOR G_GRID.

SET HANDLER G_HANDLER->HANDLE_DATA_CHANGED FOR G_GRID.

data: ls_outatb like line of itab,

v_index type sy-tabix.

DATA: LS_EDIT TYPE LVC_S_STYL,

LT_EDIT TYPE LVC_T_STYL.

LOOP AT ITAB INTO ls_outatb WHERE POSNR = '000010'.

V_INDEX = SY-TABIX.

LS_EDIT-FIELDNAME = 'VBELN'.

LS_EDIT-STYLE = CL_GUI_ALV_GRID=>MC_STYLE_DISABLED.

LS_EDIT-STYLE2 = SPACE.

LS_EDIT-STYLE3 = SPACE.

LS_EDIT-STYLE4 = SPACE.

LS_EDIT-MAXLEN = 8.

INSERT LS_EDIT INTO TABLE LT_EDIT.

INSERT LINES OF LT_EDIT INTO TABLE ls_outatb-handle_style.

MODIFY ITAB INDEX V_INDEX FROM ls_outatb TRANSPORTING

HANDLE_STYLE.

ENDLOOP.

  • setting focus for created grid control

CALL METHOD CL_GUI_CONTROL=>SET_FOCUS

EXPORTING

CONTROL = G_GRID.

  • Build fieldcat and set editable for date and reason code

  • edit enabled. Assign a handle for the dropdown listbox.

PERFORM BUILD_FIELDCAT.

  • Optionally restrict generic functions to 'change only'.

  • (The user shall not be able to add new lines).

PERFORM EXCLUDE_TB_FUNCTIONS CHANGING LT_EXCLUDE.

**Vaiant to save the layout

LS_VARI-REPORT = SY-REPID.

LS_VARI-HANDLE = SPACE.

LS_VARI-LOG_GROUP = SPACE.

LS_VARI-USERNAME = SPACE.

LS_VARI-VARIANT = SPACE.

LS_VARI-TEXT = SPACE.

LS_VARI-DEPENDVARS = SPACE.

CALL METHOD G_GRID->REGISTER_EDIT_EVENT

EXPORTING

I_EVENT_ID = CL_GUI_ALV_GRID=>MC_EVT_MODIFIED.

**Calling the Method for ALV output for First Grid

CALL METHOD G_GRID->SET_TABLE_FOR_FIRST_DISPLAY

EXPORTING

IT_TOOLBAR_EXCLUDING = LT_EXCLUDE

IS_VARIANT = LS_VARI

IS_LAYOUT = GS_LAYOUT

I_SAVE = 'A'

CHANGING

IT_FIELDCATALOG = IT_FIELDCAT

IT_OUTTAB = ITAB[].

**Calling the Method for ALV output for Second Grid

CALL METHOD G_GRID1->SET_TABLE_FOR_FIRST_DISPLAY

  • EXPORTING

  • IT_TOOLBAR_EXCLUDING = LT_EXCLUDE

CHANGING

IT_FIELDCATALOG = IT_FIELDCAT

IT_OUTTAB = ITAB[].

  • Set editable cells to ready for input initially

CALL METHOD G_GRID->SET_READY_FOR_INPUT

EXPORTING

I_READY_FOR_INPUT = 1.

ENDFORM. "CREATE_AND_INIT_ALV

&----


*& Form EXCLUDE_TB_FUNCTIONS

&----


  • text

----


  • -->PT_EXCLUDE text

----


FORM EXCLUDE_TB_FUNCTIONS CHANGING PT_EXCLUDE TYPE UI_FUNCTIONS.

  • Only allow to change data not to create new entries (exclude

  • generic functions).

DATA LS_EXCLUDE TYPE UI_FUNC.

LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_LOC_COPY_ROW.

APPEND LS_EXCLUDE TO PT_EXCLUDE.

LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_LOC_DELETE_ROW.

APPEND LS_EXCLUDE TO PT_EXCLUDE.

LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_LOC_APPEND_ROW.

APPEND LS_EXCLUDE TO PT_EXCLUDE.

LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_LOC_INSERT_ROW.

APPEND LS_EXCLUDE TO PT_EXCLUDE.

LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_LOC_MOVE_ROW.

APPEND LS_EXCLUDE TO PT_EXCLUDE.

LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_LOC_COPY.

APPEND LS_EXCLUDE TO PT_EXCLUDE.

LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_LOC_CUT.

APPEND LS_EXCLUDE TO PT_EXCLUDE.

LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_LOC_PASTE.

APPEND LS_EXCLUDE TO PT_EXCLUDE.

LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_LOC_PASTE_NEW_ROW.

APPEND LS_EXCLUDE TO PT_EXCLUDE.

LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_LOC_UNDO.

APPEND LS_EXCLUDE TO PT_EXCLUDE.

ENDFORM. " EXCLUDE_TB_FUNCTIONS

&----


*& Form build_fieldcat

&----


  • Fieldcatalog

----


FORM BUILD_FIELDCAT .

DATA: L_POS TYPE I.

L_POS = L_POS + 1.

X_FIELDCAT-SCRTEXT_M = 'Delivery'(024).

X_FIELDCAT-FIELDNAME = 'VBELN'.

X_FIELDCAT-TABNAME = 'ITAB'.

X_FIELDCAT-COL_POS = L_POS.

X_FIELDCAT-NO_ZERO = 'X'.

X_FIELDCAT-EDIT = 'X'.

X_FIELDCAT-OUTPUTLEN = '10'.

APPEND X_FIELDCAT TO IT_FIELDCAT.

CLEAR X_FIELDCAT.

L_POS = L_POS + 1.

X_FIELDCAT-SCRTEXT_M = 'Item'(025).

X_FIELDCAT-FIELDNAME = 'POSNR'.

X_FIELDCAT-TABNAME = 'ITAB'.

X_FIELDCAT-COL_POS = L_POS.

X_FIELDCAT-OUTPUTLEN = '5'.

APPEND X_FIELDCAT TO IT_FIELDCAT.

CLEAR X_FIELDCAT.

L_POS = L_POS + 1.

X_FIELDCAT-SCRTEXT_M = 'Del Date'(015).

X_FIELDCAT-FIELDNAME = 'LFDAT'.

X_FIELDCAT-TABNAME = 'ITAB'.

X_FIELDCAT-COL_POS = L_POS.

X_FIELDCAT-OUTPUTLEN = '10'.

APPEND X_FIELDCAT TO IT_FIELDCAT.

CLEAR X_FIELDCAT.

L_POS = L_POS + 1.

ENDFORM. " build_fieldcat

&----


*& Module STATUS_0100 OUTPUT

&----


  • text

----


MODULE STATUS_0100 OUTPUT.

SET PF-STATUS 'MAIN100'.

SET TITLEBAR 'MAIN100'.

IF G_CUSTOM_CONTAINER IS INITIAL.

**Initializing the grid and calling the fm to Display the O/P

PERFORM CREATE_AND_INIT_ALV.

ENDIF.

ENDMODULE. " STATUS_0100 OUTPUT

&----


*& Module USER_COMMAND_0100 INPUT

&----


  • text

----


MODULE USER_COMMAND_0100 INPUT.

CASE SY-UCOMM.

WHEN 'BACK'.

LEAVE TO SCREEN 0.

ENDCASE.

ENDMODULE. " USER_COMMAND_0100 INPUT

0 View this answer in context
Not what you were looking for? View more on this topic or Ask a question