关闭

OO实现ALV(四)<转>

403人阅读 评论(0) 收藏 举报
分类:

一、ALV的页眉页脚

除了可以通过类CL_SALV_EVENTS_TABLE的事件设置ALV的页眉页脚之外,还可以通过类CL_SALV_TABLE的方法SET_TOP_OF_LIST,SET_TOP_OF_LIST_PRINT,SET_END_OF_LIST,SET_END_OF_LIST_PRINT这四个方法。页眉页脚的显示可以设置显示和打印时不一样,打印时的页眉页脚通过方法SET_TOP_OF_LIST_PRINT和SET_END_OF_LIST_PRINT。

1.声明设置页眉页脚所需要的Layout的实例和变量。

DATA: lr_grid TYPE REF TO cl_salv_form_layout_grid,

lr_grid2 TYPE REF TO cl_salv_form_layout_grid,

lr_text TYPE REF TO cl_salv_form_text,

lr_action TYPE REF TO cl_salv_form_action_info,

lr_header TYPE REF TO cl_salv_form_header_info,

lr_label TYPE REF TO cl_salv_form_label.

2.添加页眉页脚所需要的文本到Layout里。

lr_header = lr_grid->create_header_information(

row = 1

column = 1

text = 'Header Information'

).

lr_label = lr_grid->create_label(

row = 2

column = 1

text = 'Label'

).

lr_action = lr_grid->create_action_information(

row = 3

column = 1

text = 'Action Information'

).

lr_text = lr_grid->create_text(

text = 'text'

row = 4

column = 1

).

gr_table->set_top_of_list( lr_grid ).

lr_text = lr_grid2->create_text(

text = 'end of page'

row = 1

column = 1

).

3.使用方法SET_TOP_OF_LIST设置页眉

gr_table->set_top_of_list( lr_grid ).

下面是程序的所有代码:

REPORT y_xin_002.

*变量定义==============================================================

TYPES: BEGIN OF gs_spfli,

mandt TYPE spfli-mandt,

carrid TYPE spfli-carrid,

connid TYPE spfli-connid,

countryfr TYPE spfli-countryfr,

cityfrom TYPE spfli-cityfrom,

airpfrom TYPE spfli-airpfrom,

countryto TYPE spfli-countryto,

cityto TYPE spfli-cityto,

airpto TYPE spfli-airpto,

fltime TYPE spfli-fltime,

deptime TYPE spfli-deptime,

arrtime TYPE spfli-arrtime,

distance TYPE spfli-distance,

distid TYPE spfli-distid,

fltype TYPE spfli-fltype,

period TYPE spfli-period,

END OF gs_spfli.

TYPES ty_spfli TYPE gs_spfli OCCURS 0.

*----------------------------------------------------------------------*

* CLASS lcl_alv DEFINITION

*----------------------------------------------------------------------*

* ALV操作类(定义)

*----------------------------------------------------------------------*

CLASS lcl_alv DEFINITION.

PUBLIC SECTION.

METHODS: getdata "取得要显示的数据

RETURNING value(lt_tab) TYPE ty_spfli,

alv_full "全屏Grid列表处理方法

IMPORTING value(lt_tab) TYPE ty_spfli.

PRIVATE SECTION.

DATA: gr_table TYPE REF TO cl_salv_table.

ENDCLASS. "lcl_alv DEFINITION

*----------------------------------------------------------------------*

* CLASS lcl_alv IMPLEMENTATION

*----------------------------------------------------------------------*

* ALV操作类(实现)

*----------------------------------------------------------------------*

CLASS lcl_alv IMPLEMENTATION.

*取得要显示的数据======================================================

METHOD getdata.

SELECT * INTO CORRESPONDING FIELDS OF TABLE lt_tab FROM spfli.

ENDMETHOD. "getdata

*输出全屏网格列表的方法================================================

METHOD alv_full.

DATA: lr_functions TYPE REF TO cl_salv_functions_list,

lr_grid TYPE REF TO cl_salv_form_layout_grid,

lr_grid2 TYPE REF TO cl_salv_form_layout_grid,

lr_text TYPE REF TO cl_salv_form_text,

lr_action TYPE REF TO cl_salv_form_action_info,

lr_header TYPE REF TO cl_salv_form_header_info,

lr_label TYPE REF TO cl_salv_form_label.

"创建实例

TRY.

cl_salv_table=>factory(

IMPORTING

r_salv_table = gr_table

CHANGING

t_table = lt_tab

).

CATCH cx_salv_msg.

ENDTRY.

"ALV标准功能

lr_functions = gr_table->get_functions( ).

lr_functions->set_all( 'X' ).

"页眉页脚

CREATE OBJECT: lr_grid,lr_grid2.

lr_header = lr_grid->create_header_information(

row = 1

column = 1

text = 'Header Information'

).

lr_label = lr_grid->create_label(

row = 2

column = 1

text = 'Label'

).

lr_action = lr_grid->create_action_information(

row = 3

column = 1

text = 'Action Information'

).

lr_text = lr_grid->create_text(

text = 'text'

row = 4

column = 1

).

gr_table->set_top_of_list( lr_grid ).

lr_text = lr_grid2->create_text(

text = 'end of page'

row = 1

column = 1

).

gr_table->set_end_of_list( lr_grid2 ).

"显示列表

gr_table->display( ).

ENDMETHOD. "alv_full

ENDCLASS. "lcl_alv IMPLEMENTATION

*&---------------------------------------------------------------------*

*& Form f_main

*&---------------------------------------------------------------------*

* 整合数据,执行

*----------------------------------------------------------------------*

FORM f_main.

DATA: lt_tab TYPE ty_spfli,

lr_alv TYPE REF TO lcl_alv.

CREATE OBJECT lr_alv.

"取得要显示的数据

lt_tab = lr_alv->getdata( ).

lr_alv->alv_full( lt_tab ).

ENDFORM. "f_main

*执行动作==============================================================

START-OF-SELECTION.

PERFORM f_main.

二、ALV的聚合,排序,过滤

在ALV提供的标准工具栏上我们可以实现输出结果的聚合(最大值,最小值,汇总,平均数),排序,过滤功能,同样在程序里也可以达到这些功能。

1.声明聚合,排序,过滤所需要的变量。

DATA: lr_aggregations TYPE REF TO cl_salv_aggregations,

lr_filters TYPE REF TO cl_salv_filters,

lr_sorts type ref to cl_salv_sorts.

2.汇总

lr_aggregations->add_aggregation(

columnname = 'DISTANCE'

aggregation = if_salv_c_aggregation=>total

).

3.最小值

lr_aggregations->add_aggregation(

columnname = 'FLTIME'

aggregation = if_salv_c_aggregation=>minimum

).

4.过滤

lr_filters->add_filter(

columnname = 'CITYFROM'

sign = 'I'

option = 'NE'

low = 'ROM'

high = ''

).

5.排序

lr_sorts->add_sort(

columnname = 'CITYFROM'

sequence = if_salv_c_sort=>sort_down

subtotal = 'X'

obligatory = 'X').

下面是程序的所有代码:

REPORT y_xin_002.

*变量定义==============================================================

TYPES: BEGIN OF gs_spfli,

mandt TYPE spfli-mandt,

carrid TYPE spfli-carrid,

connid TYPE spfli-connid,

countryfr TYPE spfli-countryfr,

cityfrom TYPE spfli-cityfrom,

airpfrom TYPE spfli-airpfrom,

countryto TYPE spfli-countryto,

cityto TYPE spfli-cityto,

airpto TYPE spfli-airpto,

fltime TYPE spfli-fltime,

deptime TYPE spfli-deptime,

arrtime TYPE spfli-arrtime,

distance TYPE spfli-distance,

distid TYPE spfli-distid,

fltype TYPE spfli-fltype,

period TYPE spfli-period,

END OF gs_spfli.

TYPES ty_spfli TYPE gs_spfli OCCURS 0.

*----------------------------------------------------------------------*

* CLASS lcl_alv DEFINITION

*----------------------------------------------------------------------*

* ALV操作类(定义)

*----------------------------------------------------------------------*

CLASS lcl_alv DEFINITION.

PUBLIC SECTION.

METHODS: getdata "取得要显示的数据

RETURNING value(lt_tab) TYPE ty_spfli,

alv_full "全屏Grid列表处理方法

IMPORTING value(lt_tab) TYPE ty_spfli.

PRIVATE SECTION.

DATA: gr_table TYPE REF TO cl_salv_table.

ENDCLASS. "lcl_alv DEFINITION

*----------------------------------------------------------------------*

* CLASS lcl_alv IMPLEMENTATION

*----------------------------------------------------------------------*

* ALV操作类(实现)

*----------------------------------------------------------------------*

CLASS lcl_alv IMPLEMENTATION.

*取得要显示的数据======================================================

METHOD getdata.

SELECT * INTO CORRESPONDING FIELDS OF TABLE lt_tab FROM spfli.

ENDMETHOD. "getdata

*输出全屏网格列表的方法================================================

METHOD alv_full.

DATA: lr_functions TYPE REF TO cl_salv_functions_list,

lr_aggregations TYPE REF TO cl_salv_aggregations,

lr_filters TYPE REF TO cl_salv_filters,

lr_sorts type ref to cl_salv_sorts.

"创建实例

TRY.

cl_salv_table=>factory(

IMPORTING

r_salv_table = gr_table

CHANGING

t_table = lt_tab

).

CATCH cx_salv_msg.

ENDTRY.

"ALV标准功能

lr_functions = gr_table->get_functions( ).

lr_functions->set_all( 'X' ).

lr_aggregations = gr_table->get_aggregations( ).

lr_filters = gr_table->get_filters( ).

lr_sorts = gr_table->get_sorts( ).

TRY.

"汇总

lr_aggregations->add_aggregation(

columnname = 'DISTANCE'

aggregation = if_salv_c_aggregation=>total

).

"最小值

lr_aggregations->add_aggregation(

columnname = 'FLTIME'

aggregation = if_salv_c_aggregation=>minimum

).

"过滤

lr_filters->add_filter(

columnname = 'CITYFROM'

sign = 'I'

option = 'NE'

low = 'ROM'

high = ''

).

"排序

lr_sorts->add_sort(

columnname = 'CITYFROM'

sequence = if_salv_c_sort=>sort_down

subtotal = 'X'

obligatory = 'X'

).

CATCH cx_salv_data_error.

CATCH cx_salv_existing.

CATCH cx_salv_not_found.

ENDTRY.

"显示列表

gr_table->display( ).

ENDMETHOD. "alv_full

ENDCLASS. "lcl_alv IMPLEMENTATION

*&---------------------------------------------------------------------*

*& Form f_main

*&---------------------------------------------------------------------*

* 整合数据,执行

*----------------------------------------------------------------------*

FORM f_main.

DATA: lt_tab TYPE ty_spfli,

lr_alv TYPE REF TO lcl_alv.

CREATE OBJECT lr_alv.

"取得要显示的数据

lt_tab = lr_alv->getdata( ).

lr_alv->alv_full( lt_tab ).

ENDFORM. "f_main

*执行动作===============================================================

START-OF-SELECTION.

PERFORM f_main.

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:13191次
    • 积分:266
    • 等级:
    • 排名:千里之外
    • 原创:12篇
    • 转载:6篇
    • 译文:0篇
    • 评论:1条
    文章分类
    最新评论