ALV 或任何其他网格布局中的两级列标题?

发布于 2024-09-26 21:51:38 字数 307 浏览 8 评论 0原文

为了说明我想要什么:

|         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 |     |

我需要一个看起来有点像这样的标题,我想知道除了手动写入每一行之外是否还有其他方法可以做到这一点。 :D

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

回忆追雨的时光 2024-10-03 21:51:38

cl_gui_alv_grid 中的内部表 mt_data[] 保存有关前端如何显示网格的信息。每个单元格都用以下字段表示在单独的行中:
输入图片这里的描述

事实证明,您可以将mergehoriz(和mergevert)设置为一个整数,该整数定义了将有多少相邻单元格合并到单元格中。为了更改该表,我创建了一个 cl_gui_alv_grid 子类,并实现了 redraw 方法,我就是这样做的。

这是我为此创建的演示报告。您需要一个带有自定义容器“CC”的 dynpro“100”以及带有退出命令的状态“STATUS_0100”来运行它。

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.

为了完成 dynpro 100:

process before output.
  module status_0100.

process after input.
  module user_command_0100_exit at exit-command.

这是生成的网格:
输入图片这里的描述

我调用redraw方法来更改mt_data[]并使用set_data_table将其保存到网格的实际数据表中代码>.请注意,调用 refresh_table_display 将消除这些更改,并且您必须在每次之后调用 redraw 来跟进。
此外,工具栏功能会破坏合并,因此您也必须手动实现它们。

这显然离完美还很远,但我认为这是一个不错的基础。

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.

可可 2024-10-03 21:51:38

根据具体情况,您也许可以使用 XXL API 在 Excel 中生成列表。尝试执行演示程序 XXLFTEST 并将其结果导出到数据透视表 - 也许您可以使用这个?除此之外,据我所知没有任何控件能够做到这一点。当然,您可以自己编写这些内容,或者您​​可以尝试说服用户在垂直轴上具有层次结构并使用标准的列树......

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...

我一向站在原地 2024-10-03 21:51:38

根据我的经验,你不能在 ALV Grid 中做“多层”标题。但这可以通过使用 WRITE 语句的简单列表来完成,这使您可以更好地控制如何显示数据。

当然,排序、过滤等所有功能都必须自己完成。

最好的方法是坚持使用 ALV 网格,并按照 Harmut 的建议对数据进行子分类。您可以按此子类别列对它们进行排序,这样它看起来就像在 ALV 网格中分组在一起

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文