Issue with populating JsonRest store in Dojo Enhanced Datagrid

My data grid is not loading the information from the store

The response I get from my REST call looks like this: {"onlineUsers":[],"offlineUsers":["123.456.7.890:8080"]}

This is what my code looks like:
require([  "dojo/store/JsonRest",
        "dojo/store/Memory",
        "dojo/store/Cache",
        "dojox/grid/EnhancedGrid",
        "dojox/grid/enhanced/plugins/Pagination",
        "dojo/data/ObjectStore",
        "dojo/dom-attr",
        "dojo/domReady!"],
        function(JsonRest, Memory, Cache, EnhancedGrid, Pagination, ObjectStore, domAttr){

        var store = new dojo.store.JsonRest({target:"/Url/rest/onlineusers/all"});

        var dataStore = new dojo.data.ObjectStore({objectStore: store});

        var grid = new EnhancedGrid({
            id: 'grid',
            store: dataStore,
            structure: [
          {name: "Offline Users", field: "offlineUsers", width: "100px"}
        ]
        }, "gridDiv");

            grid.startup();
    });

In the logs I see this message: [object Object]

I also notice this warning in the console: [18:12:57.822] Use of getAttributeNode() is deprecated. Use getAttribute() instead. @ http://ajax.googleapis.com/ajax/libs/dojo/1.8.4/dojo/dojo.js:146

What could be causing this issue?

Answer №1

It seems like the issue lies in the way your data structure is set up. Are you trying to display a grid where each host or address is listed as a separate row?

The correct data structure for a store should be an array of objects. In your current setup, you have defined an object with arrays of strings, which does not match the required structure. You can find more information on the formatting specifications for your REST service in the RFC 2616 document.

You have a few different options:

  1. Adjust your RESTful webservice to return data in a compatible format for Dojo
  2. Create your own custom store implementation
  3. Parse your data on the client side by manually sending an AJAX request and mapping the object

If you choose the third option, your code might look something like this:

var data = {"onlineUsers":[],"offlineUsers":["123.456.7.890:8080"]}; // Fetch data with AJAX
var offlineUsers = array.map(data.offlineUsers, function(user) {
    return {
        host: user  
    };
});
var store = new Memory({ data: offlineUsers });
var dataStore = new ObjectStore({objectStore: store});

To update the structure property of your grid, use:

[
    {name: "Offline Users", field: "host", width: "100px"}
]

I have also created a JSFiddle demonstrating this example, which you can view here.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Encountering a random error: org.json.JSONException: Cannot convert Value <!DOCTYPE of type java.lang.String to JSONObject

Help needed with JsonException occurring sporadically. Can anyone provide a solution for the following code snippet? public int getResult(String name, double lat, double lon) { try { HttpClient httpclient = new DefaultHttpClient(); ...

Accessing JSON output to retrieve data from an Asterix file

Looking to convert a radar data file, provided in JSON format, into a usable DataFrame structure. The initial three lines of the file present as follows: {"id":1,"length":43,"crc":"D81B2DB5","timestamp":16 ...

Slider with a progress bar

Have you ever wondered if it's possible to create a progress bar using an input range? Imagine someone entering a number and the bar moves to the right, changing its background color on the left side of the thumb based on the size of the number. Check ...

Troubleshooting the JSONArray issue: Resolving difficulties in retrieving data on Android Studio

I've been encountering difficulties with JSON in Android Studio despite watching and reading examples. I'm still struggling to figure out how to pass data from a database into the "Movie" class as demonstrated in Java code. The error log indicat ...

Develop a dynamic animation using gradient and opacity properties

I'm still getting the hang of HTML, JavaScript, and CSS but I recently made some changes to someone else's code to animate a gradient. The original code used background: rgb, but I switched it to background: rgba. It seems to be working fine, but ...

How can we dynamically reference past elements in an array without using indices like "i-1", "i-2", and so on?

function play_tune(melody) { let played_notes = []; let durations = []; let positions = []; let wait_time = 0; let time_passed = 0; console.log("Melody initiation.") for (let key in melody) { played_notes.push(melody[key].note); dur ...

Detecting Null Values

Recently, I encountered an issue with a Javascript function I created. Despite my efforts, I was not getting the desired result. The purpose of the function is to check various variables for null values and replace them with blank spaces when necessary. He ...

When using AutoComplete in MUI, I encountered an issue while attempting to assign a default value to the checkbox from an API. Instead of achieving the desired result, I received an error stating "(inter

Within this snippet, I am seeking to retrieve a default value from the API to populate a checkbox initially. I have employed the Material-UI Autocomplete component, which includes a defaultValue prop. Despite my efforts to utilize this prop, I am encounter ...

Please provide the parameter in JSON format when using Sails

When working with controllers and other parts of the application where a req object is present, I typically access request parameters using req.params('username'). While this method works well for standard POSTed data, I am looking to have my API ...

ReactJs allows for fluid scroll animations that persist as long as the mouse is clicked or a button

Just some background information: I'm aiming to replicate the scrolling effect seen in Etsy's product image thumbnails carousel. Essentially, when you hover over the top part of the div, it automatically scrolls down until the last image is reve ...

Submitting modal form information using AJAX to PHP

As a novice in the realm of web programming, I find myself seeking some guidance to untangle a riddle. Regrettably, my grasp of the basics still leaves much to be desired. Within my main page, view.adminsettings.php, I've designated a Navigation DIV ...

Exploring Mathematical Functions Using React State Management

I'm trying to perform a mathematical operation with a value stored in a state that is extracted from an input. It seems like the input saves a numerical string in the state, but I'm struggling to convert it to an integer. Even using parseInt does ...

Javascript unable to access SVG element content on mobile Safari

My approach to reusing certain SVG objects involves defining symbols in an SVG element at the top of my DOM. When I want to display an SVG, I typically use: <svg><use xlink:href="#symbol-identifier" /></svg> For animating SVG's, I ...

The contents of my script.js file do not align with the code in the mail

I find myself in a bit of a pickle and could really use some assistance. I recently took over for someone and have been struggling to get my form to send via ajax POST. Despite receiving a 200 OK response, the mail.php file I've been using is not send ...

What could be the reason for the failure of Angular Material Table 2 selection model?

A Question about Angular Mat Table 2 Selection Model Why does the selection model in Angular Mat Table 2 fail when using a duplicate object with its select() or toggle() methods? Sharing Debugging Insights : Delve into my debugging process to understand ...

Struggling with Mootools selectors

Take a look at this HTML snippet: <div id="link-thumbs-list"> <img src="image.php?f=http://www.jetphotos.net/trans.gif&amp;h=72&amp;w=128" /> <img src="image.php?f=http://www.jetphotos.net/logo-nb.jpg&amp;h=72&amp;w=128" /&g ...

Obtain the contents of the table row td element within the specified div class without relying on

I am currently working with a basic table layout, shown below: <table> <tbody> <tr> <td>img</td> <td class="mediaTitle"><span class="media_title">Media Name Title</span>< ...

Difficulty with changing JSON keys in jq not functioning as expected

My jq key assignment isn't working correctly. I attempted to follow the advice provided in this post for my specific scenario. Using jq how can I replace the name of a key with something else In my case, I am trying to change id to item_id. jq &apos ...

Executing a click on a checkbox element in Python with Selenium

Background: Currently, I am in the process of developing a Python script that will automatically download listings from a specific website. I have successfully programmed the script to navigate the webpage, search for keywords, click on the search button, ...

Validation of forms in Angular using a pseudo submission method

On a webpage, there is a form with two buttons: one to calculate a price and the other to submit the form. Below is a basic example: <form novalidate name="formStep1"> <select ng-model="address" required> <option></option> ...