abap alv动态显示列!
在abap中,alv显示的时候,我想根据需要动态的显示列,应该如何去实现,比如我想看每个事业部公司的情况,但是不同的事业部的公司个数不一样,这样在列的显示就会不同,麻烦大...
在abap 中, alv 显示的时候,我想根据需要动态的显示 列, 应该如何去实现,比如我想看每个事业部公司的情况,但是不同 的事业部 的 公司个数不一样,这样在列的显示就会不同,麻烦大神可以 给个例子吗,本人学习阶段,非常感谢!!!
展开
展开全部
前几天刚好给初学的同事做了个很简单的例子,你也看看吧,直接复制到SE38里面就可以执行的,在屏幕条件里输入个数字,就会根据你输入的数字,动态的显示列数。
*-------------ALV 相关--------------------------
DATA: wa_layo TYPE lvc_s_layo.
DATA: wa_fcat TYPE lvc_s_fcat,
it_fcat TYPE lvc_t_fcat.
DATA it_scol TYPE lvc_t_scol .
DATA: g_grid TYPE REF TO cl_gui_alv_grid.
FIELD-SYMBOLS: <dyn_table> TYPE STANDARD TABLE,
<dyn_wa>,
<dyn_field>.
DATA: dy_table TYPE REF TO data,
dy_line TYPE REF TO data.
DATA: colname(10),
index(2).
PARAMETERS: col TYPE i."列数
CLEAR: it_fcat.
DO col TIMES." 根据条件动态生成列
index = index + 1.
CONCATENATE 'COL' index INTO colname.
PERFORM frm_add_fcat USING:
colname 'C' '描述' '100'.
ENDDO.
" 根据it_fact生成动态表
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = it_fcat
IMPORTING
ep_table = dy_table.
ASSIGN dy_table->* TO <dyn_table>.
CREATE DATA dy_line LIKE LINE OF <dyn_table>.
ASSIGN dy_line->* TO <dyn_wa>.
" 给表字段赋值
CLEAR: index.
DO col TIMES.
index = index + 1.
CONCATENATE 'COL' index INTO colname.
ASSIGN COMPONENT colname OF STRUCTURE <dyn_wa> TO <dyn_field>.
<dyn_field> = '列数据'.
ENDDO.
APPEND <dyn_wa> TO <dyn_table>.
" 相当于给内表放入两条数据
CLEAR: index.
DO col TIMES.
index = index + 1.
CONCATENATE 'COL' index INTO colname.
ASSIGN COMPONENT colname OF STRUCTURE <dyn_wa> TO <dyn_field>.
<dyn_field> = '列数据'.
ENDDO.
APPEND <dyn_wa> TO <dyn_table>.
CLEAR: wa_layo.
wa_layo-zebra = 'X'.
wa_layo-cwidth_opt = 'X'.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
EXPORTING
i_callback_program = sy-repid
is_layout_lvc = wa_layo
it_fieldcat_lvc = it_fcat
TABLES
t_outtab = <dyn_table>
EXCEPTIONS
program_error = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
*&---------------------------------------------------------------------*
*& Form frm_add_fcat
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->VALUE1 text
* -->VALUE2 text
* -->VALUE3 text
* -->VALUE4 text
*----------------------------------------------------------------------*
FORM frm_add_fcat USING value1 value2 value3 value4.
wa_fcat-fieldname = value1.
wa_fcat-inttype = value2.
wa_fcat-reptext = value3.
wa_fcat-intlen = value4.
APPEND wa_fcat TO it_fcat.
CLEAR: wa_fcat.
ENDFORM. "frm_add_fcat
*-------------ALV 相关--------------------------
DATA: wa_layo TYPE lvc_s_layo.
DATA: wa_fcat TYPE lvc_s_fcat,
it_fcat TYPE lvc_t_fcat.
DATA it_scol TYPE lvc_t_scol .
DATA: g_grid TYPE REF TO cl_gui_alv_grid.
FIELD-SYMBOLS: <dyn_table> TYPE STANDARD TABLE,
<dyn_wa>,
<dyn_field>.
DATA: dy_table TYPE REF TO data,
dy_line TYPE REF TO data.
DATA: colname(10),
index(2).
PARAMETERS: col TYPE i."列数
CLEAR: it_fcat.
DO col TIMES." 根据条件动态生成列
index = index + 1.
CONCATENATE 'COL' index INTO colname.
PERFORM frm_add_fcat USING:
colname 'C' '描述' '100'.
ENDDO.
" 根据it_fact生成动态表
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = it_fcat
IMPORTING
ep_table = dy_table.
ASSIGN dy_table->* TO <dyn_table>.
CREATE DATA dy_line LIKE LINE OF <dyn_table>.
ASSIGN dy_line->* TO <dyn_wa>.
" 给表字段赋值
CLEAR: index.
DO col TIMES.
index = index + 1.
CONCATENATE 'COL' index INTO colname.
ASSIGN COMPONENT colname OF STRUCTURE <dyn_wa> TO <dyn_field>.
<dyn_field> = '列数据'.
ENDDO.
APPEND <dyn_wa> TO <dyn_table>.
" 相当于给内表放入两条数据
CLEAR: index.
DO col TIMES.
index = index + 1.
CONCATENATE 'COL' index INTO colname.
ASSIGN COMPONENT colname OF STRUCTURE <dyn_wa> TO <dyn_field>.
<dyn_field> = '列数据'.
ENDDO.
APPEND <dyn_wa> TO <dyn_table>.
CLEAR: wa_layo.
wa_layo-zebra = 'X'.
wa_layo-cwidth_opt = 'X'.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
EXPORTING
i_callback_program = sy-repid
is_layout_lvc = wa_layo
it_fieldcat_lvc = it_fcat
TABLES
t_outtab = <dyn_table>
EXCEPTIONS
program_error = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
*&---------------------------------------------------------------------*
*& Form frm_add_fcat
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->VALUE1 text
* -->VALUE2 text
* -->VALUE3 text
* -->VALUE4 text
*----------------------------------------------------------------------*
FORM frm_add_fcat USING value1 value2 value3 value4.
wa_fcat-fieldname = value1.
wa_fcat-inttype = value2.
wa_fcat-reptext = value3.
wa_fcat-intlen = value4.
APPEND wa_fcat TO it_fcat.
CLEAR: wa_fcat.
ENDFORM. "frm_add_fcat
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
ZESTRON
2024-09-04 广告
2024-09-04 广告
在Dr. O.K. Wack Chemie GmbH,我们高度重视ZESTRON的表界面分析技术。该技术通过深入研究材料表面与界面的性质,为提升产品质量与可靠性提供了有力支持。ZESTRON的表界面分析不仅涵盖了相变化、化学反应、吸附与解吸...
点击进入详情页
本回答由ZESTRON提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询