Please disable your adblock and script blockers to view this page

Search this blog

Thursday, 18 April 2019

Populate Oracle JET table on top of Oracle ADF BC using REST Web Service

 Previously I have posted about creating REST Web Service from ADF Application Module and in this post, I am going to use the same REST service for populating data in a JET table. Here we'll populate JET table on top of Oracle ADF BC (Business Components).

Here I am taking the same sample application that I have discussed in my first post Getting started with Oracle JET and JDeveloper

The URL of ADF BC based web service is http://127.0.0.1:7101/RestWebServApp-RESTWebService-context-root/rest/Jdev12.2.1/Department and it returns a list of Departments in nested JSON format



Let’s see the implementation part

Add an oj-table component in the dashboard.html page and define the required number of columns, this web service returns a list of Departments so I have taken 4 columns for Department Id, Department Name, Manager Id, and Location Id

  1. <div class="oj-hybrid-padding">
  2. <h1>Dashboard Content Area</h1>
  3. <div id="div1">
  4. <oj-table id='table' aria-label='Departments Table'
  5. data='[[datasource]]'
  6. selection-mode='{"row": "multiple", "column": "multiple"}'
  7. dnd='{"reorder": {"columns": "enabled"}}'
  8. scroll-policy='loadMoreOnScroll'
  9. scroll-policy-options='{"fetchSize": 10}'
  10. columns='[{"headerText": "Department Id",
  11. "field": "dept_id",
  12. "resizable": "enabled"},
  13. {"headerText": "Department Name",
  14. "field": "dept_name",
  15. "resizable": "enabled"},
  16. {"headerText": "Manager Id",
  17. "field": "mgr_id",
  18. "resizable": "enabled"},
  19. {"headerText": "Location Id",
  20. "field": "loc_id",
  21. "resizable": "enabled"}
  22. ]'
  23. style='width: 100%; height: 400px;'>
  24. </oj-table>
  25. </div>
  26. </div>

Now see the code of dashboard.js, Read comments to understand the javascript code, Here getJSON() method of jQuery is used to call REST Web Service as jQuery is a part of Oracle JET libraries.

  1. /*
  2. * Your dashboard ViewModel code goes here
  3. */
  4. define(['ojs/ojcore', 'knockout', 'jquery', 'ojs/ojinputtext', 'ojs/ojtable', 'ojs/ojarraytabledatasource'],
  5. function (oj, ko, $) {
  6. function DashboardViewModel() {
  7. var self = this;
  8. //Define a Knockout observable array and name it tabData
  9. self.tabData = ko.observableArray();
  10. //Use jQuery method to call REST Web Service
  11. $.getJSON("http://127.0.0.1:7101/RestWebServApp-RESTWebService-context-root/rest/Jdev12.2.1/Department").
  12. then(function (departments) {
  13. //JSON response is nested that's why 'items' is used to access records
  14. $.each(departments.items, function () {
  15. //Push data in Observable array
  16. self.tabData.push( {
  17. dept_id : this.DepartmentId,
  18. dept_name : this.DepartmentName,
  19. mgr_id : this.ManagerId,
  20. loc_id : this.LocationId
  21. });
  22. });
  23. });
  24. //Pass observable array in utility class to show data in table
  25. self.datasource = new oj.ArrayTableDataSource(self.tabData,
  26. {
  27. idAttribute : 'dept_id'
  28. });
  29. .
  30. .
  31. .

tabData is a knockout observable array that is defined to hold returned value and you can see that
self.tabData.push is used to push web service data into this array and at last a JET utility class
ArrayTableDataSource is used to convert this observable array into tabular format data.

Now run the application and you can see that a JET table is populated with Departments data



Cheers 🙂 Happy Learning

Wednesday, 10 April 2019

Populate Oracle JET table from JSON based REST Web Service

 Before starting with this post first go through my previous post about using JET with JDeveloper. Here I am using the same JET nav drawer template application. In this post, we'll see how to consume JSON based REST Web Service and populate data in the Oracle JET table (oj-table) component

We have the URL of REST Web Service - http://dummy.restapiexample.com/api/v1/employees, it returns a list of employees and their details.

Let's see the implementation part

Add an oj-table component in the dashboard.html page and define the required number of columns, this web service returns multiple details of an Employee so I have taken 4 columns for Id, Name, Salary, and Age

  1. <div class="oj-hybrid-padding">
  2. <h1>Dashboard Content Area</h1>
  3. <div id="div1">
  4. <oj-table id='table' aria-label='Departments Table'
  5. data='[[datasource]]'
  6. selection-mode='{"row": "multiple", "column": "multiple"}'
  7. dnd='{"reorder": {"columns": "enabled"}}'
  8. scroll-policy='loadMoreOnScroll'
  9. scroll-policy-options='{"fetchSize": 10}'
  10. columns='[{"headerText": "ID",
  11. "field": "emp_id",
  12. "resizable": "enabled"},
  13. {"headerText": "Name",
  14. "field": "emp_name",
  15. "resizable": "enabled"},
  16. {"headerText": "Salary",
  17. "field": "emp_sal",
  18. "resizable": "enabled"},
  19. {"headerText": "Age",
  20. "field": "emp_age",
  21. "resizable": "enabled"}
  22. ]'
  23. style='width: 100%; height: 400px;'>
  24. </oj-table>
  25. </div>
  26. </div>

Now see the code of dashboard.js, Read comments to understand the javascript code, Here getJSON() method of jQuery is used to call REST Web Service as jQuery is a part of Oracle JET libraries.

  1. /*
  2. * Your dashboard ViewModel code goes here
  3. */
  4. define(['ojs/ojcore', 'knockout', 'jquery', 'ojs/ojinputtext', 'ojs/ojtable', 'ojs/ojarraytabledatasource'], function (oj, ko, $) {
  5. function DashboardViewModel() {
  6. var self = this;
  7. //Define a Knockout observable array and name it tabData
  8. self.tabData = ko.observableArray();
  9. //Use jQuery method to call REST Web Service
  10. $.getJSON("http://dummy.restapiexample.com/api/v1/employees").then(function (employees) {
  11. $.each(employees, function () {
  12. //Push data in Observable array
  13. self.tabData.push( {
  14. emp_id : this.id,
  15. emp_name : this.employee_name,
  16. emp_sal : this.employee_salary,
  17. emp_age : this.employee_age
  18. });
  19. });
  20. });
  21. //Pass observable array in utility class to show data in table
  22. self.datasource = new oj.ArrayTableDataSource(self.tabData,
  23. {
  24. idAttribute : 'emp_id'
  25. });
  26. .
  27. .
  28. .

tabData is a knockout observable array that is defined to hold returned value and you can see that self.tabData.push is used to push web service data into this array and at last a JET utility class ArrayTableDataSource is used to convert this observable array in tabular format data.

Here id, employee_name, employee_salary, employee_age are the attribute's names returned by Web Service in JSON format.

Now right click on the index.html file in JDeveloper and choose run



and see Oracle JET table in the browser, Data is populated from Web Service. In the same way, you can try some other Web Service URLs.



All Done :)

Cheers 🙂 Happy Learning

Read more about Oracle JET Components