creating dynamic alv with dynamic editable columns and dynamic colors to the columns based on...

11
7/18/2019 Creating Dynamic ALV With Dynamic Editable Columns and Dynamic Colors to the Columns Based on Condition http://slidepdf.com/reader/full/creating-dynamic-alv-with-dynamic-editable-columns-and-dynamic-colors-to-the 1/11 Creating dynamic ALV with dynamic editable columns and dynamic colors to the columns based on condition By Saikumar Bonakurthi, HCL Technologies 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 CHAR5,  END OF TY_1. DATA : LV_FLD_1 TYPE TY_1. START-OF-SELECTION. * Create !"a#$% ta'e (tr$%t)re  PERFORM !ET_TABLE_STRUCTURE. *Create !"a#$% $"ter"a'e ta'e  PERFORM CREATE_ITAB_DYNAMICALLY. * +$''$", te ata $"to !"a#$% $"ter"a' ta'e  PERFORM !ET_DATA. END-OF-SELECTION. * $(p'a! !"a#$% $"tera' ata  PERFORM DISPLAY_ALV_REPORT.  CALL SCREEN .///.

Upload: assign7997061

Post on 01-Mar-2016

244 views

Category:

Documents


1 download

DESCRIPTION

Creating Dynamic ALV With Dynamic Editable Columns and Dynamic Colors to the Columns Based on Condition

TRANSCRIPT

Page 1: Creating Dynamic ALV With Dynamic Editable Columns and Dynamic Colors to the Columns Based on Condition

7/18/2019 Creating Dynamic ALV With Dynamic Editable Columns and Dynamic Colors to the Columns Based on Condition

http://slidepdf.com/reader/full/creating-dynamic-alv-with-dynamic-editable-columns-and-dynamic-colors-to-the 1/11

Creating dynamic ALV with dynamic editable columns and dynamic colors to thecolumns based on condition

By Saikumar Bonakurthi, HCL Technologies

This Program will help to create dynamic ALV with dynamic editable columns and dynamic colors to thecolumns 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 CHAR5,

  END OF TY_1.

DATA: LV_FLD_1 TYPE TY_1.

START-OF-SELECTION.

* Create !"a#$% ta'e (tr$%t)re

  PERFORM !ET_TABLE_STRUCTURE.

*Create !"a#$% $"ter"a'e ta'e

  PERFORM CREATE_ITAB_DYNAMICALLY.

* +$''$", te ata $"to !"a#$% $"ter"a' ta'e

  PERFORM !ET_DATA.

END-OF-SELECTION.

* $(p'a! !"a#$% $"tera' ata

  PERFORM DISPLAY_ALV_REPORT.

  CALL SCREEN .///.

Page 2: Creating Dynamic ALV With Dynamic Editable Columns and Dynamic Colors to the Columns Based on Condition

7/18/2019 Creating Dynamic ALV With Dynamic Editable Columns and Dynamic Colors to the Columns Based on Condition

http://slidepdf.com/reader/full/creating-dynamic-alv-with-dynamic-editable-columns-and-dynamic-colors-to-the 2/11

Page 3: Creating Dynamic ALV With Dynamic Editable Columns and Dynamic Colors to the Columns Based on Condition

7/18/2019 Creating Dynamic ALV With Dynamic Editable Columns and Dynamic Colors to the Columns Based on Condition

http://slidepdf.com/reader/full/creating-dynamic-alv-with-dynamic-editable-columns-and-dynamic-colors-to-the 3/11

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

*& For# ,et_ta'e_(tr)%t)re

*&---------------------------------------------------------------------** Get (tr)%t)re o+ a" SAP ta'e

*----------------------------------------------------------------------*

FORM !ET_TABLE_STRUCTURE.

  DATA : REF_TABLE_DESCR TYPE REF TO CL_ABAP_STRUCTDESCR,

  IT_TABDESCR TYPE ABAP_COMPDESCR_TAB,

Page 4: Creating Dynamic ALV With Dynamic Editable Columns and Dynamic Colors to the Columns Based on Condition

7/18/2019 Creating Dynamic ALV With Dynamic Editable Columns and Dynamic Colors to the Columns Based on Condition

http://slidepdf.com/reader/full/creating-dynamic-alv-with-dynamic-editable-columns-and-dynamic-colors-to-the 4/11

  WA_TABDESCR TYPE ABAP_COMPDESCR,

  LV_NO_DAYS TYPE P,

  LV_DATE TYPE SY-DATUM,

  LV_DATUM TYPE SY-DATUM,

  LV_DAY TYPE TE"T#,

  LV_WEEK_DAY TYPE TE"T1,

  LIN TYPE SY-TFILL,  C_REM TYPE STRIN! VALUE 0Re#ar1(0 .

* +$'' te ata $"to +$e'%at'o, o+ (tat$% +$e'

  PERFORM BUILD_FIELDCATALO! USING IT_FIELDCAT.

* Ret)r" (tr)%t)re o+ te ta'e  REF_TABLE_DESCR $% CL_ABAP_TYPEDESCR%>DESCRIBE_BY_DATA& LV_FLD_1 '.  IT_TABDESCR() % REF_TABLE_DESCR->COMPONENTS().

* Get "o o+ a!( $" a #o"t

  CALL FUNCTION 0HR_E_NUM_OF_DAYS_OF_MONTH0

  E2PORTING  P_FECHA % SY-DATUM

  IMPORTING

  NUMBER_OF_DAYS % LV_NO_DAYS.

* ,et "o o+ '$"e(

  DESCRIBE TABLE IT_FIELDCAT LINES LIN .

  LV_DATUM % SY-DATUM.

  LV_DATUM*3&.' % 4.  WA_FIELDCAT-COL_POS % LIN.

* +$'' !"a#$% +$e' $"to +$e'( %ate

  DO LV_NO_DAYS TIMES.

  LV_DATE % LV_DATUM * SY-INDE2 - 4.

  CALL FUNCTION 0DATE_TO_DAY0

  E2PORTING

  DATE  % LV_DATE  IMPORTING  WEEKDAY % LV_WEEK_DAY.

  CONCATENATE LV_DATE*3&.' 050 LV_WEEK_DAY INTO LV_DAY.

  LOOP AT IT_TABDESCR INTO WA_TABDESCR.

  WA_FIELDCAT-COL_POS % WA_FIELDCAT-COL_POS * 4  .

  IF SY-TABI" % 4.  WA_FIELDCAT-FIELDNAME % LV_DATE .  WA_FIELDCAT-COLTE"T % LV_DAY.

  ELSE.

  CONCATENATE LV_DATE 0_R0  INTO WA_FIELDCAT-FIELDNAME.

  WA_FIELDCAT-COLTE"T % C_REM.60Re#ar1(0

  ENDIF.  WA_FIELDCAT-INTTYPE % WA_TABDESCR-TYPE_KIND.

  WA_FIELDCAT-INTLEN % WA_TABDESCR-LEN!TH + ..

  APPEND  WA_FIELDCAT TO  IT_FIELDCAT.

  ENDLOOP.

  ENDDO.

ENDFORM. 6,et_ta'e_(tr)%t)re

Page 5: Creating Dynamic ALV With Dynamic Editable Columns and Dynamic Colors to the Columns Based on Condition

7/18/2019 Creating Dynamic ALV With Dynamic Editable Columns and Dynamic Colors to the Columns Based on Condition

http://slidepdf.com/reader/full/creating-dynamic-alv-with-dynamic-editable-columns-and-dynamic-colors-to-the 5/11

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

*& For# %reate_$ta_!"a#$%a''!

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

* Create $"ter"a' ta'e !"a#$%a''!*----------------------------------------------------------------------*

FORM CREATE_ITAB_DYNAMICALLY.* Create !"a#$% $"ter"a' ta'e a" a(($," to F$e'-S!#o'

  CLEAR  WA_FIELDCAT.

*#o7e +$e'( +ro# IT_FIELDCAT $"to IT_FIELDCAT_4  IT_FIELDCAT_1 % IT_FIELDCAT.

* U(e re+ ta'e CALENDAR_TYPE a" re+ +$e' 0COLTAB0

WA_FIELDCAT-FIELDNAME % 0CELLCOLOR0.

  WA_FIELDCAT-REF_TABLE % 0CALENDAR_TYPE0.

  WA_FIELDCAT-REF_FIELD % 0COLTAB0.

  APPEND WA_FIELDCAT TO IT_FIELDCAT.

  CLEAR  WA_FIELDCAT.

* %reate a ta'e t!pe 08CELLSTYL0 9$t +$e' 0CELLSTYLE0 o+ t!pe LVC_T_STYL

  WA_FIELDCAT-FIELDNAME % 0CELLSTYLE0.

  WA_FIELDCAT-REF_TABLE % 08CELLSTYL0.

  WA_FIELDCAT-REF_FIELD % 0CELLSTYLE0.

  APPEND WA_FIELDCAT TO IT_FIELDCAT.

Page 6: Creating Dynamic ALV With Dynamic Editable Columns and Dynamic Colors to the Columns Based on Condition

7/18/2019 Creating Dynamic ALV With Dynamic Editable Columns and Dynamic Colors to the Columns Based on Condition

http://slidepdf.com/reader/full/creating-dynamic-alv-with-dynamic-editable-columns-and-dynamic-colors-to-the 6/11

*Create a !"a#$% ta'e 9$t IT_FIELDCAT* a" U(e IT_FIELDCAT_4 to $(p'a! ALV

  CALL METHOD CL_ALV_TABLE_CREATE%>CREATE_DYNAMIC_TABLE

  E2PORTING  IT_FIELDCATALO! % IT_FIELDCAT

  IMPORTING  EP_TABLE % DYN_TABLE.

  ASSIGN DYN_TABLE-> TO <LINE>.

* Create !"a#$% 9or1 area a" a(($," to F$e' S!#o'

  CREATE DATA DYN_LINE LI:E LINE OF <LINE> .

  ASSIGN DYN_LINE-> TO <WA> .

ENDFORM. 6%reate_$ta_!"a#$%a''!

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

*& For# ,et_ata

*&---------------------------------------------------------------------** Pop)'ate !"a#$% $ta

*----------------------------------------------------------------------*

FORM !ET_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 CHAR1 VALUE 0 Ho'$a! 0.

  ASSIGN COMPONENT 0PERNR0 OF STRUCTURE <WA> TO <FS_PERNR>.

  <FS_PERNR> % 4 .

  LOOP  AT IT_FIELDCAT ASSIGNING <WA_FIELDCAT> .6;HERE

* ae %o'or to !"a#%$ +$e'(

  ASSIGN COMPONENT 0CELLCOLOR0 OF STRUCTURE <WA> TO <FS_COLOR>.

  IF <WA_FIELDCAT>-INTTYPE % 0T0.

Page 7: Creating Dynamic ALV With Dynamic Editable Columns and Dynamic Colors to the Columns Based on Condition

7/18/2019 Creating Dynamic ALV With Dynamic Editable Columns and Dynamic Colors to the Columns Based on Condition

http://slidepdf.com/reader/full/creating-dynamic-alv-with-dynamic-editable-columns-and-dynamic-colors-to-the 7/11

  PERFORM  MODIFY_CELL_COLOR USING <WA_FIELDCAT>-FIELDNAME

  CHANGING IT_CELLCOLOR.  <FS_COLOR> % IT_CELLCOLOR.

  ELSE.

  IF <WA_FIELDCAT>-FIELDNAME*<&.' % 0_R0.

  LV_MOD_DATE % <WA_FIELDCAT>-FIELDNAME*/&<'.  L_DAY_P % LV_MOD_DATE MOD =.

  IF L_DAY_P > 4.

  L_DAY_P % L_DAY_P - 4.

  ELSE.

  L_DAY_P % L_DAY_P * 3.

  ENDIF.

  IF L_DAY_P % 3 OR  L_DAY_P % =.

  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.60 Ho'$a! 0

  ENDIF.

  ENDIF.

  ENDIF.

*D!"a#$% e$ta'e

  IF <WA_FIELDCAT>-FIELDNAME*/&<' % SY-DATUM.

  ASSIGN COMPONENT 0CELLSTYLE0 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. 6,et_ata

*&---------------------------------------------------------------------**& For# #o$+!_%e''_%o'or 

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

* -->P_FIELDNAME te?t

* -->PT_CELLCOLOR te?t*----------------------------------------------------------------------*

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*<&.' % 0_R0.

  LV_DATE % P_FIELDNAME*<&.'.

  ELSE.  LV_DATE % P_FIELDNAME.

Page 8: Creating Dynamic ALV With Dynamic Editable Columns and Dynamic Colors to the Columns Based on Condition

7/18/2019 Creating Dynamic ALV With Dynamic Editable Columns and Dynamic Colors to the Columns Based on Condition

http://slidepdf.com/reader/full/creating-dynamic-alv-with-dynamic-editable-columns-and-dynamic-colors-to-the 8/11

  ENDIF.

  DAY_P % LV_DATE MOD =.

  IF DAY_P > 4.

  DAY_P % DAY_P - 4.

  ELSE.  DAY_P % DAY_P * 3.

  ENDIF.

  IF DAY_P % 3 OR  DAY_P % =.

  IF P_FIELDNAME*<&.' % 0_R0.

  L_CELLCOLOR-COLOR -COL % =. 6 Re

  L_CELLCOLOR-COLOR -INT % /.

  L_CELLCOLOR-COLOR -INV % /.

  ELSE.

  L_CELLCOLOR-COLOR -COL % =. 6 Re

  L_CELLCOLOR-COLOR -INT % 4.

  L_CELLCOLOR-COLOR -INV % 4.

  ENDIF.  L_CELLCOLOR-FNAME % P_FIELDNAME.

  APPEND L_CELLCOLOR TO PT_CELLCOLOR.

  ELSE.

  CLEAR  L_CELLCOLOR.

  ENDIF.

  IF P_FIELDNAME*/&<' % SY-DATUM.

  L_CELLCOLOR-FNAME % P_FIELDNAME.

  L_CELLCOLOR-COLOR -COL % @. 6 Re

  L_CELLCOLOR-COLOR -INT % 4.

  L_CELLCOLOR-COLOR -INV % 4.

  APPEND L_CELLCOLOR TO PT_CELLCOLOR.

  CONCATENATE P_FIELDNAME 0_R0 INTO  L_CELLCOLOR-FNAME .

  L_CELLCOLOR-COLOR -COL % @. 6 Re

  L_CELLCOLOR-COLOR -INT % 4.

  L_CELLCOLOR-COLOR -INV % 4.

  APPEND L_CELLCOLOR TO PT_CELLCOLOR.

  ENDIF.

ENDFORM. 6 MODIFY_CELL_COLOR 

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

*& For# BUILD_FIELDCATALOG*&---------------------------------------------------------------------** B)$' F$e'%ata'o, +or ALV Report5 )($", SAP ta'e (tr)%t)re

*----------------------------------------------------------------------*

FORM BUILD_FIELDCATALO! USING P_IT_FIELDCAT TYPE LVC_T_FCAT.6SLIS_T_FIELDCAT_ 

ALV

** ALV F)"%t$o" #o)'e to )$' +$e' %ata'o, +ro# SAP ta'e (tr)%t)re

  CALL FUNCTION 0LVC_FIELDCATALOG_MERGE0

Page 9: Creating Dynamic ALV With Dynamic Editable Columns and Dynamic Colors to the Columns Based on Condition

7/18/2019 Creating Dynamic ALV With Dynamic Editable Columns and Dynamic Colors to the Columns Based on Condition

http://slidepdf.com/reader/full/creating-dynamic-alv-with-dynamic-editable-columns-and-dynamic-colors-to-the 9/11

  E2PORTING

* I_BUFFER_ACTIVE

  I_STRUCTURE_NAME % 08DYNAMIC_ALV_STR0

* I_CLIENT_NEVER_DISPLAY 020

* I_BYPASSING_BUFFER

* I_INTERNAL_TABNAME

  CHANGING  CT_FIELDCAT % P_IT_FIELDCAT

 E2CEPTIONS

  INCONSISTENT_INTERFACE % 4

  PRO!RAM_ERROR % .

  OTHERS  % @  .

  IF SY-SUBRC <> /.

  MESSAGE ID SY-MS!ID TYPE SY-MS!TY  NUMBER  SY-MS!NO

  ;ITH SY-MS!V1 SY-MS!V SY-MS!V SY-MS!V#.

  ENDIF.

ENDFORM. 6 BUILD_FIELDCATALOG

*&---------------------------------------------------------------------**& For# DISPLAY_ALV_REPORT

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

* D$(p'a! report )($", ALV ,r$

*----------------------------------------------------------------------*FORM DISPLAY_ALV_REPORT.

  DATA :!_!RID_I TYPE REF TO CL_!UI_ALV_!RID,

  !_CUSTOM_CONTAINER_I TYPE REF TO CL_!UI_CUSTOM_CONTAINER,

  I_CONTAINER TYPE SCRFNAME VALUE 0ALV0,

  !S_LAYOUT TYPE LVC_S_LAYO.

  CREATE OB/ECT !_CUSTOM_CONTAINER_I

  E2PORTING  CONTAINER_NAME % I_CONTAINER.

  CREATE OB/ECT !_!RID_I

  E2PORTING  I_PARENT % !_CUSTOM_CONTAINER_I.

  !S_LAYOUT-COL_OPT % 020.

  !S_LAYOUT-CWIDTH_OPT % 020.

  !S_LAYOUT-CTAB_FNAME % 0CELLCOLOR0.

  !S_LAYOUT-STYLEFNAME % 0CELLSTYLE0.

  CALL METHOD !_!RID_I->SET_TABLE_FOR_FIRST_DISPLAY

  E2PORTING  IS_LAYOUT % !S_LAYOUT

* IT_TOOLBAR_E2CLUDING LT_E2CLUDE

  CHANGING  IT_FIELDCATALO! % IT_FIELDCAT_1  IT_OUTTAB % <LINE>.

*For E$ta'e a'7

  CALL METHOD !_!RID_I->SET_READY_FOR_INPUT

  E2PORTING

Page 10: Creating Dynamic ALV With Dynamic Editable Columns and Dynamic Colors to the Columns Based on Condition

7/18/2019 Creating Dynamic ALV With Dynamic Editable Columns and Dynamic Colors to the Columns Based on Condition

http://slidepdf.com/reader/full/creating-dynamic-alv-with-dynamic-editable-columns-and-dynamic-colors-to-the 10/11

  I_READY_FOR_INPUT % 4.

ENDFORM. 6 DISPLAY_ALV_REPORT

*&---------------------------------------------------------------------**& Mo)'e USER_COMMAND_./// INPUT

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

* te?t*----------------------------------------------------------------------*MODULE USER_COMMAND_ INPUT.

  CASE OK_CODE.

  ;HEN 0BAC:0.

  LEAVE TO SCREEN /.

  ENDCASE.

ENDMODULE. 6 USER_COMMAND_./// INPUT

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

*& Mo)'e STATUS_./// OUTPUT

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

* te?t*----------------------------------------------------------------------*

MODULE STATUS_ OUTPUT.  SET PF-STATUS 0STATUS_.///0.

* SET TITLEBAR 0???0

ENDMODULE. 6 STATUS_./// OUTPUT

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

*& For# EDITABLE_CELL

*&---------------------------------------------------------------------** -->P_IT_CELLSTYLE te?t*----------------------------------------------------------------------*

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_!UI_ALV_!RID%>MC_STYLE_ENABLED.

  INSERT WA_CELLSTYLE INTO TABLE P_IT_CELLSTYLE.

ENDFORM. 6 EDITABLE_CELL

O)tp)t

The output shows current month calendar with all the Saturday and Sundays dynamically red color willapply and !or the current date yellow color with "ditable !ields

Page 11: Creating Dynamic ALV With Dynamic Editable Columns and Dynamic Colors to the Columns Based on Condition

7/18/2019 Creating Dynamic ALV With Dynamic Editable Columns and Dynamic Colors to the Columns Based on Condition

http://slidepdf.com/reader/full/creating-dynamic-alv-with-dynamic-editable-columns-and-dynamic-colors-to-the 11/11