SAP R/3 форум ABAP консультантов
Russian ABAP Developer's Club

Home - FAQ - Search - Memberlist - Usergroups - Profile - Log in to check your private messages - Register - Log in - English
Blogs - Weblogs News

Color individual cells of an ALV GRID



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> ALV Grid / ALV Tree / ALV List
View previous topic :: View next topic  
Author Message
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1639

PostPosted: Fri Jun 04, 2010 11:24 am    Post subject: Color individual cells of an ALV GRID Reply with quote

Author: Ankur Parab

The coloring of individual cells in ALV Grid is possible through ALV using OOPS.
First declare a table (of type lvc_t_scol) for specifying the color inside the internal table which contains the values to be displayed.
c_col type lvc_t_scol, " For cell coloring
Now after populating the internal table, modify the field c_col by specifying the color and the field name.
There can be any number of such rows in the internal table.
Code:

form set_color .
 data: wa_col type lvc_s_scol.
 wa_col-fname = 'SORTL'.
 wa_col-color-col = '7'.
 read table it_temp into wa_temp index 5.
 append wa_col to wa_temp-c_col.
 modify it_temp from wa_temp index 5.
endform." set_color

Now set the ctab_fname attribute of the layout can be set to the name of the color table, which is C_COL.
Code:

gs_layout-ctab_fname = 'C_COL'.

Now call the method set_table_for_first_display to display the ALV.

This code snippet will help you understand how to color individual cells in an ALV GRID object.

Code:
types: begin of y_t_itab,
          matnr    type matnr,
          maktx    type maktx,
          colortab type lvc_t_scol,
       end of y_t_itab.


data : itab type standard table of y_t_itab.
data : color_tab type standard table of lvc_s_scol.
data : it_fcat type standard table of lvc_s_fcat.
data : wa_itab type y_t_itab.

field-symbols: <f_wa_itab> type y_t_itab.
data : wa_fcat  type lvc_s_fcat,
       wa_layo  type lvc_s_layo,
       wa_color type lvc_s_scol.

data : gc_container type ref to cl_gui_custom_container,
       gc_grid      type ref to cl_gui_alv_grid.

select-options : s_matnr for wa_itab-matnr.

select matnr maktx from makt
into corresponding fields of table itab
where matnr in s_matnr
  and spras =  sy-langu.

if sy-subrc eq 0.
  sort itab by matnr.
endif.

call screen '0100'.


module status_0100 output.
*  SET PF-STATUS 'xxxxxxxx'.
*  SET TITLEBAR 'xxx'.


* create the grid object
  create object gc_container
    exporting
      container_name              = 'CC_CONTAINER'
    exceptions
      cntl_error                  = 1
      cntl_system_error           = 2
      create_error                = 3
      lifetime_error              = 4
      lifetime_dynpro_dynpro_link = 5
      others                      = 6.

  if sy-subrc <> 0.
    message id sy-msgid type sy-msgty number sy-msgno
               with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  endif.

  create object gc_grid
    exporting
      i_parent          = gc_container
    exceptions
      error_cntl_create = 1
      error_cntl_init   = 2
      error_cntl_link   = 3
      error_dp_create   = 4
      others            = 5.

  if sy-subrc <> 0.
    message id sy-msgid type sy-msgty number sy-msgno
               with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  endif.

* create fcat
  wa_fcat-fieldname = 'MATNR'.
  wa_fcat-ref_field = 'MATNR'.
  wa_fcat-ref_table = 'MAKT'.
  append wa_fcat to it_fcat.

  wa_fcat-fieldname = 'MAKTX'.
  wa_fcat-ref_field = 'MAKTX'.
  wa_fcat-ref_table = 'MAKT'.
  append wa_fcat to it_fcat.

*add colors to the cells
* LOOPing just to add different colors to different records
  loop at itab assigning  <f_wa_itab>.
    refresh : color_tab[].

    case sy-tabix.
      when 1.
        wa_color-fname = 'MATNR'.
        wa_color-color-col = 6.  " red color
        append wa_color to color_tab.

        wa_color-fname = 'MAKTX'.
        wa_color-color-col = 5. " green color
        append wa_color to color_tab.

      when others.

        wa_color-fname = 'MATNR'.
        wa_color-color-col = 3. "yellow
        append wa_color to color_tab.

        wa_color-fname = 'MAKTX'.
        wa_color-color-col = 7. "violet
        append wa_color to color_tab.

    endcase.
    insert lines of color_tab into table <f_wa_itab>-colortab.
  endloop.

* pass the column name of the internal table
* which contains the color sequence to the layout of the grid
  wa_layo-ctab_fname = 'COLORTAB'.

* display the grid

  call method gc_grid->set_table_for_first_display
    exporting
      is_layout                     = wa_layo
    changing
      it_outtab                     = itab[]
      it_fieldcatalog               = it_fcat[]
    exceptions
      invalid_parameter_combination = 1
      program_error                 = 2
      too_many_lines                = 3
      others                        = 4.

  if sy-subrc <> 0.
    message id sy-msgid type sy-msgty number sy-msgno
               with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  endif.

endmodule.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> ALV Grid / ALV Tree / ALV List All times are GMT + 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG.
SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG.
Every effort is made to ensure content integrity. Use information on this site at your own risk.