Page 1 of 1

Colouring ALV Columns via Layout

PostPosted: Thu Mar 27, 2014 8:21 am
by PJA
Hi all.

So the question was asked by a user: why can't I colour specific columns in an ALV?

So I started looking around on the interweb and couldn't find anything, hence the program below.

Things to bear in mind about this program:
- I'm not a developer - but a lowly PM contractor, so please be forgiving with my code
- The program has never been tested properly
- You must have a layout created for the transaction/program before this code will work
- It doesn't appear to work for all ALVs - don't know why
- It doesn't always work if there are groups - don't know why
- Presently it only works for user layouts (but this could be easily changed)
- It doesn't have the field description/text

Does anyone want to take up the challenge and improve it, or sort out my poor attempt at coding...

PeteA

Code: Select all
*&--------------------------------------------------------------------&*
*& Report ZPJA0016                                                    &*
*&--------------------------------------------------------------------&*
*& This program gets an existing Layout from the database and allows  &*
*& the user to update the ALV column colours.                        &*
*& The data is then written back to the database.                     &*
*&--------------------------------------------------------------------&*
report  zpja0016.

tables: tstc.

*&--------------------------------------------------------------------&*
*& TYPES                                                              &*
*&--------------------------------------------------------------------&*
type-pools: slis.

types: begin of ty_field_data.
types: field(30) type c.
types: colour(4) type c.
types: end of ty_field_data.

*&--------------------------------------------------------------------&*
*& DATA                                                               &*
*&--------------------------------------------------------------------&*
data g_lines        type i.
data g_answer       type c.
data g_flag         type c.
data g_text1(40)    type c.
data g_text2(40)    type c.
data g_textt(40)    type c.
data g_varkey       type ltdxkey.
data g_t_fieldcat   type table of ltdxdata.
data g_fieldcat     type ltdxdata.
data g_t_field_data type table of ty_field_data with header line.
data g_t_ltdx       type table of ltdx with header line.
data g_t_spopli     type table of spopli with header line.
data g_t_fieldcat2  type slis_t_fieldcat_alv.
data g_fieldcat2    type slis_fieldcat_alv.
data g_layout       type slis_layout_alv.
data g_variant      type disvariant.
data g_user         type slis_exit_by_user.
*data g_t_celltab    type lvc_t_styl.

*&--------------------------------------------------------------------&*
*& CONSTANTS                                                          &*
*&--------------------------------------------------------------------&*
constants c_lt(2)   type c value 'LT'.
constants c_a       type c value 'A'.
constants c_f       type c value 'F'.
constants c_x       type c value 'X'.

*&--------------------------------------------------------------------&*
*& SELECTION-SCREEN                                                   &*
*&--------------------------------------------------------------------&*
selection-screen: begin of block b1 with frame title text-b01.
parameter: p_alv radiobutton group radi default 'X',
           p_col radiobutton group radi.
selection-screen: end of block b1.

selection-screen: begin of block b2 with frame title text-b02.
parameters: p_tcode  like syst-tcode obligatory memory id tcd.
parameters: p_pgmna  like tstc-pgmna.
parameters: p_userid like syst-uname obligatory default sy-uname.
selection-screen: end of block b2.

*&--------------------------------------------------------------------&*
*& START-OF-SELECTION                                                 &*
*&--------------------------------------------------------------------&*
start-of-selection.

  if p_alv = c_x.

* Get program name from transaction
    clear tstc.
    select single * from tstc where tcode = p_tcode.

    if sy-subrc <> 0.
      message s000(g01) with 'Transaction/program data invalid'.
      exit.
    endif.

* If transaction fails, then use program name if assigned in selection screen
    if tstc-pgmna is initial.
      tstc-pgmna = p_pgmna.
    endif.

* Otherwise issue an error message if TCode AND Program are invalid
    if tstc-pgmna is initial.
      message s000(g01) with 'Transaction/program data invalid'.
      exit.
    endif.

* Get user layouts from database
    select * from ltdx appending table g_t_ltdx
                      where report    = tstc-pgmna
                        and username  = p_userid.

* Check rows were selected
    g_lines = lines( g_t_ltdx ).

    if g_lines <> 0.

* Format table data for popup
      loop at g_t_ltdx.
        g_t_spopli-varoption = g_t_ltdx-variant.
        append g_t_spopli.
      endloop.

      sort g_t_spopli.
      delete adjacent duplicates from  g_t_spopli.

* Ask user to select layout for update
      g_textt = 'User Variants'.
      concatenate 'User Variants for transaction ' p_tcode '.' into g_text1 separated by space.
      g_text2 = 'Choose a layout to change:'.

      call function 'POPUP_TO_DECIDE_LIST'
        exporting
          start_col = 0
          start_row = 5
          textline1 = g_text1
          textline2 = g_text2
          titel     = g_textt
        importing
          answer    = g_answer
        tables
          t_spopli  = g_t_spopli.

* Process popup answer
      if g_answer = c_a.
        message s157(g01).
        exit.
      else.
        read table g_t_spopli index g_answer.

        read table g_t_ltdx with key variant = g_t_spopli-varoption.

        clear: g_varkey, g_variant, g_t_fieldcat.
        refresh g_t_fieldcat.

        g_varkey-report    = tstc-pgmna.
        g_varkey-handle    = g_t_ltdx-handle.
        g_varkey-log_group = g_t_ltdx-log_group.
        g_varkey-username  = p_userid.
        g_varkey-variant   = g_t_spopli-varoption.
        g_varkey-type      = c_f.

        g_variant-report    = tstc-pgmna.
        g_variant-handle    = g_t_ltdx-handle.
        g_variant-log_group = g_t_ltdx-log_group.
        g_variant-username  = p_userid.
        g_variant-variant   = g_t_spopli-varoption.
        g_variant-text      = c_f.

* Get layout details from database
        call function 'LT_DBDATA_READ_FROM_LTDX'
          exporting
            i_tool       = c_lt
            is_varkey    = g_varkey
          tables
            t_dbfieldcat = g_t_fieldcat
          exceptions
            not_found    = 1
            wrong_relid  = 2
            others       = 3.

* If found, create new layout and change the column colour
        if sy-subrc = 0.

          g_lines = lines( g_t_fieldcat ).
          check g_lines <> 0.

* Build field data table
          loop at g_t_fieldcat into g_fieldcat.
            g_t_field_data-field = g_fieldcat-key1.
            append g_t_field_data.
          endloop.

          sort g_t_field_data.
          delete adjacent duplicates from g_t_field_data.

          loop at g_t_field_data.
            loop at g_t_fieldcat into g_fieldcat
                   where key1 = g_t_field_data-field
                     and param = 'EMPHASIZE'.
              g_t_field_data-colour = g_fieldcat-value.
              modify g_t_field_data.
            endloop.
          endloop.

* Format ALV Field Catalog
          clear: g_t_fieldcat2, g_fieldcat2.
          g_fieldcat2-fieldname = 'FIELD'.
          g_fieldcat2-col_pos   = 1.
          g_fieldcat2-seltext_l = 'Field Name'.
          g_fieldcat2-outputlen = 30.
          append g_fieldcat2 to g_t_fieldcat2.

          clear: g_fieldcat2.
          g_fieldcat2-fieldname = 'COLOUR'.
          g_fieldcat2-col_pos   = 2.
          g_fieldcat2-seltext_l = 'Colour'.
          g_fieldcat2-outputlen = 4.
          g_fieldcat2-edit      = c_x.
          append g_fieldcat2 to g_t_fieldcat2.

          g_layout-colwidth_optimize = c_x.

          call function 'REUSE_ALV_GRID_DISPLAY'
            exporting
              it_fieldcat            = g_t_fieldcat2
              is_layout              = g_layout
              i_save                 = c_x
            importing
              es_exit_caused_by_user = g_user
            tables
              t_outtab               = g_t_field_data.

          if g_user(1) = c_x.

            loop at g_t_field_data.
              check g_t_field_data-field ne 'PM_SELECTED'.  " Required for PM list-edit reports
* Check number format
              if not g_t_field_data-colour is initial.
                if g_t_field_data-colour(1) = 'C' and
                 g_t_field_data-colour+1(1) co '0123456789' and
                 g_t_field_data-colour+2(1) co '0123456789' and
                 g_t_field_data-colour+3(1) co '0123456789'.

                  clear: g_fieldcat, g_flag.
                  loop at g_t_fieldcat into g_fieldcat
                         where key1 = g_t_field_data-field
                           and param = 'EMPHASIZE'.
                    g_flag = c_x.
                    g_fieldcat-value = g_t_field_data-colour.
                    modify g_t_fieldcat from g_fieldcat.
                  endloop.

                  if g_flag is initial.
* No EMPHASIZE entered
                    clear g_fieldcat.
                    g_fieldcat-key1  = g_t_field_data-field.
                    g_fieldcat-key2  = 'MASTER'.
                    g_fieldcat-param = 'EMPHASIZE'.
                    g_fieldcat-value = g_t_field_data-colour.
                    append g_fieldcat to g_t_fieldcat.
                  endif.
                endif.

* Check for case where COLOUR has been deleted
              else.
                clear: g_fieldcat, g_flag.
                loop at g_t_fieldcat into g_fieldcat
                       where key1 = g_t_field_data-field
                         and param = 'EMPHASIZE'.
                  delete g_t_fieldcat.  " Delete line
                endloop.
              endif.
            endloop.

* Write layout back to database
            g_varkey-variant    = g_t_spopli-varoption.

            call function 'LT_DBDATA_WRITE_TO_LTDX'
              exporting
                i_tool       = c_lt
                is_varkey    = g_varkey
                is_variant   = g_variant
              tables
                t_dbfieldcat = g_t_fieldcat.

* Refresh ALV buffer and issue message
            submit balvbufdel and return.
            message s539(cy) with g_t_spopli-varoption.   "Layout key "&" was saved

          else.

* Refresh ALV buffer and issue message
            submit balvbufdel and return.
            message s157(g01).   "Processing cancelled

          endif.
        endif.
      endif.

    else.
      message s501(ao).   "No records were found for your selection parameters
    endif.

  else.

*&--------------------------------------------------------------------&*
* Show ALV colour options
*&--------------------------------------------------------------------&*

    types: begin of g_coltab_wa,
      colour(4)  type c,
      text(255) type c,
      end of g_coltab_wa.

    data: g_t_coltab type table of g_coltab_wa.
    data: g_c1   type c.
    data: g_coltab type g_coltab_wa.
    data: g_fcat type slis_t_fieldcat_alv,
          g_layo type slis_layout_alv,
          g_vari type disvariant.
    data: gs_fcat type slis_t_fieldcat_alv with header line. "#EC NEEDED

* Build output table
    g_c1 = '0'.
    do 7 times.
      g_c1 = g_c1 + 1.
      concatenate 'C' g_c1 '00' into g_coltab-colour.
      concatenate 'This is colour' g_coltab-colour into g_coltab-text separated by space.
      append g_coltab to g_t_coltab.
      concatenate 'C' g_c1 '10' into g_coltab-colour.
      concatenate 'This is colour' g_coltab-colour into g_coltab-text separated by space.
      append g_coltab to g_t_coltab.
      concatenate 'C' g_c1 '01' into g_coltab-colour.
      concatenate 'This is colour' g_coltab-colour into g_coltab-text separated by space.
      append g_coltab to g_t_coltab.
    enddo.

* Variant settings
    g_vari-report   = sy-repid.
    g_vari-username = sy-uname.
    g_vari-handle   = 'MYGR'.

* Layout settings
    g_layo-colwidth_optimize = ''.
    g_layo-info_fieldname    = 'COLOUR'.

* Fieldcat settings
    gs_fcat-fieldname = 'TEXT'.
    gs_fcat-seltext_l = 'Text'.
    gs_fcat-seltext_m = 'Text'.
    gs_fcat-seltext_s = 'Text'.
    append gs_fcat to g_fcat.

* Display colours
    call function 'REUSE_ALV_GRID_DISPLAY'
      exporting
        is_layout     = g_layo
        it_fieldcat   = g_fcat
        is_variant    = g_vari
      tables
        t_outtab      = g_t_coltab
      exceptions
        program_error = 1
        others        = 2.

    if sy-subrc <> 0.

    endif.

  endif.

*&--------------------------------------------------------------------&*
*& Selection-screen texts                                             &*
*&--------------------------------------------------------------------&*
* P_ALV   Update layout data
* P_COL   Show colours
* P_TCODE   Transaction code
* P_PGMNA Program name
* P_USERID  User Name

* B01 Processing Options  18  18
* B02 ALV Data  8 8

Re: Colouring ALV Columns via Layout

PostPosted: Thu Mar 27, 2014 8:48 am
by Gothmog
Well, you CAN do it in ALV. At least OO ALV - dump this REUSE... stuff, and move to the future!
(Future happiness not garanteed, this advertisement is not contractual)
An example can be found in program SALV_DEMO_TABLE_COLUMNS.
Three simple method calls (to get columns, to get one column, and to set the color), and your column is colored.

Re: Colouring ALV Columns via Layout

PostPosted: Thu Mar 27, 2014 8:52 am
by PJA
Thanks Gothmog,

I did post my disclaimer regarding my ABAP skills... old dog - new tricks... not gonna happen..

But wouldn't I need to code this for every ALV program (standard and customer-specific)?

And how would we code this for user-specific settings?

With this stand-alone approach - I can colour most ALV reports "remotely" - and to my own requirements.

PeteA

Re: Colouring ALV Columns via Layout

PostPosted: Thu Apr 03, 2014 9:06 am
by Rich
Themed ALV..... 8)

Put a wrapper around the ALV grid.

Theme information (grid type, background color, logo's etc etc in a database table)

Inside the wrapper automatically provide hotspots for specified data elements

etc etc etc

And then always use the themed alv function module call.

Initially it's a lot of work, but done once and you have a standard look and feel to your reports

Re: Colouring ALV Columns via Layout

PostPosted: Tue Apr 08, 2014 2:13 pm
by skarkada
Pete, what you developed is cool! I did not know that something like this is possible. I was able to change the colors of SE16 display!