3

To illustrate what I want:

|         Category A            |         Category B            |  C  |
|     A.A       |      A.B      |      A.A      |      A.B      |     |
| A.A.A | A.A.B | A.B.A | A.B.B | B.A.A | B.A.B | B.B.A | B.B.B |     |

I need a header that looks somewhat like this and I was wondering if there's a way to do it other than manually WRITE each line. :D

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Mike
  • 61
  • 2
  • 3

3 Answers3

1

The internal table mt_data[] in cl_gui_alv_grid holds information about how the frontend shows the grid. Each cell is represented in a separate line with these fields: enter image description here

It turns out you can set mergehoriz (and mergevert) to an integer that defines how many adjesent cells will merge into the cell. In order to change that table I created a sub-class to cl_gui_alv_grid and implemented the method redraw where I do just that.

Here's a demo report I created for this. You need a dynpro "100" with a custom container "CC" on it and a status "STATUS_0100" with an exit-command to run it.

report zdemo_grid_merge.

call screen 100.

class cl_gui_alv_grid_merge definition inheriting from cl_gui_alv_grid.
  public section.
    methods redraw.
endclass.

class cl_gui_alv_grid_merge implementation.
  method redraw.
    field-symbols: <ms_data> type lvc_s_data.
    loop at mt_data[] assigning <ms_data>.
      case <ms_data>-row_pos.
        when 1.
*          The first row merges the first column with the next eight columns
*          so it stretches for a total of nine columns
*          It also merges the first row with the second row
          if <ms_data>-col_pos = 1.
            <ms_data>-mergehoriz = 8.
            <ms_data>-mergevert = 1.
          endif.

*          Column ten in the first row is merged with the next five lines.
          if <ms_data>-col_pos = 10.
            <ms_data>-mergevert = 5.
          endif.
        when 3.
*          Since line one and two are merged, row_pos 3 is the second line
*          In the second line column one and five are extended by three columns
*          to build two four column wide headers
          if <ms_data>-col_pos = 1 or
             <ms_data>-col_pos = 5.
            <ms_data>-mergehoriz = 3.
          endif.
        when 4.
*          Line three builds two column wide cells
          if <ms_data>-col_pos = 1 or
             <ms_data>-col_pos = 3 or
             <ms_data>-col_pos = 5 or
             <ms_data>-col_pos = 7.
            <ms_data>-mergehoriz = 1.
          endif.
        when others.
*          Everything else stays normal.
      endcase.


    endloop.

    call method set_data_table
      changing
        data_table = mt_data[].

    call method set_auto_redraw
      exporting
        enable = 1.
  endmethod.
endclass.

data: go_cc   type ref to cl_gui_custom_container.
data: go_grid type ref to cl_gui_alv_grid_merge.
types: begin of t_tab,
         field01 type text255,
         field02 type text255,
         field03 type text255,
         field04 type text255,
         field05 type text255,
         field06 type text255,
         field07 type text255,
         field08 type text255,
         field09 type text255,
         field10 type text255,
       end of t_tab.
data: gt_tab type table of t_tab.


module status_0100 output.
  perform init_grid.
  set pf-status 'STATUS_0100'.
endmodule.

module user_command_0100_exit input.
  set screen 0.
  leave screen.
endmodule.

form init_grid.

  data: lt_fcat type lvc_t_fcat.
  data: ls_vari type disvariant.
  data: ls_layo type lvc_s_layo.


  if go_cc is not initial.
    return.
  endif.

  create object go_cc
    exporting
      container_name              = 'CC'
    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 go_grid
    exporting
      i_parent          = go_cc
    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.

  perform get_fcat changing lt_fcat.
  ls_vari-report = sy-repid.
  ls_vari-username = sy-uname.
  ls_vari-handle = '0001'.
  ls_layo-sel_mode = 'B'.
  ls_layo-no_headers = 'X'.
  ls_layo-zebra = 'X'.
  ls_layo-no_toolbar = 'X'.

  perform create_table.

  call method go_grid->set_table_for_first_display
    exporting
      is_variant                    = ls_vari
      i_save                        = 'A'
      is_layout                     = ls_layo
    changing
      it_outtab                     = gt_tab
      it_fieldcatalog               = lt_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.

  call method go_grid->redraw.
endform.

form get_fcat changing ct_fcat type lvc_t_fcat.
  data: ls_fcat type lvc_s_fcat.
  data: lv_fname type lvc_fname.
  data: lv_n(2) type n.

  lv_fname = 'FIELD'.
  do 10 times.
    lv_n = sy-index.
    ls_fcat-fieldname = lv_fname && lv_n.
    ls_fcat-just = 'C'.
    ls_fcat-outputlen = 20.
    append ls_fcat to ct_fcat.
  enddo.

endform.

form create_table.
  data: ls_tab type t_tab.

  clear ls_tab.
  ls_tab-field01 = '------------- Double Line Header Stretching Over Four Cells -------------'.
  ls_tab-field10 = 'Vertical Merge'.
  append ls_tab to gt_tab.
  append ls_tab to gt_tab.

  clear ls_tab.
  ls_tab-field01 = '--- Category A ---'.
  ls_tab-field05 = '--- Category B ---'.
  ls_tab-field09 = '--- Category C ---'.
  append ls_tab to gt_tab.

  clear ls_tab.
  ls_tab-field01 = 'A.A'.
  ls_tab-field03 = 'A.B'.
  ls_tab-field05 = 'B.A'.
  ls_tab-field07 = 'B.B'.
  ls_tab-field09 = 'C.1'.
  append ls_tab to gt_tab.

  clear ls_tab.
  ls_tab-field01 = 'A.A.A.1'.
  ls_tab-field02 = 'A.A.B.1'.
  ls_tab-field03 = 'A.B.A.1'.
  ls_tab-field04 = 'A.B.B.1'.
  ls_tab-field05 = 'B.A.A.1'.
  ls_tab-field06 = 'B.A.B.1'.
  ls_tab-field07 = 'B.B.A.1'.
  ls_tab-field08 = 'B.B.B.1'.
  ls_tab-field09 = 'C.2'.
  append ls_tab to gt_tab.

  clear ls_tab.
  ls_tab-field01 = 'A.A.A.2'.
  ls_tab-field02 = 'A.A.B.2'.
  ls_tab-field03 = 'A.B.A.2'.
  ls_tab-field04 = 'A.B.B.2'.
  ls_tab-field05 = 'B.A.A.2'.
  ls_tab-field06 = 'B.A.B.2'.
  ls_tab-field07 = 'B.B.A.2'.
  ls_tab-field08 = 'B.B.B.2'.
  ls_tab-field09 = 'C.3'.
  append ls_tab to gt_tab.

  clear ls_tab.
  append ls_tab to gt_tab.
  append ls_tab to gt_tab.
endform.

For completion the dynpro 100:

process before output.
  module status_0100.

process after input.
  module user_command_0100_exit at exit-command.

This is the resulting grid: enter image description here

I call the redraw method to change mt_data[] and save it to the actual data table of the grid with set_data_table. Note that calling refresh_table_display will get rid of these changes and you would have to follow this up with calling redraw afterwards every time. Also, the toolbar functions will break the merge so you'd have to implement them by hand too.

This is obviously very far from perfect, but I think it's a decent foundation to work up from.

Herr Berthm
  • 128
  • 5
0

Based on my experience, you can't do "multi-layer" header in ALV Grid. But this can be done with simple list using WRITE statements which gives you more control on how to display the data.

But of course you have to do all the functions like Sort, Filter and so on by yourself.

The best way is to stick with ALV Grid and just Sub-categorize your data with columns just as Harmut suggested. You can sort them by this sub-category column so it will look like grouped together in the ALV Grid

user1817181
  • 1,193
  • 1
  • 8
  • 6
0

Depending on the scenario, you might be able to bend the XXL API to generate the list in Excel. Try executing the demo program XXLFTEST and have it export the results to a pivot table - maybe you could use this? Other than that, I know of no control that is able to do this. You could WRITE the stuff yourself, of course, or you could try to persuade the users to have the hierarchy on the vertical axis and use a standard column tree...

vwegert
  • 18,371
  • 3
  • 37
  • 55