SAP ALV 合并单元格
原创
©著作权归作者所有:来自51CTO博客作者wx5ce90caa8666e的原创作品,请联系作者获取转载授权,否则将追究法律责任
继承 CL_GUI_ALV_GRID 类
types: begin of ts_merge,
begin type i,
end type i,
end of ts_merge.
types: ty_t_merge type table of ts_merge.
types: begin of ts_col_merge,
row type i,
col_merge type ty_t_merge,
end of ts_col_merge.
types: ty_t_colmerge type table of ts_col_merge.
types: begin of ts_row_merge,
col type i,
row_merge type ty_t_merge,
end of ts_row_merge.
types: ty_t_rowmerge type table of ts_row_merge.
class lcl_gui_alv_grid_merge definition inheriting from cl_gui_alv_grid.
public section.
methods:
set_table_for_first_display redefinition,
set_merge_horiz importing iv_row type i
changing ct_col_merge type zsoh_t_merge_col,
set_merge_vert importing iv_col type i
changing ct_row_merge type zsoh_t_merge_row,
display.
private section.
data: mt_custom_data type lvc_t_data,
mt_cdata_temp type lvc_t_data.
endclass.
class lcl_gui_alv_grid_merge implementation.
method set_table_for_first_display.
data: lt_fcat type lvc_t_fcat.
data: ls_data type lvc_s_data.
data: lv_row_pos type i.
data: lv_style type lvc_istyle.
data: ls_alv_row type lvc_s_row.
field-symbols: <lft_color> type lvc_t_scol.
"mt_custom_data 用于记录 ooalv 中 CL_GUI_ALV_GRID->MT_DATA 的数据
"因为当数据量太大的时候,CL_GUI_ALV_GRID->MT_DATA 的数据只保留一部分
define def_add_data.
ls_data-col_pos = &1.
ls_data-row_pos = &2.
ls_data-row_id = &2.
ls_data-value = &3.
ls_data-style = &4.
append ls_data to mt_custom_data.
end-of-definition.
super->set_table_for_first_display(
exporting
i_buffer_active = i_buffer_active
i_bypassing_buffer = i_bypassing_buffer
i_consistency_check = i_consistency_check
i_structure_name = i_structure_name
is_variant = is_variant
i_save = i_save
i_default = i_default
is_layout = is_layout
is_print = is_print
it_special_groups = it_special_groups
it_toolbar_excluding = it_toolbar_excluding
it_hyperlink = it_hyperlink
it_alv_graphics = it_alv_graphics
it_except_qinfo = it_except_qinfo
ir_salv_adapter = ir_salv_adapter
changing
it_outtab = it_outtab
it_fieldcatalog = it_fieldcatalog
it_sort = it_sort
it_filter = it_filter
).
lt_fcat = it_fieldcatalog.
delete lt_fcat where no_out = abap_true or fieldname = is_layout-stylefname or fieldname = is_layout-ctab_fname.
sort lt_fcat by col_pos.
loop at it_outtab assigning field-symbol(<lfs_outtab>).
lv_row_pos = lv_row_pos + 1.
ls_alv_row-index = lv_row_pos.
"注意,-1 行是 alv 中單元格所在 row 的信息記錄(lvc_s_row),必須正確,否則 event 裡面取到的數據會有問題
clear lv_style.
def_add_data -1 lv_row_pos ls_alv_row lv_style.
loop at lt_fcat into data(ls_fcat).
assign component ls_fcat-fieldname of structure <lfs_outtab> to field-symbol(<lfg_field>).
if sy-subrc = 0.
def_add_data ls_fcat-col_pos lv_row_pos <lfg_field> lv_style.
endif.
endloop.
endloop.
endmethod
"竖向合并单元格
method set_merge_horiz.
data: lv_outputlen type i.
sort ct_col_merge.
loop at ct_col_merge assigning field-symbol(<lfs_cols>).
if <lfs_cols>-begin le 0.
continue.
endif.
if <lfs_cols>-end le <lfs_cols>-begin.
continue.
endif.
lv_outputlen = <lfs_cols>-end - <lfs_cols>-begin.
loop at mt_custom_data assigning field-symbol(<lfs_data>) where row_pos = iv_row
and ( col_pos between <lfs_cols>-begin and <lfs_cols>-end ).
if <lfs_data>-col_pos = <lfs_cols>-begin.
<lfs_data>-mergehoriz = lv_outputlen.
else.
clear <lfs_data>-mergehoriz.
endif.
endloop.
endloop.
endmethod.
"横向合并
method set_merge_vert.
data: lv_outputlen type i.
sort ct_row_merge.
loop at ct_row_merge assigning field-symbol(<lfs_rows>).
if <lfs_rows>-begin le 0.
continue.
endif.
if <lfs_rows>-end le <lfs_rows>-begin.
continue.
endif.
lv_outputlen = <lfs_rows>-end - <lfs_rows>-begin.
loop at mt_custom_data assigning field-symbol(<lfs_data>) where col_pos = iv_col
and ( row_pos between <lfs_rows>-begin and <lfs_rows>-end ).
if <lfs_data>-row_pos = <lfs_rows>-begin.
<lfs_data>-mergevert = lv_outputlen.
else.
clear <lfs_data>-mergevert.
endif.
endloop.
endloop.
endmethod.
method display.
me->set_data_table(
changing
data_table = mt_custom_data[]
).
me->set_auto_redraw(
exporting
enable = 1
).
endmethod.
endclass.
使用
data(lo_alv) = new lcl_gui_alv_grid_merge( i_parent = cl_gui_container=>screen0 ).
lo_alv->set_table_for_first_display(
exporting
is_layout = ls_layout
changing
it_fieldcatalog = lt_fcat
it_outtab = lt_outtab
exceptions
others = 1
).
"合并第1行,第2~5列,第6~8列
data(ls_colmerge) = value ts_col_merge(
row = 1
col_merge = value #(
( begin = 2 end = 5 )
( begin = 6 end = 8 )
) ).
lo_alv->set_merge_horiz(
exporting
iv_row = ls_colmerge-row
changing
ct_col_merge = ls_colmerge-col_merge
).
"合并第2列,第1~2行,第3~4行
data(ls_rowmerge) = value ts_row_merge(
col = 1
row_merge = value #(
( begin = 1 end = 2 )
( begin = 3 end = 4 )
) ).
lo_alv->set_merge_vert(
exporting
iv_col = ls_rowmerge-col
changing
ct_row_merge = ls_rowmerge-row_merge
).