This Program will help to create dynamic ALV with dynamic editable columns and dynamic colors to the columns based on condition.
*&———————————————————————*
*& Report YSHU_SAI_ALV_DYNAMIC
*&———————————————————————*
REPORT YSHU_SAI_ALV_DYNAMIC.
TYPE-POOLS : ABAP.
FIELD-SYMBOLS: <LINE> TYPE STANDARD TABLE,
<WA> TYPE ANY.
DATA: DYN_TABLE TYPE REF TO DATA,
DYN_LINE TYPE REF TO DATA,
WA_FIELDCAT TYPE LVC_S_FCAT,
IT_FIELDCAT TYPE LVC_T_FCAT,
IT_FIELDCAT_1 TYPE LVC_T_FCAT,
OK_CODE TYPE SY-UCOMM.
TYPES: BEGIN OF TY_1,
TIME TYPE T,
REMARKS TYPE CHAR50,
END OF TY_1.
DATA: LV_FLD_1 TYPE TY_1.
START-OF-SELECTION.
* Create dynamic table stricture..
PERFORM GET_TABLE_STRUCTURE.
*Create dynamic internale table..
PERFORM CREATE_ITAB_DYNAMICALLY.
* filling the data into dynamic internal table..
PERFORM GET_DATA.
END-OF-SELECTION.
* display dynamic interal data.
PERFORM DISPLAY_ALV_REPORT.
CALL SCREEN 2000.
*&———————————————————————*
*& Form get_table_structure
*&———————————————————————*
* Get structure of an SAP table
*———————————————————————-*
FORM GET_TABLE_STRUCTURE.
DATA : REF_TABLE_DESCR TYPE REF TO CL_ABAP_STRUCTDESCR,
IT_TABDESCR TYPE ABAP_COMPDESCR_TAB,
WA_TABDESCR TYPE ABAP_COMPDESCR,
LV_NO_DAYS TYPE P,
LV_DATE TYPE SY-DATUM,
LV_DATUM TYPE SY-DATUM,
LV_DAY TYPE TEXT40,
LV_WEEK_DAY TYPE TEXT10,
LIN TYPE SY-TFILL,
C_REM TYPE STRING VALUE ‘Remarks’ .
* fill the data into fieldcatlog of static field
PERFORM BUILD_FIELDCATALOG USING IT_FIELDCAT.
* Return structure of the table.
REF_TABLE_DESCR ?= CL_ABAP_TYPEDESCR=>DESCRIBE_BY_DATA( LV_FLD_1 ).
IT_TABDESCR[] = REF_TABLE_DESCR->COMPONENTS[].
* Get no of days in a month..
CALL FUNCTION ‘HR_E_NUM_OF_DAYS_OF_MONTH’
EXPORTING
P_FECHA = SY-DATUM
IMPORTING
NUMBER_OF_DAYS = LV_NO_DAYS.
* get no of lines..
DESCRIBE TABLE IT_FIELDCAT LINES LIN .
LV_DATUM = SY-DATUM.
LV_DATUM+6(2) = 1.
WA_FIELDCAT-COL_POS = LIN.
* fill dynamic field into fields cate..
DO LV_NO_DAYS TIMES.
LV_DATE = LV_DATUM + SY-INDEX – 1.
CALL FUNCTION ‘DATE_TO_DAY’
EXPORTING
DATE = LV_DATE
IMPORTING
WEEKDAY = LV_WEEK_DAY.
CONCATENATE LV_DATE+6(2) ‘,’ LV_WEEK_DAY INTO LV_DAY.
LOOP AT IT_TABDESCR INTO WA_TABDESCR.
WA_FIELDCAT-COL_POS = WA_FIELDCAT-COL_POS + 1 .
IF SY-TABIX = 1.
WA_FIELDCAT-FIELDNAME = LV_DATE .
WA_FIELDCAT-COLTEXT = LV_DAY.
ELSE.
CONCATENATE LV_DATE ‘_R’ INTO WA_FIELDCAT-FIELDNAME.
WA_FIELDCAT-COLTEXT = C_REM.”‘Remarks’.
ENDIF.
WA_FIELDCAT-INTTYPE = WA_TABDESCR-TYPE_KIND.
WA_FIELDCAT-INTLEN = WA_TABDESCR-LENGTH / 2.
APPEND WA_FIELDCAT TO IT_FIELDCAT.
ENDLOOP.
ENDDO.
ENDFORM. “get_table_structure
*&———————————————————————*
*& Form create_itab_dynamically
*&———————————————————————*
* Create internal table dynamically
*———————————————————————-*
FORM CREATE_ITAB_DYNAMICALLY.
* Create dynamic internal table and assign to Field-Symbol
CLEAR WA_FIELDCAT.
*move fields from IT_FIELDCAT into IT_FIELDCAT_1.
IT_FIELDCAT_1 = IT_FIELDCAT.
* Use ref table CALENDAR_TYPE and ref field ‘COLTAB’
WA_FIELDCAT-FIELDNAME = ‘CELLCOLOR’.
WA_FIELDCAT-REF_TABLE = ‘CALENDAR_TYPE’.
WA_FIELDCAT-REF_FIELD = ‘COLTAB’.
APPEND WA_FIELDCAT TO IT_FIELDCAT.
CLEAR WA_FIELDCAT.
create a table type ‘ZCELLSTYL’ with field ‘CELLSTYLE’ of type LVC_T_STYL
WA_FIELDCAT-FIELDNAME = ‘CELLSTYLE’.
WA_FIELDCAT-REF_TABLE = ‘ZCELLSTYL’.
WA_FIELDCAT-REF_FIELD = ‘CELLSTYLE’.
APPEND WA_FIELDCAT TO IT_FIELDCAT.
*Create a dynamic table with IT_FIELDCAT.
- and Use IT_FIELDCAT_1 to display ALV.
CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE
EXPORTING
IT_FIELDCATALOG = IT_FIELDCAT
IMPORTING
EP_TABLE = DYN_TABLE. ASSIGN DYN_TABLE->* TO . - Create dynamic work area and assign to Field Symbol
CREATE DATA DYN_LINE LIKE LINE OF .
ASSIGN DYN_LINE->* TO .
ENDFORM. “create_itab_dynamically
*&———————————————————————*
*& Form get_data
*&———————————————————————*
* Populate dynamic itab
*———————————————————————-*
FORM GET_DATA.
FIELD-SYMBOLS: <FS_PERNR> TYPE ANY,
<FS_FIELD> TYPE ANY,
<FS_COLOR> TYPE LVC_T_SCOL,
<FS_STYLE> TYPE LVC_T_STYL,
<WA_FIELDCAT> TYPE LVC_S_FCAT.
DATA: IT_CELLCOLOR TYPE LVC_T_SCOL,
IT_CELLSTYLE TYPE LVC_T_STYL,
L_DAY_P TYPE P,
LV_MOD_DATE TYPE DATUM,
C_HOL TYPE CHAR10 VALUE ‘ Holiday ‘.
ASSIGN COMPONENT ‘PERNR’ OF STRUCTURE <WA> TO <FS_PERNR>.
<FS_PERNR> = 1 .
LOOP AT IT_FIELDCAT ASSIGNING <WA_FIELDCAT> .”WHERE
* adde color to dynamci fields..
ASSIGN COMPONENT ‘CELLCOLOR’ OF STRUCTURE <WA> TO <FS_COLOR>.
IF <WA_FIELDCAT>-INTTYPE = ‘T’.
PERFORM MODIFY_CELL_COLOR USING <WA_FIELDCAT>-FIELDNAME
CHANGING IT_CELLCOLOR.
<FS_COLOR> = IT_CELLCOLOR.
ELSE.
IF <WA_FIELDCAT>-FIELDNAME+8(2) = ‘_R’.
LV_MOD_DATE = <WA_FIELDCAT>-FIELDNAME+0(8).
L_DAY_P = LV_MOD_DATE MOD 7.
IF L_DAY_P > 1.
L_DAY_P = L_DAY_P – 1.
ELSE.
L_DAY_P = L_DAY_P + 6.
ENDIF.
IF L_DAY_P = 6 OR L_DAY_P = 7.
PERFORM MODIFY_CELL_COLOR USING <WA_FIELDCAT>-FIELDNAME
CHANGING IT_CELLCOLOR.
<FS_COLOR> = IT_CELLCOLOR.
ASSIGN COMPONENT <WA_FIELDCAT>-FIELDNAME OF STRUCTURE <WA> TO <FS_FIELD>.
<FS_FIELD> = C_HOL.”‘ Holiday ‘.
ENDIF.
ENDIF.
ENDIF.
*Dynamic editable
IF <WA_FIELDCAT>-FIELDNAME+0(8) = SY-DATUM.
ASSIGN COMPONENT ‘CELLSTYLE’ OF STRUCTURE <WA> TO <FS_STYLE>.
PERFORM EDITABLE_CELL USING <WA_FIELDCAT>-FIELDNAME
CHANGING IT_CELLSTYLE.
<FS_STYLE> = IT_CELLSTYLE.
ENDIF.
ENDLOOP.
APPEND <WA> TO <LINE>.
ENDFORM. “get_data
*&———————————————————————*
*& Form modify_cell_color
*&———————————————————————*
* –>P_FIELDNAME text
* –>PT_CELLCOLOR text
*———————————————————————-*
FORM MODIFY_CELL_COLOR USING P_FIELDNAME TYPE LVC_FNAME
CHANGING PT_CELLCOLOR TYPE TABLE.
DATA L_CELLCOLOR TYPE LVC_S_SCOL.
DATA : LV_DATE TYPE DATUM.
DATA: DAY_P TYPE P.
CLEAR L_CELLCOLOR.
IF P_FIELDNAME+8(2) = ‘_R’.
LV_DATE = P_FIELDNAME+8(2).
ELSE.
LV_DATE = P_FIELDNAME.
ENDIF.
DAY_P = LV_DATE MOD 7.
IF DAY_P > 1.
DAY_P = DAY_P – 1.
ELSE.
DAY_P = DAY_P + 6.
ENDIF.
IF DAY_P = 6 OR DAY_P = 7.
IF P_FIELDNAME+8(2) = ‘_R’.
L_CELLCOLOR-COLOR-COL = 7. ” Red.
L_CELLCOLOR-COLOR-INT = 0.
L_CELLCOLOR-COLOR-INV = 0.
ELSE.
L_CELLCOLOR-COLOR-COL = 7. ” Red.
L_CELLCOLOR-COLOR-INT = 1.
L_CELLCOLOR-COLOR-INV = 1.
ENDIF.
L_CELLCOLOR-FNAME = P_FIELDNAME.
APPEND L_CELLCOLOR TO PT_CELLCOLOR.
ELSE.
CLEAR L_CELLCOLOR.
ENDIF.
IF P_FIELDNAME+0(8) = SY-DATUM.
L_CELLCOLOR-FNAME = P_FIELDNAME.
L_CELLCOLOR-COLOR-COL = 3. ” Red.
L_CELLCOLOR-COLOR-INT = 1.
L_CELLCOLOR-COLOR-INV = 1.
APPEND L_CELLCOLOR TO PT_CELLCOLOR.
CONCATENATE P_FIELDNAME ‘_R’ INTO L_CELLCOLOR-FNAME .
L_CELLCOLOR-COLOR-COL = 3. ” Red.
L_CELLCOLOR-COLOR-INT = 1.
L_CELLCOLOR-COLOR-INV = 1.
APPEND L_CELLCOLOR TO PT_CELLCOLOR.
ENDIF.
ENDFORM. ” MODIFY_CELL_COLOR
*&———————————————————————*
*& Form BUILD_FIELDCATALOG
*&———————————————————————*
* Build Fieldcatalog for ALV Report, using SAP table structure
*———————————————————————-*
FORM BUILD_FIELDCATALOG USING P_IT_FIELDCAT TYPE LVC_T_FCAT.”SLIS_T_FIELDCAT_ALV .
** ALV Function module to build field catalog from SAP table structure
CALL FUNCTION ‘LVC_FIELDCATALOG_MERGE’
EXPORTING
* I_BUFFER_ACTIVE =
I_STRUCTURE_NAME = ‘ZDYNAMIC_ALV_STR’
* I_CLIENT_NEVER_DISPLAY = ‘X’
* I_BYPASSING_BUFFER =
* I_INTERNAL_TABNAME =
CHANGING
CT_FIELDCAT = P_IT_FIELDCAT
EXCEPTIONS
INCONSISTENT_INTERFACE = 1
PROGRAM_ERROR = 2
OTHERS = 3
.
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. ” BUILD_FIELDCATALOG
*&———————————————————————*
*& Form DISPLAY_ALV_REPORT
*&———————————————————————*
* Display report using ALV grid
*———————————————————————-*
FORM DISPLAY_ALV_REPORT.
DATA :G_GRID_I TYPE REF TO CL_GUI_ALV_GRID,
G_CUSTOM_CONTAINER_I TYPE REF TO CL_GUI_CUSTOM_CONTAINER,
I_CONTAINER TYPE SCRFNAME VALUE ‘ALV’,
GS_LAYOUT TYPE LVC_S_LAYO.
CREATE OBJECT G_CUSTOM_CONTAINER_I
EXPORTING
CONTAINER_NAME = I_CONTAINER.
CREATE OBJECT G_GRID_I
EXPORTING
I_PARENT = G_CUSTOM_CONTAINER_I.
GS_LAYOUT-COL_OPT = ‘X’.
GS_LAYOUT-CWIDTH_OPT = ‘X’.
GS_LAYOUT-CTAB_FNAME = ‘CELLCOLOR’.
GS_LAYOUT-STYLEFNAME = ‘CELLSTYLE’.
CALL METHOD G_GRID_I->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
IS_LAYOUT = GS_LAYOUT
* IT_TOOLBAR_EXCLUDING = LT_EXCLUDE[]
CHANGING
IT_FIELDCATALOG = IT_FIELDCAT_1
IT_OUTTAB = <LINE>.
*For Editable alv…
CALL METHOD G_GRID_I->SET_READY_FOR_INPUT
EXPORTING
I_READY_FOR_INPUT = 1.
ENDFORM. ” DISPLAY_ALV_REPORT
*&———————————————————————*
*& Module USER_COMMAND_2000 INPUT
*&———————————————————————*
* text
*———————————————————————-*
MODULE USER_COMMAND_2000 INPUT.
CASE OK_CODE.
WHEN ‘BACK’.
LEAVE TO SCREEN 0.
ENDCASE.
ENDMODULE. ” USER_COMMAND_2000 INPUT
*&———————————————————————*
*& Module STATUS_2000 OUTPUT
*&———————————————————————*
* text
*———————————————————————-*
MODULE STATUS_2000 OUTPUT.
SET PF-STATUS ‘STATUS_2000’.
* SET TITLEBAR ‘xxx’.
ENDMODULE. ” STATUS_2000 OUTPUT
*&———————————————————————*
*& Form EDITABLE_CELL
*&———————————————————————** –>P_IT_CELLSTYLE text
*———————————————————————-*
FORM EDITABLE_CELL USING P_FIELDNAME TYPE LVC_FNAME
CHANGING P_IT_CELLSTYLE TYPE LVC_T_STYL.
DATA : WA_CELLSTYLE TYPE LVC_S_STYL.
WA_CELLSTYLE-FIELDNAME = P_FIELDNAME .
WA_CELLSTYLE-STYLE = CL_GUI_ALV_GRID=>MC_STYLE_ENABLED.
INSERT WA_CELLSTYLE INTO TABLE P_IT_CELLSTYLE.
ENDFORM. ” EDITABLE_CELL