Create the master view in an Oracle JET web 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 master view in an Oracle JavaScript Extension Toolkit (Oracle JET) web app.
In the viewModel for the Oracle JET web app, 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. In the view for the Oracle JET web app, the data
attribute of Oracle JET custom HTML elements binds to the data through an instance of a MutableArrayDataProvider
object that is declared and populated in the app’s viewModel source code.
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 with that data.
Prerequisites
- A development environment set up to create Oracle JET web 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 Web Application on Different Screen Sizes
- (Option 2) If you haven’t completed the previous learning path in this series: The jet_web_application_temp.zip downloaded
- The product_images.zip downloaded to the
JET_Web_Application/src/css/images/
directory
Task 1: Download the Starter App
Skip this task if you’re continuing to work in an app that you created in the previous learning path.
-
Rename
jet_web_application_temp.zip
asJET_Web_Application.zip
. Extract the contents to theJET_Web_Application
folder. -
Navigate to the
JET_Web_Application
folder, and restore the Oracle JET web 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_web_application_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 web app again.
-
Download the
product_images.zip
file to theJET_Web_Application/src/css/images
directory. -
Extract the content of the zip file into a new
product_images
folder in theimages
folder.The path to the new folder is
JET_Web_Application/src/css/images/product_images
, and the folder contains the images that your app uses.
Task 3: Enhance the Activities List in the View
Modify the Oracle JET List View component to display multiple data items of the Activities array data source for each list row. To instantiate the contents of the list row data items at runtime, the Activities List View component uses an HTML template
element with a slot
attribute as a placeholder.
-
Navigate to the
JET_Web_Application/src/ts/views
directory and open thedashboard.html
file in an editor. -
Find the
div
element withid="activityItemsContainer"
, and delete theoj-sm-only-text-align-end
helper class to prevent realignment of the Activities list contents.<div id="activitiesContainer" class="oj-flex-item oj-bg-info-30 oj-md-4 oj-sm-12" ></div>
-
Find the
oj-list-view
custom HTML element whereid="activitiesList"
, and add aclass
attribute with the valueitem-display
.<oj-list-view id="activitiesList" class="item-display" aria-labelledby="activitiesHeader" data="[[activityDataProvider]]" gridlines.item="visible" ></oj-list-view>
To supply the List View data source, the
data
attribute binds theactivityDataProvider
observable as a one-way data binding. -
In the
oj-list-view
element whereid="activitiesList"
, find the template element whereslot="itemTemplate"
, and delete thediv
element and the containedoj-bind-text
element.<template slot="itemTemplate"> </template>
-
In the empty
template
element, add threediv
elements with code to populate the List View component rows with a background image, name, and short description.<template slot="itemTemplate"> <div class="oj-flex no-wrap"> <span class="demo-thumbnail oj-flex-item" :style.background-image="[[' url('+$current.data.image+')']]" ></span> <div class="demo-content oj-flex-item"> <div> <strong> <oj-bind-text value="[[$current.data.name]]"></oj-bind-text> </strong> </div> <span class="demo-metadata"> <oj-bind-text value="[[$current.data.short_desc]]"></oj-bind-text> </span> </div> </div> </template>
-
Save the
dashboard.html
file.Your file should look similar to final-master-list-dashboard-html.txt.
-
Navigate to the
JET_Web_Application/src/css
directory and open theapp.css
file. Here you can add app-specific CSS styles. Create an.item-display
style definition withwidth
,height
, andoverflow-x
properties to support the display of multiple data items in the list rows..item-display { width: 100%; height: 500px; overflow-x: hidden; }
Task 4: Review the Activities Data Provider in the ViewModel
To populate the Activities List View component, the viewModel creates an instance of the Activities data array by using an Oracle JET MutableArrayDataProvider
class. The JSON.parse
method parses the local JSON data store as an argument that returns the data.
-
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 argumentkeyAttributes
to create an instance of anMutableArrayDataProvider
class based on the key attribute of the passed-in data object. -
Navigate to the
JET_Web_Application/src/ts/viewModels
directory and open thedashboard.ts
file in an editor. Find the declaration ofthis.activityDataProvider
in the constructor method.this.activityDataProvider = new MutableArrayDataProvider< Activity["id"], Activity >(JSON.parse(storeData), { keyAttributes: "id", }); }
This creates the
activityDataProvider
object, an instance of theMutableArrayDataProvider
class. ThekeyAttributes
property ofMutableArrayDataProvider
populates the Activities 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. -
Close the
dashboard.ts
file without making any changes.Your file should look similar to final-master-list-dashboard-ts.txt.
Task 5: Run the Web App and Debug the App’s Cascading Style Sheet
-
In the terminal window, change to the
JET_Web_Application
directory and run the app.npx ojet serve
Oracle JET tooling runs your web app in a local web browser, where you can view the databound master list. The images displayed to the left of the Activities list activity names are only partially displayed because they’re 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
product_images
folder. -
In your Google Chrome browser, right-click the partial image for the Soccer activity and select Inspect. The browser displays developer tools for inspecting the HTML page source.
-
In the Elements panel, select the
span class="demo-thumbnail"
element. 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 the size is too large to be displayed in the Oracle JET List View component. To render a thumbnail-sized image from a full-sized image, you can define a Cascading Style Sheets (CSS) style class.
Description of the illustration inspect_image_url_contents.png
-
Close the developer tools window and return to the app in the browser.
Task 6: Fix the CSS and View the Master List
-
Navigate to the
JET_Web_Application/src/css
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. Also define ano-wrap
class selector with the propertyflex-wrap
set tonowrap
..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; } .no-wrap { flex-wrap: nowrap; }
In the
dashboard.html
file, thespan
element references the class selector to format the Activities list image as a thumbnail by setting thedemo-thumbnail
CSS style on theclass
attribute. In thespan
element, thebackground-image
CSS property sets the image based on the URL in theimage
property of the current Activity parent object.<span class="demo-thumbnail oj-flex-item" :style.background-image="[[' url('+$current.data.image+')']]" ></span>
-
Save and close the
app.css
file.Your file should look similar to app-css-final.txt.
-
In the web browser, reload the page.
The browser displays the databound master list with thumbnail-sized images.
-
Close the browser window or tab that displays your running web 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 web app
F11705-09
March 2024
Copyright © 2022, 2024, Oracle and/or its affiliates.