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

问题描述 投票:0回答:3

为了说明我想要什么:

| A 类 | B 类 |丙 |
|一个.A | A.B |一个.A | A.B | |
| A.A.A | A.A.B | A.B.A | A.B.B | B.A.A | B.A.B |学士 | B.B.B | |

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

header grid abap alv
3个回答
1
投票

mt_data[]
中的内部表
cl_gui_alv_grid
包含有关前端如何显示网格的信息。每个单元格都在单独的行中表示,其中包含以下字段:

事实证明,您可以将

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
来跟进。 此外,工具栏功能会破坏合并,因此您也必须手动实现它们。

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


0
投票

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


0
投票

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

但是当然你必须自己完成所有功能,如排序,过滤等。

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

© www.soinside.com 2019 - 2024. All rights reserved.