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

Connect parent-child models with checkboxes

Currently, I am working on an application where I need to link checkboxes for a role-based menu. The challenge lies in dealing with parent-child checkboxes and ensuring that the values of checkboxes are checked based on user input 1, 1.1, 1.2, 2, 3. Despi ...

What sets apart initializing an Express app using "app = new Express()" compared to "app = express()"?

Throughout my experience, express apps have always been initialized like this: var express = require('express') var app = express() However, today I came across an example where a new operator was used: var Express = require('express&apos ...

In React, the clearInterval() function does not effectively clear intervals

Currently, I am implementing the following code: startInterval = () => { this.interval = setInterval(this.intervalFunction, 10000) } stopInterval = () => { clearInterval(this.interval) } However, a problem arises when I invoke the stopInte ...

Issue with Facebook like button not consistently loading on the website

Getting ready to release my new iOS app that creates customized mp3s and distributes them through a CDN-hosted webpage. Check it out at . I've integrated the XFBML code from http://developers.facebook.com/docs/reference/plugins/like/ because it' ...

transferring data between react components

I am currently working on breaking down code into smaller components, starting with the snippet below from Rows.jsx. I am struggling to grasp how props are passed between parent and child components. Despite reading several guides and articles, I remain un ...

Is there a way to create a timed fadeIn and fadeOut effect for a div using HTML and CSS?

I have a single div that initially displays text, and I want it to fade out after a specific time so that another div will then fade in. However, my attempt at coding this transition is not producing the desired result: $(function() { $(".preloa ...

String type that may or may not be unwrapped

I've set up a PHP page on my webserver, grabmapinfo.php, to interact with a MySQL database. When I access the page, it returns the following data: [{"companyname":"Brunos Burgers","companyphone":"7745632382","companytown":"858 Western Ave, Lynn, MA 0 ...

I'm encountering difficulties in utilizing Ajax to upload images

I have been attempting to utilize ajax and bootstrap (modal) to add a new product. However, when I click the save changes button, I encounter an issue where all fields return an error message stating 'Undefined index'. Below is the code snippet ...

Encountering: Unable to break down the property 'DynamicServerError' of 'serverHooks' as it does not have a defined value

An error has arisen in a Nextjs app with TypeScript, specifically in the line of my react component which can be found here. This is my inaugural package creation and after several trials, I managed to test it successfully in a similar vite and TypeScript ...

The React Hook useEffect is missing a dependency: 'handleLogout'. Make sure to either add it to the dependency array or remove it from the useEffect hook

import { useState, useEffect } from "react"; import LoginModal from "./LoginModal"; import { NavLink, useLocation, useNavigate } from "react-router-dom"; import { useDispatch } from "react-redux"; import { userLogout ...

Failure of jQuery ajax success callback to trigger

I have been using jQuery's $.ajax method in my code to fetch data from a JSONP-compatible web service. It seems like everything is working fine as I am able to receive the response and display the data in my HTML. However, I am facing an issue where t ...

What is the best way to remove text using javascript?

Hey everyone! I am new to coding in javascript and I'm hoping for some help with stripping text. I have a string that is formatted like this: <iframe src="http://embed.videokoo.com/61YVek?client_file_id=390856&width=720&height=480" style=" ...

Using JavaScript to replace a radio button with the term "selected"

I am currently in the process of developing a quiz that is powered by jQuery and possibly JSON, with data being stored in a database. Everything is functioning correctly at this point, but I would like to enhance the user interface by hiding the radio butt ...

How can I center the window vertically based on the middle of an element?

Hey guys, I need some help with my code. I've been trying to create a button that will shift the window's Y position to center the "Box - 5" div vertically through an onclick event. Basically, I want to make sure the "Box - 5" div is positioned i ...

What steps can I take to resolve the spawn unknown error when using npx create-next-app in Visual Studio Code (VSCode)?

Every time I attempt to create a new app using either vscode or cmd, I encounter a spawn unknown error. The error message says "Aborting installation. Unexpected error. Please report it as a bug: Error: spawn UNKNOWN". Unfortunately, the installation abo ...

Unable to transfer files, encountering issues with PHP and AngularJS integration

I am currently working on uploading files using both AngularJS and PHP. For the AngularJS part, I am utilizing angular-file-upload from https://github.com/danialfarid/angular-file-upload. I suspect that the issue lies in my PHP code, as it fails to move ...

What methods are available to load sections of a complex SVG shape asynchronously?

I'm currently in the process of creating a website with a geographic map built using SVG, pulling data from OpenStreetMap. Due to its large size and potential for transformations such as zooming and moving, only a portion of it will be visible on the ...

Tips for concealing the values within a selected dropdown list using jQuery

Hello, I'm currently working on a jQuery application that involves a dropdown list box and a gridview. The first column of the gridview has checkboxes with a check all button at the top. My goal is to disable corresponding values in the dropdown list ...

Ensuring proper functionality of JQModal when displayed above an iframe with the usage of ?wmode=

Here's an interesting one... I'm currently working on a site with JQModal and everything seems to be functioning properly except for the fact that the iframe appears on top of the modal. An easy fix is to append ?wmode=opaque at the end of the ...

Combine several items to form a single entity

When I have two objects returned from the database, my goal is to combine them into one object. Here are the examples: { "id": 4, "first_name": "s", "last_name": "a", ...