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.zipdownloaded to theJET-Virtual-DOM-app/src/styles/imagesdirectory
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.zipasJET-Virtual-DOM-app.zip. Extract the contents to theJET-Virtual-DOM-appdirectory. -
In the terminal window, navigate to the
JET-Virtual-DOM-appdirectory, and restore the Oracle JET app.npm installYour 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
yto 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.zipfile to theJET-Virtual-DOM-app/src/styles/imagesdirectory. -
Extract the content of the zip file into a new
product_imagesdirectory in theimagesdirectory.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
keyAttributesto create an instance of aMutableArrayDataProviderbased 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.jsonfile to theJET-Virtual-DOM-app/src/componentsdirectory. -
Navigate to the
JET-Virtual-DOM-app/src/componentsdirectory and open theParentContainer1.tsxfile in an editor. -
At the top of the file, add
importstatements for theMutableArrayDataProviderclass and the JSON data in thestore_data.jsonfile.import MutableArrayDataProvider = require('ojs/ojmutablearraydataprovider'); import * as storeData from 'text!./store_data.json'; -
Create an
Activitytype alias.type Activity = { id: number; name: string; short_desc: string; }; -
Create an
activityDataProviderinstance of aMutableArrayDataProvider.const activityDataProvider = new MutableArrayDataProvider<Activity['id'], Activity>( JSON.parse(storeData), { keyAttributes: 'id', } );The
keyAttributesproperty of theMutableArrayDataProviderclass populates an array based on theidattribute of the parent object. Theidattribute is the key attribute for parent and child data objects in the JSON document. -
In the
ParentContainer1function, add adataattribute to theActivityContainerelement 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/Activitydirectory and open theActivityContainer.tsxfile in an editor. -
Add the following import statements for the Oracle JET List View component and
MutableArrayDataProviderclass to the top of the file.import 'ojs/ojlistview'; import { ojListView } from 'ojs/ojlistview'; import MutableArrayDataProvider = require('ojs/ojmutablearraydataprovider'); -
Create
PropsandActivitytype aliases.type Props = { data?: MutableArrayDataProvider<Activity['id'], Activity>; value?: string; }; type Activity = { id: number; name: string; short_desc: string; }; -
Find the
divelement withid="activitiesContainer", and delete theoj-sm-only-text-align-endhelper class to prevent realignment of the Activity Container’s contents. -
Create the
listItemRendererfunction that renders each list item and includesspananddivelements to populate the List View component’s rows with a background image, name, and short description.Also define an
imagevariable to ensure that the virtual DOM app can locate the product images in the app’sstylesdirectory.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
listItemRendererfunction, add the followingListViewPropstype 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
propsto theActivityContainerfunction definition.const ActivityContainer = (props: Props) => { -
Inside the
ActivityContainerfunction’sreturnstatement, delete thedivelement whereid="activitiesItemsand the list inside of it. Replace them with the following List View component, where the data provider object is accessed viapropsin the List View’sdataattribute.<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
templateelement with aslotattribute 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_appdirectory and run the app.npx ojet serveOracle 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_imagesdirectory.
-
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
spanelement whereclass="demo-thumbnail oj-flex-item"should be selected. In the Styles panel, locate the highlightedelement.style background-imagestyle. Right-click the URL for thejrsoccerball.jpgimage, 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/stylesdirectory and open theapp.cssfile in an editor. -
Define the properties for the
.demo-thumbnailclass selector. In the list of properties, set abackground-repeatproperty tono-repeatto 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
listItemRendererfunction in theActivityContainer.tsxfile, thespanelement references the class selector to format the Activities list images as thumbnails by setting thedemo-thumbnailCSS style on theclassattribute. -
Save and close the
app.cssfile. 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
yto exit the Oracle JET tooling batch job.
Next Step
Proceed to the next tutorial in this module.
This tutorial is part of the module Master Detail Views in Oracle JET.
- Create the Master View in an Oracle JET Virtual DOM App
- Create the Detail View in an Oracle JET Virtual DOM App
- Handle Selection Events in an Oracle JET Virtual DOM App
You can return to the virtual DOM learning path’s main page to access all the modules on building virtual DOM apps.
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-05