Elements
webforJ developers have the option of choosing not only from the rich library of components provided, but also integrating components from elsewhere. To facilitate this, the Element
component can be used to simplify the integration of anything from simple HTML elements, to more complex custom web components.
The Element
component cannot be extended, and is not the base component for all components within webforJ. To read more about webforJ's component hierarchy, read this article.
Show Code
- Java
- CSS
Adding events
In order to utilize events that may come with your element, you can use the Element
component's addEventListener
methods. Adding an event requires at least the type/name of the event the component expects, and a listener to be added to the event.
There are also additional options to further customize events by using the Event Options configurations.
Show Code
- Java
- CSS
Component interaction
The Element
component acts like a container for other components. It's provides a way to organize and retrieve information for child components, and offers a clear set of functions to add or remove these child components as needed.
Adding child components
The Element
component supports the composition of child components. Developers can organize and manage complex UI structures by adding components as children to the Element
. Three methods exist to set content within the an Element
:
-
add(Component... components)
: This method will allow one or multiple components to be added to an optionalString
which designates a specified slot when used with a Web Component. Omitting the slot will add the component between the HTML tags. -
setHtml(String html)
: This method take theString
passed to the method and inject it as HTML within the component. Depending on theElement
, this may be rendered in different ways. -
setText(String text)
: This method behaves similarly to thesetHtml()
method, but injects literal text into theElement
.
Show Code
- Java
- CSS
Calling setHtml()
or setText()
will replace content currently contained between the element's opening and closing tags.
Removing components
In addition to adding components to an Element
, the following methods are implemented for removal of various child components:
-
remove(Component... components)
: This method takes one or more components and will remove them as child components. -
removeAll()
: This method will remove all child components from theElement
.
Accessing components
To access the various child components present within an Element
, or information regarding these components, the following methods are available:
-
getComponents()
: This method returns a JavaList
of all children of theElement
. -
getComponents(String id)
: This method is similar to the method above, but will take the server-side ID of a specific component and return it when found. -
getComponentCount()
: Returns the number of child components present within theElement
.
Calling JavaScript functions
The Element
component provides two API methods which allow for JavaScript functions to be called on HTML elements.
-
callJsFunction(String functionName, Object... arguments)
: This method takes a function name as a string, and optionally takes one or more Objects as parameters for the function. This method is executed synchronously, meaning that the executing thread is blocked until the JS method returns, and results in a round trip. The results of the function are returned as anObject
, which can be casted and used in Java. -
callJsFunctionAsync(String functionName, Object... arguments)
: As with the previous method, a function name and optional arguments for the function can be passed. This method executes asynchronously and does not block the executing thread. It returns aPendingResult
, which allows for further interaction with the function and its payload.
Passing parameters
Arguments that are passed to these methods which are used in the execution of JS functions are serialized as a JSON array. There are two notable arguments types that are handled as follows:
this
: Using thethis
keyword will give the method a reference to the client-side version of the invoking component.Component
: Any Java component instances passed into one of the JsFunction methods will be replaced with the client-side version of the component.
Both synchronous and asynchronous function calling will wait to call a method until the Element
has been added to the DOM before executing a function, but callJsFunction()
will not wait for any component
arguments to attach, which can result in failure. Conversely, invoking callJsFunctionAsync()
may never complete if a component argument is never attached.
In the demo below, an event is added to an HTML Button
. This event is then fired programmatically by calling the callJsFunctionAsync()
method. The resulting PendingResult
is then used to create another message box once the asynchronous function has been completed.
Show Code
- Java
- CSS
Executing JavaScript
In addition to executing JavaScript from the application level, it is also possible to execute JavaScript from the Element
level as well. Performing this execution at the Element
level allows the context of the HTML element to be included in the execution. This is a powerful tool that acts as a developer's conduit to interactive capabilities with client-side environments.
Similar to function execution, executing JavaScript can be done with synchronously or asynchronously with the following methods:
-
executeJs(String script)
: This method takesString
, which will be executed as JavaScript code in the client. This script is executed synchronously, meaning that the executing thread is blocked until the JS execution returns and results in a round trip. The results of the function are returned as anObject
, which can be casted and used in Java. -
executeJsAsync(String script)
: As with the previous method, a passedString
parameter will be executed as JavaScript code on the client. This method executes asynchronously and does not block the executing thread. It returns aPendingResult
, which allows for further interaction with the function and its payload.
These methods have access to the component
keyword, which gives the JavaScript code access to the client-side instance of the component executing the JavaScript.