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

...Previous

* 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 <LINE>.
* Create dynamic work area and assign to Field Symbol
  
CREATE DATA DYN_LINE LIKE LINE OF <LINE> .
  
ASSIGN DYN_LINE->* TO <WA> .

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

Output:

The output shows current month calendar with all the Saturday and Sundays dynamically red color will apply and for the current date yellow color with Editable fields.

 

Please send us your feedback/suggestions at webmaster@SAPTechnical.COM 

HomeContribute About Us Privacy Terms Of Use • Disclaimer • SafeCompanies: Advertise on SAPTechnical.COM | Post JobContact Us  

Graphic Design by Round the Bend Wizards

footer image footer image