Data bind a component in an Oracle JET web app
Introduction
This tutorial shows you how to use a local JSON document in your Oracle JavaScript Extension Toolkit (Oracle JET) web app.
Oracle JET is a development toolkit that provides a number of data providers classes to manage data from various sources. In the viewModel, you’ll populate an instance of MutableArrayDataProvider
with the data you read from a JSON file, then bind the MutableArrayDataProvider
instance to Oracle JET custom HTML elements in the view.
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 component to the JSON data objects.
Prerequisites
- A development environment set up to create Oracle JET web apps that includes an installation of Node.js
- Completion of the previous tutorial in this learning path, Add Components to an Oracle JET Web Application
- The store_data.json downloaded to the
JET_Web_Application/src/ts
directory
Task 1: Download the JSON Document and Examine the Data
- Download the
store_data.json
file to theJET_Web_Application/src/ts/
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 the ViewModel
You push data from the JSON file into an Oracle JET MutableArrayDataProvider
object.
-
Navigate to the
JET_Web_Application/src/ts/viewModels
directory and open thedashboard.ts
file in an editor. -
Import the data from the
store_data.json
file so that it is available in the ViewModel.import * as storeData from "text!../store_data.json";
-
Before the
DashboardViewModel
class, define a type alias,Activity
, that includes theid
field of typenumber
.... type Activity = { id: number; } class DashboardViewModel {
-
In the
DashboardViewModel
class, add a type for theactivityDataProvider
that you will create in the next step.class DashboardViewModel { . . . chartDataProvider: MutableArrayDataProvider<string, {}>; chartData: Array<Object>; activityDataProvider: MutableArrayDataProvider<Activity["id"], Activity>; . . .
-
In the
constructror()
method of theDashboardViewModel
class, create an instance ofMutableArrayDataProvider
(activityDataProvider
) and use theJSON.parse()
method to read in the JSON data.constructor() { this.val = ko.observable("pie"); ... this.activityDataProvider = new MutableArrayDataProvider< Activity["id"], Activity >(JSON.parse(storeData), { keyAttributes: "id", }); . . .
-
Import the Oracle JET List View component module.
import "ojs/ojlistview";
The
ojlistview
module supports the view in the next section of this tutorial. -
Save the
dashboard.ts
file. Yourdashboard.ts
file should look similar to final-data-bind-dashboard-ts.txt.
Task 3: Data Bind the List View Component in the View
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
variable populated in the viewModel to the data
attribute of the List View.
-
Navigate to the
JET_Web_Application/src/ts/views
directory and open thedashboard.html
file in an editor. -
In the
dashboard.html
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-hybrid-padding"> <h1>Product Information</h1> <div id="activitiesContainer"> </div> <div id="itemDetailsContainer"> <oj-label for="basicSelect">Select Chart:</oj-label> ... </div> </div>
-
Within the
activitiesContainer
div
element, add anoj-list-view
element with adata
attribute that uses a one-way binding to get the list data.<div class="oj-hybrid-padding"> <h1>Product Information</h1> <div id="activitiesContainer"> <oj-list-view id="activitiesList" aria-labelledby="activitiesHeader" data="[[activityDataProvider]]" gridlines.item="visible"> </oj-list-view> </div> <div id="itemDetailsContainer"> . . .
We specify the
visible
value for thegridlines
attribute so that a grid line appears between items in the list, and we include thearia-labelledby
attribute as an accessibility best practice. -
Within the
oj-list-view
element you created, add an HTMLtemplate
element with aslot
attribute to render each item in the list.<div class="oj-hybrid-padding"> <h1>Product Information</h1> <div id="activitiesContainer"> <oj-list-view id="activitiesList" aria-labelledby="activitiesHeader" data="[[activityDataProvider]]" gridlines.item="visible"> <template slot="itemTemplate"> </template> </oj-list-view> </div> </div>
-
Within the template element, add another
div
element that contains anoj-bind-text
custom HTML element with avalue
attribute that uses a one-way binding to get the list data from the current data object.<div class="oj-hybrid-padding"> <h1>Product Information</h1> <div id="activitiesContainer"> <oj-list-view id="activitiesList" aria-labelledby="activitiesHeader" data="[[activityDataProvider]]" gridlines.item="visible"> <template slot="itemTemplate"> <div> <strong> <oj-bind-text value="[[$current.data.name]]"></oj-bind-text> </strong> </div> </template> </oj-list-view> </div> <div . . .
-
Add an HTML
h3
element to supply a header for the list of activities.<div class="oj-hybrid-padding"> <h1>Product Information</h1> <div id="activitiesContainer"> <h3 id="activitiesHeader">Activities</h3> <oj-list-view id="activitiesList" . . .
-
In the
itemDetailsContainer
div
, add anh3
element and anoj-label
element to supply a header for the chart.. . . <div id="itemDetailsContainer"> <h3>Item Details</h3> <oj-label for="basicSelect">Select Chart:</oj-label> . . . </div>
-
Save the
dashboard.html
file. Yourdashboard.html
file should look similar to final-data-bind-dashboard-html.txt.
Task 4: Run the Web App
-
In the terminal window, change to the
JET_Web_Application
directory and run the app.npx ojet serve
Your browser displays the list of activities visible in the Dashboard tab of your web app.
-
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. -
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 web app
F10959-06
February 2024
Copyright © 2022, 2024, Oracle and/or its affiliates.