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/tsdirectory
Task 1: Download the JSON Document and Examine the Data
- Download the
store_data.jsonfile to theJET_Web_Application/src/ts/directory. - Open the
store_data.jsonfile in your web browser and view the data structure. Thestore_data.jsonfile 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/viewModelsdirectory and open thedashboard.tsfile in an editor. -
Import the data from the
store_data.jsonfile so that it is available in the ViewModel.import * as storeData from "text!../store_data.json"; -
Before the
DashboardViewModelclass, define a type alias,Activity, that includes theidfield of typenumber.... type Activity = { id: number; } class DashboardViewModel { -
In the
DashboardViewModelclass, add a type for theactivityDataProviderthat 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 theDashboardViewModelclass, 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
ojlistviewmodule supports the view in the next section of this tutorial. - Save the
dashboard.tsfile. Yourdashboard.tsfile 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/viewsdirectory and open thedashboard.htmlfile in an editor. -
In the
dashboard.htmlfile, add adivelement with anidattribute value ofactivitiesContainerabove theitemDetailsContainerdivelement 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
activitiesContainerdivelement, add anoj-list-viewelement with adataattribute 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
visiblevalue for thegridlinesattribute so that a grid line appears between items in the list, and we include thearia-labelledbyattribute as an accessibility best practice. -
Within the
oj-list-viewelement you created, add an HTMLtemplateelement with aslotattribute 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
divelement that contains anoj-bind-textcustom HTML element with avalueattribute 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
h3element 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
itemDetailsContainerdiv, add anh3element and anoj-labelelement 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.htmlfile. Yourdashboard.htmlfile 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_Applicationdirectory and run the app.npx ojet serveYour 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
yto exit the Oracle JET tooling batch job. - Leave the terminal window open for your next tutorial.
Next Step
Proceed to the next tutorial in this module.
This tutorial is part of the module Your First Oracle JET Web Application.
- Create an Oracle JET Web Application by Using a Starter Template
- Add Components to an Oracle JET Web Application
- Data Bind a Component in an Oracle JET Web Application
- Debug an Oracle JET Web Application
- Add Unit Tests to an Oracle JET Web Application
- Prepare to Deploy an Oracle JET Web Application
You can return to the learning path’s main page to access all the modules on building web 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.
Data bind a component in an Oracle JET web app
F10959-07