`
JerryWang_SAP
  • 浏览: 1032232 次
  • 性别: Icon_minigender_1
  • 来自: 成都
文章分类
社区版块
存档分类
最新评论

在SAP WebClient UI里使用AJAX进行异步数据读取

阅读更多

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

 

 

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)

 

 

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

 

 

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

<%@page language="abap"%>
<%@extension name="htmlb" prefix="htmlb"%>
<%
data: lv_url TYPE string,
lv_query type string.
lv_query = 'query='.
lv_url = cl_crm_web_utility=>create_url( iv_path = '/sap/crm/zajax'
iv_query = lv_query
iv_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 = "<%= lv_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

<body>
input name: <input type="text" id="fname" onkeyup="showResult(this.value)" />
<div id = "result" ></div>
</body>
comment: just bind the event handler to event “onkeyup”.

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.

 

 

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:

 

 

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: <fs_scarr> 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 <fs_scarr>.
    FIND lv_input_str IN <fs_scarr>-carrname IGNORING CASE.
    CHECK sy-subrc = 0.
    IF strlen( lv_html ) = 0.
      CONCATENATE `<a href=’` <fs_scarr>-url `’ target=’_blank’>`
      <fs_scarr>-carrname `</a>` INTO lv_html.
    ELSE.
      CONCATENATE lv_html `<br />` `<a href=’` <fs_scarr>-url `’ target=’_blank’>`
      <fs_scarr>-carrname `</a>` INTO lv_html.
    ENDIF.
  ENDLOOP.
ENDIF.

IF strlen( lv_html ) = 0.
  lv_html = '&lt;no suggestion&gt;'.
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”.

 

 

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:

 

 

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.

 

 

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

 

 

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

 

 

要获取更多Jerry的原创文章,请关注公众号"汪子熙":

0
0
分享到:
评论

相关推荐

    CRM7.0 WebClient UI config

    在SAP CRM 7.0版本中,WebClient UI配置是一项关键任务,它涉及到了用户界面的定制和优化,以提升用户体验和业务效率。WebClient UI是SAP CRM的一个核心组件,提供了一个基于Web的交互界面,使得用户能够访问和操作...

    基于WebClient的Ftp异步文件上传

    类库版可能提供更高级别的抽象,封装了FTP服务器连接、文件分包和合并等细节,而组件版可能是一个可复用的UI控件,允许开发者在用户界面上直接拖放文件进行上传。 总的来说,`WebClient`的异步FTP文件上传功能为...

    C#webClient.DownloadFileAsync异步源码

    在编程领域,异步操作是提高程序性能和用户体验的关键技术之一。C# 是一种广泛使用的.NET编程语言,它提供了丰富的异步支持。本教程将详细解释`WebClient.DownloadFileAsync`方法,这是一个异步下载文件的功能,适用...

    C#使用WebClient获取网页源文件例子

    在这个“C#使用WebClient获取网页源文件例子”中,我们将深入探讨如何利用`WebClient`来下载网页源代码,并进行UTF8编码的解码处理。 首先,`WebClient`类是`System.Net`命名空间的一部分,它提供了一个简洁的接口...

    WebClient下载文件展示进度条

    下面是一个完整的示例,展示了如何在WinForms应用程序中创建一个简单的进度条,并使用WebClient下载文件: ```csharp public partial class MainForm : Form { private WebClient webClient; private long ...

    silverlight 使用webclient 实现上传和下载数据流

    在开发Silverlight应用程序时,网络通信是一个重要的组成部分。为了实现数据的上传和下载,可以使用.NET ...对于初学者而言,了解如何使用WebClient类进行数据上传和下载是学习Silverlight开发的一个重要方面。

    WebClient方式 上传数据到服务器 IIS发布网站

    - 对敏感数据进行加密,防止在传输过程中被截获。 - 考虑使用身份验证机制,如Basic Auth、OAuth等,确保只有授权用户可以上传文件。 总结,`WebClient`是.NET开发中一个强大且实用的工具,它简化了向IIS服务器上传...

    C# 使用WebClient类 下载网络指定资源

    需要注意的是,这个事件在异步下载完成后触发,无论下载是否成功: ```csharp client.DownloadFileCompleted += (sender, e) =&gt; { if (e.Cancelled) Console.WriteLine("下载已取消"); else if (e.Error != null...

    WEBCLIENT使用方法

    `WebClient`也支持异步操作,可以避免阻塞UI线程。例如异步下载文件: ```csharp client.DownloadFileAsync(new Uri("http://example.com/file.txt"), "localFilePath.txt"); client.DownloadFileCompleted += ...

    使用WebClient实现文件下载

    在.NET框架中,C#提供了一种简单的方式来下载文件,这就是使用`WebClient`类。`WebClient`是System.Net命名空间的一部分,它提供了一系列方法和属性,方便开发者执行HTTP请求,包括文件的上传和下载。在这个场景中,...

    C#通过webclient下载demo

    - 异步版本:`DownloadFileTaskAsync(Uri address, String fileName)`,这个方法返回一个`Task`对象,可以在异步编程中使用。 2. **显示下载进度**: - 要显示下载进度,我们需要订阅`WebClient`的`...

    Java爬虫Jsoup+httpclient获取动态生成的数据

    这些数据在用户打开网页时不会立即展示,而是在页面加载之后通过JavaScript异步调用接口进行加载。这类数据的抓取对爬虫来说是一个挑战,因为爬虫默认情况下只能获取初始加载的HTML内容,而不会执行任何JavaScript...

    WebClientDemo_webclient_

    通过学习和理解这些源代码,开发者可以了解到网络请求的基本流程,异步编程的概念,以及如何在UI中实时反映下载状态。此外,还能学习到如何自定义WebClient以满足特定需求,以及如何在C#应用程序中实现日志记录和...

    C#中在WebClient中使用post发送数据实现方法

    在某些场景下,我们需要使用POST方法来传递数据,比如提交表单或者与Web服务进行交互。POST方法相比于GET方法的优势在于它可以传输更大的数据量,不受URL长度限制。 首先,让我们深入了解WebClient类。WebClient类...

    C# WebClient 上传文件

    ### C# WebClient 上传文件知识点解析 ...最后,虽然`WebClient`类提供了一个简单易用的接口,但在某些场景下,可能需要更复杂的控制和配置,这时可以考虑使用`HttpClient`等更现代的API来进行文件上传。

    C#中WebClient实现文件下载

    为了确保总是获取最新文件,可以在URL后面附加一个随机查询字符串,这样每次请求看起来都是不同的,CDN就不会使用缓存的版本: ```csharp string urlWithTimestamp = $"{url}?{DateTime.Now.Ticks}"; wc....

    SAPCRM基础培训.doc

    在SAP CRM基础培训中,WebClient UI(Web客户端用户界面)是一个重要的学习焦点,它是用户与系统交互的主要界面。下面我们将深入探讨WebClient UI的相关知识点。 WebClient UI的发展历程是从GUI(图形用户界面)...

    Creating a Dropdown in SAP CRM WebUI.docx

    在SAP CRM WebUI中创建下拉列表是一个关键任务,特别是在设计用户界面以提供更高效、用户友好的交互时。本教程将引导你通过一系列步骤来实现这一目标,特别是针对SEX(性别)属性创建一个下拉框。 首先,你需要获取...

    SAP_CRM中文自学笔记

    SAP CRM的基础知识通常包括对于其核心组件的理解,包括主数据(如账户、组织模型、产品等),交易处理、定价、开票、CRM中间件以及CRM WebClient UI。CRM中间件主要负责不同系统组件之间的信息交换,而CRM WebClient...

    SAPCRM基础培训教材.doc

    从最初的GUI界面,到PCUI,再到IC WebClient,最终发展到目前最新的WebClient UI,这一过程中,用户界面不断优化,以适应不同设备和用户的使用需求。在CRM 7.0版本中,引入了BSP/BOL技术和HTTP协议,允许用户通过...

Global site tag (gtag.js) - Google Analytics