webclient 采集 ajax 生成数据,在SAP WebClient UI里使用AJAX进行异步数据读取

本文介绍了如何在Web客户端UI组件中实现AJAX功能,通过创建ICF节点和后台接口,实现在输入字段输入'a'后立即显示包含'a'的SCARR表数据,无需手动刷新。详细步骤包括创建URL、定义JavaScript函数和监控AJAX请求。
摘要由CSDN通过智能技术生成

For POC purpose I need to implement the AJAX functionality in Webclient UI component. The UI component has only one input field:

199773353_1_20200820095439958.jpg

Once type “a” into the input field, it immediately displays all the records in the database table SCARR whose column carrname contains the character “a” ( without any other operation like manual refresh)

199773353_2_202008200954405_wm.jpg

change the value in the input field, the page will display the latest matched result automatically:

199773353_3_20200820095440208_wm.jpg

Here below are the steps how to build this very simple UI component which implement AJAX functionality:

(1) Create a new UI component and a new empty view

In the html view, paste the following code: Part1

create_url( iv_path = '/sap/crm/zajax'iv_query = lv_queryiv_in_same_session = 'X' ).%>

Since we will send asynchronous xml request to ABAP backend to query records from database table. The query must be implemented in ABAP backend. Here I will create a new ICF service node to accomplish such query. The creation of ICF node and its handler class will be discussed later. In this step we just need to pass in the ICF node path ‘/sap/crm/zajax’ to metod create_url. That method will return the url which would be used as prefix of the final url of the asynchronous request to be sent to ABAP backend.

Part2

Here we define the four JavaScript functions:

function GetXmlHttpObject(){ if (window.XMLHttpRequest) {    return new XMLHttpRequest(); } if (window.ActiveXObject) {    return new ActiveXObject("Microsoft.XMLHTTP"); } return null;}

comment: this function is designed to support different kinds of browsers.

function stateChanged() {  if (xmlhttp.readyState == 4) {     document.getElementById("result").innerHTML = xmlhttp.responseText;     document.getElementById("result").style.border = "1px solid #A5ACB2";  }}comment: this function is callback function which will automatically be called when the backend response returned by our ICF handler class is available to consume ( that means, the result is ready to be displayed in the frontend UI )

function getRequestURL(str) {  var url = "" + str;  url = url + "&sid=" + Math.random();  return url;}

comment: this function will assemble the final url which is to be sent to ABAP backend. The ABAP variable lv_url contains the full url of our icf node appended with request prefix “query=”. So within this function we just simply concatenate the string which is typed by end user in the input field.

function showResult(str){ if (str.length == 0 ) {   document.getElementById("result").innerHTML = "";   document.getElementById("result").style.border = "0px";   return; } xmlhttp = GetXmlHttpObject(); if (xmlhttp == null ){   alert ("Your browser does not support XML HTTP Request");   return; } var requesturl = getRequestURL(str); xmlhttp.onreadystatechange = stateChanged ; xmlhttp.open("GET",requesturl,true); xmlhttp.send(null);}comment: we will bind this function to event onkeyup of input field, so that once we finish the typing in input field, it will be called. Within it, the asynchronous xml request is sent. We bind our callback function “stateChanged” to xmlhttp.onreadystatechange and do not need to care about when to call it – instead the framework will call this callback automatically when it should be called.

Part3

for the complete source code which could directly be “Ctrl + C” and “Ctrl + V”, please find it in attachment.

(2) Create a new ICF node and its handler class

Use tcode SICF, create a new ICF node.

199773353_4_20200820095440255_wm.jpg

The path should be consistent with the hardcode path in the method call in step one:

lv_url = cl_crm_web_utility=>create_url( iv_path = '/sap/crm/zajax'  iv_query = lv_query  iv_in_same_session = 'X' ).

Create a new handler class which implement interface IF_HTTP_EXTENSION:

199773353_5_20200820095440442_wm.jpg

Implement the method HANDLE_REQUEST as below:

method IF_HTTP_EXTENSION~HANDLE_REQUEST.DATA: lv_input_str TYPE string,lv_html TYPE string,lt_scarr TYPE TABLE OF scarr.FIELD-SYMBOLS:  TYPE scarr.lv_input_str = server->request->get_form_field( 'query' ).SELECT * FROM scarr INTO TABLE lt_scarr.IF strlen( lv_input_str ) > 0.  LOOP AT lt_scarr ASSIGNING .    FIND lv_input_str IN -carrname IGNORING CASE.    CHECK sy-subrc = 0.    IF strlen( lv_html ) = 0.      CONCATENATE `-url `’ target=’_blank’>`      -carrname `` INTO lv_html.    ELSE.      CONCATENATE lv_html `
` `-url `’ target=’_blank’>`      -carrname `` INTO lv_html.    ENDIF.  ENDLOOP.ENDIF.IF strlen( lv_html ) = 0.  lv_html = '<no suggestion>'.ENDIF.server->response->set_cdata( lv_html ).endmethod.

Monitor AJAX request and response in Chrome

It is very convenient to monitor AJAX behavior via developer tool in Chrome. Launch the U component with Chrome, click F12 to open developer tool. Then mark the checkbox “Any XHR” under “XHR Breakpoints”.

199773353_6_20200820095440505_wm.jpg

So that once there is a AJAX request sent from your application, the developer tool will automatically stop at the very code in which the XHR ( XML Header Request ) is sent: Switch to debug mode, and then type a character like “a” in the input field, the session will stop – The UI becomes gray and there is a tooltip “Paused in debugger” in the top-right part of the window:

199773353_7_20200820095440614_wm.png

In the left-most part of development tool, you can observe which view the AJAX request is sent from. In our example from prefix “bspwd_cmp_test” we could judge that currently our ui component is launched in test mode ( by clicking the test button in UI Component Workbench); In the middle part we could see the exact line where the request is sent;

In the right most part we could check the detail value of variables used in JavaScript and the function callstack, just the same logic as ABAP debugger. For example we could get the detail of XHR like request url, and what character end user has input.

199773353_8_20200820095440677_wm.jpg

click F10 to step over until the response is returned from ABAP backend.

199773353_9_20200820095440755_wm.jpg

Put the mouse into field “responseText” and it will display the complete content of it:

199773353_10_20200820095440849_wm.jpg

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值