Create the master view in an Oracle JET virtual DOM app
Introduction
This tutorial discusses how to read master-detail hierarchical data from a local JavaScript Object Notation (JSON) document and display the data in a List View component of an Oracle JavaScript Extension Toolkit (Oracle JET) virtual DOM app.
In Oracle JET, you can use the Oracle JET API to create a data provider object. The object represents a data array that you populate from the parent objects of a JSON document data store. The data
attribute of Oracle JET custom HTML elements binds to the data through an instance of a MutableArrayDataProvider
data provider object that you create.
Objectives
In this tutorial, you will data bind multiple items of the parent data objects in a local JSON document and populate the rows of an Oracle JET List View component in the Activity Container component with that data.
Prerequisites
- A development environment set up to create Oracle JET virtual DOM apps that includes an installation of Node.js
- Google Chrome web browser installed and set as the default web browser
- Access to the Oracle JET Developer Cookbook
- (Option 1) Completion of the final tutorial in the previous learning path in this series: Test the Oracle JET Virtual DOM App on Different Screen Sizes
- (Option 2) If you haven’t completed the previous learning path in this series: the jet-virtual-dom-app-temp.zip downloaded
- The
product_images.zip
downloaded to theJET-Virtual-DOM-app/src/styles/images
directory
Task 1: Download the Starter Virtual DOM App
Skip this task if you’re continuing to work in an app that you created in the previous learning path.
-
Rename
jet-virtual-dom-app-temp.zip
asJET-Virtual-DOM-app.zip
. Extract the contents to theJET-Virtual-DOM-app
directory. -
In the terminal window, navigate to the
JET-Virtual-DOM-app
directory, and restore the Oracle JET app.npm install
Your app is ready to use.
Task 2: Download the Product Images
Skip this task if you downloaded the starter app in jet-virtual-dom-app-temp.zip
.
-
If the Oracle JET tooling server batch job is running in the terminal window, press Ctrl+C, and if prompted, enter
y
to exit the server batch job.The server batch job only recognizes revisions that you make to existing app files. After you create new files, you must run the virtual DOM app again.
-
Download the
product_images.zip
file to theJET-Virtual-DOM-app/src/styles/images
directory. -
Extract the content of the zip file into a new
product_images
directory in theimages
directory.The path to the new directory is
JET-Virtual-DOM-app/src/styles/images/product_images
, and the directory contains the images that your app uses.
Task 3: Create a Mutable Array Data Provider in Parent Container 1
-
Open the Oracle JET Cookbook and navigate to the Cookbook Home page. Click Framework, then Data Provider, and then Mutable Array Data Provider. In the Cookbook toolbar, click API Doc.
-
In the API documentation, read about the MutableArrayDataProvider class. Note how you can use the optional argument
keyAttributes
to create an instance of aMutableArrayDataProvider
based on the key attribute of the passed-in data object. -
If you have not already done so in a previous learning path, download the
store_data.json
file to theJET-Virtual-DOM-app/src/components
directory. -
Navigate to the
JET-Virtual-DOM-app/src/components
directory and open theParentContainer1.tsx
file in an editor. -
At the top of the file, add
import
statements for theMutableArrayDataProvider
class and the JSON data in thestore_data.json
file.import MutableArrayDataProvider = require('ojs/ojmutablearraydataprovider'); import * as storeData from 'text!./store_data.json';
-
Create an
Activity
type alias.type Activity = { id: number; name: string; short_desc: string; };
-
Create an
activityDataProvider
instance of aMutableArrayDataProvider
.const activityDataProvider = new MutableArrayDataProvider<Activity['id'], Activity>( JSON.parse(storeData), { keyAttributes: 'id', } );
The
keyAttributes
property of theMutableArrayDataProvider
class populates an array based on theid
attribute of the parent object. Theid
attribute is the key attribute for parent and child data objects in the JSON document. -
In the
ParentContainer1
function, add adata
attribute to theActivityContainer
element that passes the data provider object to the Activity Container component usingprops
.return ( <div id="parentContainer1" class="oj-flex oj-flex-init oj-panel oj-bg-warning-20"> <ActivityContainer data={activityDataProvider} /> <ParentContainer2 /> </div> ); };
Your code should look similar to
parent-container1-1-tsx.txt
.
Task 4: Create the Activity Container’s List View Component
Create an Oracle JET List View component in the Activity Container component to display multiple data items for each list row from the data provider object passed down via props
from the ParentContainer1
component. To instantiate the contents of the list row data items at runtime, the List View component uses an HTML template
element with a slot
attribute as a placeholder.
-
Navigate to the
JET-Virtual-DOM-app/src/components/Activity
directory and open theActivityContainer.tsx
file in an editor. -
Add the following import statements for the Oracle JET List View component and
MutableArrayDataProvider
class to the top of the file.import 'ojs/ojlistview'; import { ojListView } from 'ojs/ojlistview'; import MutableArrayDataProvider = require('ojs/ojmutablearraydataprovider');
-
Create
Props
andActivity
type aliases.type Props = { data?: MutableArrayDataProvider<Activity['id'], Activity>; value?: string; }; type Activity = { id: number; name: string; short_desc: string; };
-
Find the
div
element withid="activitiesContainer"
, and delete theoj-sm-only-text-align-end
helper class to prevent realignment of the Activity Container’s contents. -
Create the
listItemRenderer
function that renders each list item and includesspan
anddiv
elements to populate the List View component’s rows with a background image, name, and short description.Also define an
image
variable to ensure that the virtual DOM app can locate the product images in the app’sstyles
directory.const listItemRenderer = (item: ojListView.ItemTemplateContext) => { let image = item.data.image.replace('css', 'styles'); return ( <div class="oj-flex no-wrap"> <span class="demo-thumbnail oj-flex-item" style={'background-image:url(' + image + ')'}></span> <div class="demo-content oj-flex-item"> <div> <strong>{item.data.name}</strong> </div> <span class="demo-metadata">{item.data.short_desc}</span> </div> </div> ); };
-
Underneath the
listItemRenderer
function, add the followingListViewProps
type alias and two variables to define the gridline and scroll properties for the List View component.type ListViewProps = ComponentProps<'oj-list-view'>; const gridlinesItemVisible: ListViewProps['gridlines'] = { item: 'visible' }; const scrollPolicyOpts: ListViewProps['scrollPolicyOptions'] = { fetchSize: 5 };
-
Add
props
to theActivityContainer
function definition.const ActivityContainer = (props: Props) => {
-
Inside the
ActivityContainer
function’sreturn
statement, delete thediv
element whereid="activitiesItems
and the list inside of it. Replace them with the following List View component, where the data provider object is accessed viaprops
in the List View’sdata
attribute.<h3 id="activitiesHeader">Activities</h3> <oj-list-view id="activitiesList" class="item-display" aria-labelledby="activitiesHeader" data={props.data} gridlines={gridlinesItemVisible} selectionMode="single" scrollPolicy="loadMoreOnScroll" scrollPolicyOptions={scrollPolicyOpts}> </oj-list-view>
-
Within the List View component, add a
template
element with aslot
attribute to render each item in the list.. . . <template slot="itemTemplate" render={listItemRenderer}></template> </oj-list-view>
Save the file. Your code should look similar to
activity-container-tsx-1.txt
.
Task 5: Run the App and Debug the App’s Cascading Style Sheet
-
In the terminal window, navigate to the
JET_Virtual_DOM_app
directory and run the app.npx ojet serve
Oracle JET tooling runs your virtual DOM app in your local web browser, where you can view the changes to the Activity Container component. The images displayed to the left of the activity names and short descriptions are only partially displayed because they are too large.
If you resize your browser and still don’t see a page similar to this sample screenshot, confirm that you extracted the app images into the
images/product_images
directory. -
In your Google Chrome browser, right-click the partial image for the Soccer activity and select Inspect. The browser displays the Chrome Developer Tools (DevTools) where you can inspect the HTML page source.
-
In the Elements panel, the
span
element whereclass="demo-thumbnail oj-flex-item"
should be selected. In the Styles panel, locate the highlightedelement.style background-image
style. Right-click the URL for thejrsoccerball.jpg
image, and select Reveal in Sources panel. -
The Chrome DevTools Sources panel shows that the image is 300 x 300 pixels, and therefore the size is too large to be displayed in the list view. To render a thumbnail-sized image from a full-sized image, you can define a Cascading Style Sheets (CSS) style class.
-
Close the Chrome DevTools window and return to the app in the browser.
Task 6: Fix the CSS and View the Activities List Changes
-
Navigate to the
JET-Virtual-DOM-app/src/styles
directory and open theapp.css
file in an editor. -
Define the properties for the
.demo-thumbnail
class selector. In the list of properties, set abackground-repeat
property tono-repeat
to show the image only once..demo-thumbnail { border-radius: 5px; background-size: 40px 40px; background-repeat: no-repeat; min-width: 40px; min-height: 40px; max-width: 40px; margin-right: 10px; }
In the
listItemRenderer
function in theActivityContainer.tsx
file, thespan
element references the class selector to format the Activities list images as thumbnails by setting thedemo-thumbnail
CSS style on theclass
attribute. -
Save and close the
app.css
file. Your file should look similar to app-css.txt. -
Return to the browser and view the updates.
The app displays the Activities list with thumbnail images.
-
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.
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.
Create the master view in an Oracle JET virtual DOM app
F72825-04
November 2024