Data bind a component in an Oracle JET virtual DOM app
Introduction
This tutorial shows you how to use a local JSON document in your Oracle JavaScript Extension Toolkit (Oracle JET) virtual DOM app.
Oracle JET is a development toolkit that provides a number of data provider classes to manage data from various sources. In this tutorial, you’ll populate an instance of the MutableArrayDataProvider
class with the data you read from a JSON file, and then bind the MutableArrayDataProvider
instance to Oracle JET custom HTML elements.
Objectives
In this tutorial, you will learn how to read data from a JSON document into a data provider, add an Oracle JET List View component, and data bind the Oracle JET List View component to the JSON data objects.
Prerequisites
- A development environment set up to create Oracle JET virtual DOM apps that includes an installation of Node.js
- Completion of the previous tutorial in this learning path, Add Components to an Oracle JET Virtual DOM App
- The store_data.json downloaded to the
JET-Virtual-DOM-app/src/components/
directory
Task 1: Download the JSON Document and Examine the Data
-
Download the
store_data.json
file to theJET-Virtual-DOM-app/src/components
directory. -
Open the
store_data.json
file in your web browser and view the data structure. Thestore_data.json
file contains a list of activity objects and their properties. Each activity includes an array of items with their own properties.
Task 2: Populate a Data Provider in Your Virtual DOM App
Push data from the JSON file into an instance of an Oracle JET MutableArrayDataProvider
class.
-
Navigate to the
JET-Virtual-DOM-app/src/components/content
directory and open theindex.tsx
file in an editor. -
Import the data from the
store_data.json
file so that it is available in your virtual DOM app.import { h, ComponentProps } from "preact"; import { useState, useCallback } from "preact/hooks"; import "ojs/ojlabel"; import "ojs/ojselectsingle"; import { ojSelectSingle } from "ojs/ojselectsingle"; import MutableArrayDataProvider = require("ojs/ojmutablearraydataprovider"); import "ojs/ojchart"; import { ojChart } from "ojs/ojchart"; import * as storeData from "text!../store_data.json";
The
MutableArrayDataProvider
module that you use to hold the JSON data was imported in the previous tutorial. -
Before the
Content ()
function, define theActivity
type alias, which includes theid
field of typenumber
and thename
field of typestring
.... type Activity = { id: number; name: string; } export function Content() { . . .
-
Also before the
Content ()
function, create an instance ofMutableArrayDataProvider
(activityDataProvider
) and use theJSON.parse
method to read in the JSON data.... type Activity = { id: number; name: string; } const parsedData = typeof storeData === "string" ? JSON.parse(storeData) : storeData; const activityDataProvider = new MutableArrayDataProvider<Activity["id"], Activity>( parsedData, { keyAttributes: "id" } ); export function Content() { . . .
-
Import the type definition and the module for the Oracle JET List View component as well as the module for the List Item Layout component.
. . . import MutableArrayDataProvider = require("ojs/ojmutablearraydataprovider"); import "ojs/ojchart"; import { ojChart } from "ojs/ojchart"; import * as storeData from "text!../store_data.json"; import "ojs/ojlistview"; import { ojListView } from "ojs/ojlistview"; import "ojs/ojlistitemlayout";
-
Save the
index.tsx
file.Your
index.tsx
file should look similar to final-data-bind-index-tsx.txt.
Task 3: Bind Data to the List View Component
The oj-list-view
element is an Oracle JET custom HTML element with interactive features that is an enhanced version of an HTML list. You provide the data to the list by binding the activityDataProvider
instance to the data
attribute of the List View component.
-
In the open
index.tsx
file, add adiv
element with anid
attribute value ofactivitiesContainer
above theitemDetailsContainer
div
element that contains the Oracle JET Select Single and Chart components.<div class="oj-web-applayout-max-width oj-web-applayout-content"> <h1>Product Information</h1> <div id="activitiesContainer"> </div> <div id="itemDetailsContainer"> <oj-label for="basicSelect">Select Chart:</oj-label> <oj-select-single . . .
-
Within the
activitiesContainer
div
element, add anoj-list-view
element with adata
attribute that references the instance ofMutableArrayDataProvider
we created previously (activityDataProvider
).<div class="oj-web-applayout-max-width oj-web-applayout-content"> <h1>Product Information</h1> <div id="activitiesContainer"> <oj-list-view id="activitiesList" aria-labelledby="activitiesHeader" data= {activityDataProvider} gridlines={gridlinesItemVisible}> </oj-list-view> </div> <div id="itemDetailsContainer"> <oj-label for="basicSelect">Select Chart:</oj-label> <oj-select-single . . .
We specify the
aria-labelledby
attribute as an accessibility best practice, and we also specify thegridlinesItemVisible
value for thegridlines
attribute to display a grid line between items in the list. We’ll perform an additional step to access thegridlines
attribute of the List View component. -
Within the
oj-list-view
element you created, add an HTMLtemplate
element withslot
andrender
attributes to render each item in the list.return ( <div class="oj-web-applayout-max-width oj-web-applayout-content"> <h1>Product Information</h1> <div id="activitiesContainer"> <oj-list-view id="activitiesList" aria-labelledby="activitiesHeader" data= {activityDataProvider} gridlines={gridlinesItemVisible}> <template slot="itemTemplate" render={renderListItem}></template> </oj-list-view> </div>
-
Within the
activitiesContainer
div
inside thereturn
statement, add an HTMLh3
element to supply a header for the list of activities.<div id="activitiesContainer"> <h3 id="activitiesHeader">Activities</h3> <oj-list-view id="activitiesList" aria-labelledby="activitiesHeader" data={activityDataProvider} gridlines={gridlinesItemVisible} > <template slot="itemTemplate" render={renderListItem}></template> </oj-list-view> </div>
-
In the
itemDetailsContainer
div
, add anh3
element to supply a header for the chart.<div id="itemDetailsContainer"> <h3>Item Details</h3> <oj-label for="basicSelect">Select Chart:</oj-label> <oj-select-single
-
Before the
return
statement in theContent()
function, add arenderListItem
function that returns a list of activity names to render in the List View component.. . . export function Content() { . . . const renderListItem = (item: ojListView.ItemTemplateContext<Activity["id"], Activity> ) => { return ( <li> <oj-list-item-layout> <div class="oj-typography-body-md"> {item.data.name} </div> </oj-list-item-layout> </li> ); } ; return ( . . . <oj-list-view . . .
-
Before the
Content()
function, declare aListViewProps
type and use it to create a variable (gridlinesVisible
) with a value ofvisible
.. . . type ListViewProps = ComponentProps<"oj-list-view">; const gridlinesItemVisible: ListViewProps["gridlines"] = { item: "visible" }; export function Content() { const renderListItem = . . .
-
Save the
index.tsx
file.Your
index.tsx
file should look similar to index.tsx.txt.
Task 4: Run the Virtual DOM App
-
In the terminal window, change to the
JET-Virtual-DOM-app
directory and run the app.npx ojet serve
Your browser displays the list of activities visible in your virtual DOM app.
-
Close the browser window or tab that displays your running virtual DOM app.
-
In the terminal window, press Ctrl+C, and, if prompted, enter
y
to exit the Oracle JET tooling batch job. -
Leave the terminal window open for your next tutorial.
Next Step
To proceed to the next tutorial in this learning path, click here.
More Learning Resources
Explore other labs on docs.oracle.com/learn or access more free learning content on the Oracle Learning YouTube channel. Additionally, visit education.oracle.com/learning-explorer to become an Oracle Learning Explorer.
For product documentation, visit Oracle Help Center.
Data bind a component in an Oracle JET virtual DOM app
F61085-03
December 2024