货铺QQ群号:834508274
下面开始干货部分……
ALV标准功能汇总,分类汇总,排序,过滤这些功能除了可以直接使用它的标准功能按钮之外,你也可以在程序里设定,让ALV列表第一次显示出来就已经使用了这些功能。
用REUSE_ALV_GRID_DISPLAY实现的ALV不能在程序设定输出字段的平均值,最小值,最大值等标准功能。用OO方式实现ALV输出的就可以,
<!--[if !supportLists]-->一、<!--[endif]-->排序
排序用到类型slis_t_sortinfo_alv,按条件把过滤的字段和规则设置好,添加到一个参考slis_t_sortinfo_alv类型的内表,在调用Function的时候指导内表赋给it_sort参数
<!--[if !supportLists]-->二、<!--[endif]-->过滤
过滤用到类型slis_t_filter_alv,按条件把排序的字段和规则设置好,添加到一个参考slis_t_filter_alv类型的内表,在调用Function的时候把内表的值赋给it_fitler参数
<!--[if !supportLists]-->三、<!--[endif]-->分类汇总
分类汇总是汇总和排序两个功能来进行分类和汇总的。按照排序的字段的值进行分类,对已经汇总的字段进行分类汇总。在设置排序的时候设置类型slis_t_sortinfo_alv-subtot = ‘X’ 就可以了。
<!--[if !supportLists]-->四、<!--[endif]-->汇总
汇总是设置要汇总的字段的Fieldcat属性,设置slis_t_fieldcat_alv-do_sum = ‘X’。
下面是源代码
TYPE-POOLS slis.
TYPES ty_tab_spfli TYPE spfli OCCURS 0.
START-OF-SELECTION.
PERFORM f_display.
*&---------------------------------------------------------------------*
*& Form f_getdata
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->PT_SPFLI text
*----------------------------------------------------------------------*
FORM f_getdata TABLES pt_spfli TYPE ty_tab_spfli.
SELECT * FROM spfli INTO TABLE pt_spfli.
ENDFORM. "f_getdata
*&---------------------------------------------------------------------*
*& Form f_display
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM f_display.
DATA: lt_spfli TYPE ty_tab_spfli,
lt_filter TYPE slis_t_filter_alv,
lt_sort TYPE slis_t_sortinfo_alv,
lt_fieldcat TYPE slis_t_fieldcat_alv.
PERFORM f_getdata TABLES lt_spfli.
PERFORM f_filter_build TABLES lt_filter.
PERFORM f_sort_build TABLES lt_sort.
PERFORM f_fieldcat CHANGING lt_fieldcat.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = sy-repid
it_fieldcat = lt_fieldcat
it_filter = lt_filter
it_sort = lt_sort
TABLES
t_outtab = lt_spfli.
ENDFORM. "f_display
*&---------------------------------------------------------------------*
*& Form f_fieldcat
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->PT_FIELDCAT text
*----------------------------------------------------------------------*
FORM f_fieldcat CHANGING pt_fieldcat TYPE slis_t_fieldcat_alv.
FIELD-SYMBOLS <fs_fieldcat> LIKE LINE OF pt_fieldcat.
CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
EXPORTING
i_structure_name = 'SPFLI'
CHANGING
ct_fieldcat = pt_fieldcat
EXCEPTIONS
inconsistent_interface = 1
program_error = 2
other = 3.
LOOP AT pt_fieldcat ASSIGNING <fs_fieldcat> WHERE fieldname = 'DISTANCE'.
<fs_fieldcat>-do_sum = 'X'.
ENDLOOP.
ENDFORM. "f_fieldcat
*&---------------------------------------------------------------------*
*& Form f_filter_build
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->PT_FILTER text
*----------------------------------------------------------------------*
FORM f_filter_build TABLES pt_filter TYPE slis_t_filter_alv.
DATA lwa_filter LIKE LINE OF pt_filter.
lwa_filter-fieldname = 'DISTANCE'.
lwa_filter-sign0 = 'E'.
lwa_filter-optio = 'EQ'.
lwa_filter-valut = 'MI '.
APPEND lwa_filter TO pt_filter.
ENDFORM. "f_filter_build
*&---------------------------------------------------------------------*
*& Form f_sort_build
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->PT_SORT text
*----------------------------------------------------------------------*
FORM f_sort_build TABLES pt_sort TYPE slis_t_sortinfo_alv.
DATA lwa_sort LIKE LINE OF pt_sort.
lwa_sort-fieldname = 'CARRID'.
lwa_sort-up = 'X'.
lwa_sort-subtot = 'X'.
APPEND lwa_sort TO pt_sort.
ENDFORM. "f_sort_build