ABAP 查看股票 SALV版本

Posted techtalk

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ABAP 查看股票 SALV版本相关的知识,希望对你有一定的参考价值。

ABAP 查看股票 SALV版本:

 

执行界面:

 

 

 

代码:

  1 REPORT zrep_ooalv_stock.
  2 
  3 *********************************************************************
  4 *使用方法:
  5 *输入股票号码,获得新浪财经API实时沪深两市股票数据,双击可显示个股K线
  6 *代码移植:
  7 *在SAP SE38中创建报表ZREP_OOALV_STOCK,粘贴代码
  8 *需要双击屏幕号码 Screen 100并激活
  9 *双击状态 SET PF-STATUS \'ALV1\'的 \'ALV1\',设置\'BACK\'按钮,激活
 10 *声明:
 11 *程序仅供参考,不能作为投资依据. Aaron Hao - 2017/11/11
 12 *********************************************************************
 13 
 14 
 15 "定义股票信息结构体
 16 TYPES: BEGIN OF ty_stock_live,
 17          sid    TYPE char10,  sname  TYPE char10,
 18          topen  TYPE char10,  yclose TYPE char10,
 19          cprice TYPE char10,  thigh  TYPE char10,
 20          tlow   TYPE char10,  buy    TYPE char10,
 21          sale   TYPE char10,  number TYPE char20,
 22          amount TYPE char20,  buy11  TYPE char10,
 23          buy12  TYPE char10,  buy21  TYPE char10,
 24          buy22  TYPE char10,  buy31  TYPE char10,
 25          buy32  TYPE char10,  buy41  TYPE char10,
 26          buy42  TYPE char10,  buy51  TYPE char10,
 27          buy52  TYPE char10,  sale11 TYPE char10,
 28          sale12 TYPE char10,  sale21 TYPE char10,
 29          sale22 TYPE char10,  sale31 TYPE char10,
 30          sale32 TYPE char10,  sale41 TYPE char10,
 31          sale42 TYPE char10,  sale51 TYPE char10,
 32          sale52 TYPE char10,  date   TYPE char15,
 33          time   TYPE char15,  color  TYPE lvc_t_scol, "Color.
 34        END OF ty_stock_live.
 35 
 36 TYPES: BEGIN OF ty_stock_id,
 37          stock_id TYPE char6,
 38        END OF ty_stock_id.
 39 
 40 TYPES ty_tab_stock_id TYPE STANDARD TABLE OF ty_stock_id.
 41 
 42 TYPES ty_tab_stock_live TYPE STANDARD TABLE OF ty_stock_live.
 43 
 44 DATA: gv_stock_id(6) TYPE c.
 45 DATA: tmp_num(6) TYPE n,
 46       idx        TYPE i,
 47       idx_tmp    TYPE i.
 48 
 49 "定义内表,定义SALV 的类CL_GUI_SALV_TABLE的对象变量
 50 DATA gt_stock_id TYPE ty_tab_stock_id.
 51 DATA gs_stock_id TYPE ty_stock_id.
 52 
 53 "定义股票信息内表
 54 DATA gt_stock TYPE ty_tab_stock_live .
 55 DATA gs_stock TYPE  ty_stock_live.
 56 DATA: gt_stock_pub_list TYPE TABLE OF string.
 57 
 58 "定义股票信息OOALV相关类
 59 DATA go_table TYPE REF TO cl_salv_table.
 60 DATA go_functions TYPE REF TO cl_salv_functions_list.
 61 DATA: go_columns TYPE REF TO cl_salv_columns_table.
 62 DATA: go_column TYPE REF TO cl_salv_column_table.
 63 DATA: go_column_list TYPE REF TO cl_salv_column_list.
 64 DATA:go_container TYPE REF TO cl_gui_custom_container.
 65 DATA: go_dock TYPE REF TO cl_gui_docking_container,
 66       go_picture   TYPE REF TO cl_gui_picture.
 67 DATA: gv_url        TYPE char256,
 68       gt_data           TYPE STANDARD TABLE OF x255.
 69 
 70 "本地类用于获得股票信息
 71 CLASS lcl_stock DEFINITION DEFERRED.
 72 "本地类用于处理双击事件
 73 CLASS lcl_event_handler DEFINITION DEFERRED.
 74 
 75 DATA go_stock TYPE REF TO lcl_stock.
 76 DATA: go_timer TYPE REF TO cl_gui_timer,
 77       go_evt_hndl TYPE REF TO lcl_event_handler.
 78 
 79 "定义本地类
 80 CLASS lcl_stock DEFINITION.
 81   PUBLIC SECTION.
 82     TYPES:
 83       BEGIN OF ty_stock_live,
 84              sid    TYPE char10, "股票ID
 85              sname  TYPE char10, "股票名字
 86              topen  TYPE char10, "今日开盘价
 87              yclose TYPE char10, "昨日收盘价;
 88              cprice TYPE char10, "当前价格;
 89              thigh  TYPE char10, "当日最高价
 90              tlow   TYPE char10, "今日最低价
 91              buy    TYPE char10, "竞买价,即“买一”报价;
 92              sale   TYPE char10, "竞卖价,即“卖一”报价;
 93              number TYPE char20, "成交的股票数
 94              amount TYPE char20, "成交金额
 95              buy11  TYPE char10, "买一,数量
 96              buy12  TYPE char10, "买一,报价
 97              buy21  TYPE char10, "买二,数量
 98              buy22  TYPE char10, "买二,报价
 99              buy31  TYPE char10, "买三,数量
100              buy32  TYPE char10, "买三,报价
101              buy41  TYPE char10, "买四,数量
102              buy42  TYPE char10, "买四,报价
103              buy51  TYPE char10, "买五,数量
104              buy52  TYPE char10, "买五,报价
105              sale11 TYPE char10, "卖一,数量
106              sale12 TYPE char10, "卖一,报价
107              sale21 TYPE char10, "卖二,数量
108              sale22 TYPE char10, "卖二,报价
109              sale31 TYPE char10, "卖三,数量
110              sale32 TYPE char10, "卖三,报价
111              sale41 TYPE char10, "卖四,数量
112              sale42 TYPE char10, "卖四,报价
113              sale51 TYPE char10, "卖五,数量
114              sale52 TYPE char10, "卖五,报价
115              date   TYPE char15, "日期
116              time   TYPE char15, "时间
117              color  TYPE lvc_t_scol, "Color.
118            END OF ty_stock_live .
119     TYPES:
120       ty_tab_stock_live TYPE STANDARD TABLE OF ty_stock_live .
121     TYPES:
122       BEGIN OF ty_stock_id,
123              stock_id TYPE char6,
124            END OF ty_stock_id .
125     TYPES:
126       ty_tab_stock_id TYPE STANDARD TABLE OF ty_stock_id .
127 
128     DATA gv_stock_id TYPE char6 .
129     DATA mt_stock TYPE ty_tab_stock_live .
130     DATA ms_stock TYPE ty_stock_live .
131     DATA mt_stock_id TYPE ty_tab_stock_id .
132     DATA ms_stock_id TYPE ty_stock_id .
133     DATA:
134       mt_stock_pub_list TYPE TABLE OF string .
135 
136     METHODS get_live_stock_info
137       IMPORTING
138         !it_stock_id TYPE ty_tab_stock_id .
139     METHODS fill_alv
140       CHANGING
141         VALUE(ct_stock) TYPE ty_tab_stock_live .
142   PROTECTED SECTION.
143   PRIVATE SECTION.
144 ENDCLASS.
145 
146 
147 
148 CLASS lcl_stock IMPLEMENTATION.
149   "定义本地类方法用于获得新浪财经实时股票信息
150   METHOD get_live_stock_info.
151     DATA: lo_abap_conv     TYPE REF TO cl_abap_conv_in_ce,
152           li_http_client   TYPE REF TO if_http_client,
153          lv_stock_pub_url TYPE string VALUE \'http://hq.sinajs.cn/list=\',
154           abap_encoding    TYPE abap_encoding,
155           l_str            TYPE string,
156           l_xstr           TYPE xstring,
157           l_tmp            TYPE string,
158           l_url            TYPE string.
159     DATA: BEGIN OF wa_num,
160             num(6) TYPE c,
161           END OF wa_num.
162     DATA: BEGIN OF wa_lin,
163             str TYPE string,
164           END OF wa_lin.
165     DATA: it_num    LIKE TABLE OF wa_num,
166           it_restab LIKE TABLE OF wa_num,
167           it_res    LIKE TABLE OF wa_lin.
168     IF it_stock_id[] IS INITIAL.
169       RETURN.
170     ENDIF.
171     CLEAR:it_res.
172     REFRESH:it_res.
173     LOOP AT it_stock_id INTO ms_stock_id.
174       CLEAR l_tmp.
175       IF ms_stock_id-stock_id(1) < 6.
176         CONCATENATE \'sz\' ms_stock_id-stock_id INTO l_tmp.
177       ENDIF.
178       IF ms_stock_id-stock_id(1) = \'6\'.
179         CONCATENATE \'sh\' ms_stock_id-stock_id INTO l_tmp.
180       ENDIF.
181       CONCATENATE l_str l_tmp INTO l_str SEPARATED BY \',\'.
182     ENDLOOP.
183     IF l_str(1) = \',\'.
184       SHIFT l_str LEFT DELETING LEADING \',\'.
185     ENDIF.
186     CONCATENATE lv_stock_pub_url l_str INTO l_url.
187     CALL METHOD cl_http_client=>create_by_url
188       EXPORTING
189         url                = l_url
190       IMPORTING
191         client             = li_http_client
192       EXCEPTIONS
193         argument_not_found = 1
194         plugin_not_active  = 2
195         internal_error     = 3
196         OTHERS             = 4.
197     CALL METHOD li_http_client->request->set_header_field
198       EXPORTING
199         name  = \'Content-Type\'
200         value = \'text/html;charset=utf-8\'. " utf-8
201     CALL METHOD li_http_client->send
202       EXCEPTIONS
203         http_communication_failure = 1
204         http_invalid_state         = 2.
205     CALL METHOD li_http_client->receive
206       EXCEPTIONS
207         http_communication_failure = 1
208         http_invalid_state         = 2
209         http_processing_failed     = 3.
210     l_xstr = li_http_client->response->get_data( ).
211     IF sy-subrc = 0 .
212       li_http_client->close( ).
213     ENDIF.
214     CALL METHOD cl_abap_conv_in_ce=>create
215       EXPORTING
216         input       = l_xstr
217         encoding    = \'8400\'
218         replacement = \'?\'
219         ignore_cerr = abap_true
220       RECEIVING
221         conv        = lo_abap_conv.
222     TRY.
223         CALL METHOD lo_abap_conv->read
224           IMPORTING
225             data = l_str.
226       CATCH cx_sy_conversion_codepage.
227       CATCH cx_sy_codepage_converter_init.
228       CATCH cx_parameter_invalid_type.
229       CATCH cx_parameter_invalid_range.
230     ENDTRY.
231     REFRESH mt_stock_pub_list.
232     SPLIT l_str AT cl_abap_char_utilities=>newline
233     INTO TABLE mt_stock_pub_list.
234 
235   ENDMETHOD.
236   "定义本地类方法用于填充股票信息内表
237   METHOD fill_alv.
238     DATA lt_res TYPE TABLE OF string.
239     DATA l_str TYPE string.
240     DATA l_str_field TYPE string.
241     FIELD-SYMBOLS: <fs_value> TYPE any.
242 
243     FIELD-SYMBOLS: <fs_stock> TYPE ty_stock_live.
244     DATA: ls_color               TYPE lvc_s_scol.
245     DATA: tmp_num(6) TYPE n,
246           idx        TYPE i,
247           idx_tmp    TYPE i.
248 
249     REFRESH lt_res.
250     REFRESH mt_stock.
251     LOOP AT mt_stock_pub_list INTO l_str.
252       IF strlen( l_str ) < 30.
253         CONTINUE.
254       ENDIF.
255       SPLIT l_str AT \',\' INTO TABLE lt_res.
256       READ TABLE lt_res INDEX 1 INTO l_str_field.
257       ms_stock-sid = l_str_field+13(6).
258       ms_stock-sname = l_str_field+21.
259 
260       idx = 2.
261       WHILE idx < 33.
262         READ TABLE lt_res INDEX idx INTO l_str_field.
263         idx = idx + 1.
264         ASSIGN COMPONENT idx OF STRUCTURE ms_stock TO <fs_value>.
265         <fs_value> = l_str_field.
266       ENDWHILE.
267 
268       INSERT ms_stock INTO TABLE mt_stock.
269     ENDLOOP.
270 
271     "填充股票信息内表的颜色列
272     LOOP AT mt_stock ASSIGNING <fs_stock>.
273       REFRESH <fs_stock>-color.
274       IF <fs_stock>-cprice > <fs_stock>-topen.
275         ls_color-color-col = 6. "上涨,红色
276         ls_color-color-int = 1.
277         ls_color-color-inv = 0.
278         APPEND ls_color TO <fs_stock>-color.
279       ELSE.
280         IF <fs_stock>-cprice < <fs_stock>-topen.
281           ls_color-color-col = 5." 下跌,绿色
282           ls_color-color-int = 0.
283           ls_color-color-inv = 0.
284           APPEND ls_color TO <fs_stock>-color.
285         ENDIF.
286       ENDIF.
287     ENDLOOP.
288 
289     ct_stock = mt_stock.
290 
291   ENDMETHOD.
292 
293 
294 ENDCLASS.
295 
296 
297 
298 "定义ALV双击事件类
299 CLASS lcl_handle_events DEFINITION.
300   PUBLIC SECTION.
301     METHODS: on_double_click FOR EVENT double_click OF
302 cl_salv_events_table
303       IMPORTING
304           row "事件触发所在的行号
305           column."事件触发所在的列名
306     METHODS: on_user_command FOR EVENT added_function OF
307 cl_salv_events_table
308       IMPORTING e_salv_function.
309 ENDCLASS.
310 
311 CLASS lcl_handle_events IMPLEMENTATION.
312   METHOD on_double_click.
313     PERFORM show_stock_details
314     USING row column.
315   ENDMETHOD.
316   METHOD on_user_command.
317     PERFORM handle_user_command ABAP中定义一个webdynpro,已做好layout,如何用ALV方式查询数据?请教详细步骤!如哪定义ALV。

求助:Web Dynpro for ABAP的ALV如何得到被选中的多行

linux中怎么查看mysql数据库版本

◆◆0SALV的一些限制和注意事项汇总

◆◆0SALV教程20-单元格可编辑

SALV双击事件,相应另一个SALV