1. 首先我们要取得类CL_SALV_SELECTIONS的实例。
DATA lr_selectionsTYPE REF TO cl_salv_selections.
lr_selections = gr_table->get_selections( ).
2. 设置选择模式。
lr_selections->set_selection_mode(if_salv_c_selection_mode=>row_column ).
3. 选择行
DATA lt_row TYPE salv_t_row.
APPEND 3 TO lt_row.
APPEND 4 TO lt_row.
lr_selections->set_selected_rows( lt_row ).
4. 选择列
DATA lt_column TYPEsalv_t_column.
APPEND 3 TO lt_row.
APPEND 4 TO lt_row.
lr_selections->set_selected_rows( lt_row ).
5. 选择一个单元格
DATA ls_cell TYPEsalv_s_cell.
ls_cell-row = 7.
ls_cell-columnname = 'CITYFROM'.
lr_selections->set_current_cell( ls_cell )
6. 选择多个单元格
DATA lt_cell TYPE salv_t_cell,
ls_cell TYPE salv_s_cell.
ls_cell-row = 5.
ls_cell-columnname = 'CITYFROM'.
APPEND ls_cell TO lt_cell.
ls_cell-row = 6.
ls_cell-columnname = 'CITYFROM'.
APPEND ls_cellTO lt_cell.
lr_selections->set_selected_cells( lt_cell ).
下面是效果图
下面是程序的所有代码:
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.
DATA: gr_table TYPE REF TO cl_salv_table.
*----------------------------------------------------------------------*
* CLASS lcl_alv DEFINITION
*----------------------------------------------------------------------*
* ALV操作类(定义)
*----------------------------------------------------------------------*
CLASS lcl_alv DEFINITION.
PUBLIC SECTION.
METHODS: getdata "取得要显示的数据
RETURNING value(lt_tab) TYPEty_spfli,
alv_full "全屏Grid列表处理方法
IMPORTING value(lt_tab) TYPE ty_spfli,
main. "主方法
ENDCLASS. "lcl_alv DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_alv IMPLEMENTATION
*----------------------------------------------------------------------*
* ALV操作类(实现)
*----------------------------------------------------------------------*
CLASS lcl_alv IMPLEMENTATION.
*取得要显示的数据
METHOD getdata.
SELECT * INTO CORRESPONDING FIELDS OF TABLElt_tab FROM spfli.
ENDMETHOD. "getdata
*输出全屏网格列表的方法
METHOD alv_full.
DATA: lr_functions TYPE REF TO cl_salv_functions,
lr_selections TYPE REF TOcl_salv_selections,
lt_column TYPE salv_t_column,
lt_row TYPE salv_t_row,
lt_cell TYPE salv_t_cell,
ls_cell TYPE salv_s_cell.
"创建实例
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' ).
"取得类CL_SALV_SELECTIONS的实例
lr_selections =gr_table->get_selections( ).
"设置选择模式
lr_selections->set_selection_mode(if_salv_c_selection_mode=>row_column ).
"选择列
APPEND 'FLTIME' TO lt_column.
APPEND 'DISTANCE' TO lt_column.
lr_selections->set_selected_columns(lt_column ).
"选择行
APPEND 3 TO lt_row.
APPEND 4 TO lt_row.
lr_selections->set_selected_rows( lt_row).
* "选择多个单元格
* ls_cell-row = 5.
* ls_cell-columnname = 'CITYFROM'.
* APPEND ls_cell TO lt_cell.
* ls_cell-row = 6.
* ls_cell-columnname = 'CITYFROM'.
* APPEND ls_cell TO lt_cell.
* lr_selections->set_selected_cells(lt_cell ).
"选择一个单元格
ls_cell-row = 7.
ls_cell-columnname = 'CITYFROM'.
lr_selections->set_current_cell( ls_cell).
"显示列表
gr_table->display( ).
ENDMETHOD. "alv_full
*主方法,整合数据
METHOD main.
DATA: lt_tab TYPE ty_spfli.
"取得要显示的数据
lt_tab = me->getdata( ).
me->alv_full( lt_tab ).
ENDMETHOD. "main
ENDCLASS. "lcl_alvIMPLEMENTATION
*执行动作
START-OF-SELECTION.
DATA lr_alv TYPE REF TO lcl_alv.
CREATE OBJECT lr_alv.
lr_alv->main( ).