Purpose
Consume a simple OData service and display the result in a table

Main points

  • Build up the URL for the OData service

  • Use CopyText to receive the OData response (JSON format) in a GuiXT text variable

  • Call up a JavaScript routine via CallJS to transform the JSON format into GuiXT variables

  • Display the result as a table, using Table and Column statements

GuiXT Script

// Build up OData URL (here: OData Test service)
Set
V[url] _
  
"http://services.odata.org/V4/Northwind/Northwind.svc/Customers"

// Read Northwind customers via OData service
CopyText fromFile="&V[url]" toText="cust"

// Fill GuiXT variables via JavaScript
CallJS customercount = odata_customers

// Display customer table
Table (3,6) (15,101) title="Northwind Customers" -singlerowselection _
name="nwcustomers" rows="&V[customercount]" fixedcolumns=3

Column "Company" size=40 name="company"
Column "City" size=30 name="city"
Column "Country" size=20 name="country"

JavaScript

function odata_customers() {

  // OData response (JSON format)
  var s = guixt.getText("cust");

  // build JavaScript object from JSON
  var x = eval('(' + s + ')');

  for (var k = 0; k < x.value.length; k++)  
  {
    var c = x.value[k];
 
  var n = k + 1;

    guixt.Set(
"nwcustomers.cell.company." + n, c.CompanyName);
    guixt.Set(
"nwcustomers.cell.city." + n, c.City);
    guixt.Set(
"nwcustomers.cell.country." + n, c.Country);
  };

  // return customer count
  return
x.value.length;

}

Result

Components
InputAssistant + GuiXT Controls