dojo combobox with data loaded dynamically from JSON

I am facing an issue with populating a Combobox dynamically using a jsonRest from a cross-origin request.

While I have managed to do it statically (if that's the correct term), I am struggling to implement it for multiple cases. This is just a small part of a larger website with five Comboboxes.

Below is the code snippet:

require([
"dojo/_base/array",
"dojo/store/Memory",
"dojo/store/JsonRest",  
"dijit/form/ComboBox", 
"dojo/store/Cache",
"dojo/store/Observable",
"dijit/form/Textarea",
"dojo/domReady!"
 ], 

function(array, Memory, JsonRest, ComboBox, Cache, Observable, Textarea){

var myArray = new Array;
var myStore = new Observable (new Cache (new JsonRest ({
            target: “URL / target”,
            idProperty: "WA",
            headers: { "X-Requested-With": "" }
        }), new Memory ()));
var myTextarea = new Textarea ({
    name: "myarea",
    style: "width:200px;"
}, "myarea");
myStore.query().then(function(response){
            });

store = new Memory({data: myArray});        

var comboBoxWA = new ComboBox({
    id: "comboWA",
    name: "WA",
    value: "",
    store: store, 
    searchAttr: "WA"
}, "comboWA");

myStore.query().then(function(response){
    dojo.forEach( response, function( obj ) {
        for (var p in obj) {
            if(p=="WA"){

//I'm currently stuck at this point where I can't change the “WA” in myArray.push to a global Variable.

                myArray.push({"WA" : obj[p]});  
                console.debug(myArray.toSource()); 
      }}
      });
        });
 });

The JSON response appears like this [Object { WA=‘'WA_30_14"}, Object { WA="WA_30_12"} , Object { WA="WA_30_10"}, Object { WA="WA_30_16"},…]

If anyone has any ideas or a simple example to share, it would be greatly appreciated. Thanks, Georg

Answer №1

If you're looking for another approach, consider the following method. Instead of using "myArray.push({"WA" : obj[p]});", try using "myArray.push({some_global_variable: obj[p]});". Here's how you can do it:
1. Start by creating an empty local object.
2. Assign the property to the local variable using array syntax.
3. Pass the local variable as a parameter to the push method.

var localobj = {}; // Step 1
localobj[global_var] = obj[p]; // Step 2
myArray.push(localobj); // Step 3

Before utilizing the push() function, you may want to verify the value of the global variable by executing the following code:

console.log("My global variable: " + global_var);

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

Regular Expression: locate the occurrence of "//" followed by any character other than a space

I am looking to add a space by replacing the target with the code "// ". That's all you need to know from the title. My attempts so far have looked like this: \/\/\b(?! ) However, this approach does not catch strings like "//$..." ...

Twilio Group MMS feature experiencing technical difficulties

I currently have a Twilio Trial account with an active number that supports SMS/MMS. My goal is to use this number for group MMS chats, but I am facing some challenges. After following a tutorial on Tut, I attempted to create a basic test using the provid ...

Stop images from being stored in the database instead of text characters

Using a proxy, I attempt to parse some HTML. One of the elements is retrieved using jQuery: var site = 'http://www.kartabu.com/pl/index.php?filter=random' var url = 'http://localhost/taboo.blue-world.pl/admin/proxy.php?url=' + encodeUR ...

JavaScript Regex: Removing all characters that are not numbers

There have been several inquiries about this particular question, such as this one on Stack Overflow. Despite my efforts to replicate the solution and research regex, I can't seem to get it to work: $("#button").click(function () { new_number = $ ...

Display directional arrow on Ext.grid when the page is initially loaded

Displaying a grid with the product ID is our current setup. While the data is sorted according to the product ID, the sort arrow does not display upon page load. I have observed that clicking on the column reveals the arrow. How can we ensure that the so ...

Creating an Interactive Table Using HTML, JavaScript, and PHP?

After reviewing this project, I am looking to customize the js function in relation to my own table. The key focus is on the following code: $("#employee_table tr:last").after("<tr id='row"+$rowno+"'><td><input type='text&apo ...

Express Module Employs Promises for Returns

I have a JavaScript file for elasticsearch (could be any other database as well) that performs a simple query and uses a promise to return the data. I am using this module in my Express server (server.js) with the hope of retrieving the data, as I ultimat ...

Exploring the benefits of utilizing TypeScript's async await feature within the Node

I've encountered a challenge trying to accomplish the following tasks simultaneously: Developing in Node.js Coding in TypeScript Implementing async await Facilitating debugging Background: In my TypeScript-based Node.js project, I am incorporating ...

PHP code to paginate and format content for Point of Sale printers

Currently, I am working on a project that requires the use of a POS printer to generate receipts. The client has recently requested a new feature where the paper will be automatically cut after the receipt is printed. This way, if the client needs to pri ...

Unable to retrieve a state property within a Vue template

Embarking on my Vue journey, I've been immersing myself in online videos to grasp the essence of this framework. One intriguing observation that has piqued my curiosity is the difference in behavior when I switch from a template to a render function i ...

Steps to release a react application as an npm package

Hey there! I have a set of JavaScript files named auth.js, cookies.js, hooks.js, product.js, and index.js. My plan is to package them using npm for publishing. In my index.js file, I am exporting all the other files with the syntax: export * from './ ...

The functionality of ZoneAwarePromise has been modified within the Meteor framework

After updating to the latest Angular 2.0.1 release on Meteor 1.4.1.1, I'm facing an error that says: Zone.js has detected that ZoneAwarePromise (window|global).Promise has been overwritten I've attempted running meteor update and meteor reset, b ...

Display or conceal certain HTML form elements based on the selection made in the previous form element

I need assistance with a function that can dynamically show or hide certain HTML form elements based on the user's previous selection using JavaScript. For example, if a user selects "Bleached" from the Dyingtype drop-down menu, there is no need to di ...

children blocking clicks on their parents' divs

I previously asked a question about a parent div not responding to click events because its children were blocking it. Unfortunately, I couldn't recreate the issue without sharing a lot of code, which I didn't want to do. However, since I am stil ...

Discovering the size of an attribute in an object using Angular

I am working with a JSON object named "programs" that contains an array of key/value pairs. My task is to determine the count of objects within this array that have the key/value pair "status": "Review". In this case, there are 4 objects in total and 2 o ...

What is the best way to add or delete data when specific radio buttons are chosen?

Hey there, I'm facing an issue where the data is being appended regardless of which radio button is selected. Can someone help me with a solution on how to properly add and remove data based on the selected radio button? $( document ).ready(functio ...

The Discord OAuth2 bot fails to assign roles to authenticated users

While working on OAuth2 login for my website, I encountered an issue. After a user successfully logs in through OAuth2, the bot should assign a role to them on my Discord server. However, when I tested the login process myself, the bot returned an error me ...

ClickListener for RecycleView

Looking to customize a RecycleView with model images and text layout? Everything is set up, but I need help implementing the onClickListener for clicking on items. Can anyone assist? FeedItem.java public class FeedItem { private String title; private Str ...

Creating dynamic URL routes for a static website using Vue

I am facing a challenge with my static site as I am unable to add rewrites through htaccess. Currently, our site is built using Vue on top of static .html templates such as \example\index.html. When I want to create a subpage within this layout, ...

Unusual actions observed with that particular button

Currently, I am working on creating a pomodoro clock using Codepen. While I acknowledge that my code isn't flawless yet, I have encountered a peculiar behavior with the Start button. When I click on it once, the timer starts as expected. However, if I ...