Complicated JSON configuration paired with Dojo dropdown menus

I have a data structure that I'm struggling to manage efficiently. To tackle this issue, I've decided to leverage Dojo and utilize JSON. You can find the specific structure I'm referring to at the end of this message, so please take a look before proceeding further.

The challenge at hand involves having 3 Selects. The first Select will display the "first level" options (a, b, c), while the second one will show the "second level" choices (aa, ab, etc.) and so on.

Each Select (excluding the first one) should dynamically adjust its contents based on the selection made in the preceding Select.

For instance, if "a" is chosen in the first Select, only "aa, ab, ac" should be visible in the second Select. Subsequently, selecting "ac" in the second Select should reveal "aca, acb, acc".

It's important to note that there are scenarios where there isn't a "third level." In such cases, the third select should either remain hidden or be left empty if handling it becomes too complex.

Now, concerning references: Opting for "d" in the first Select should present "aa, bc, ca" as choices in the second Select. Following this, choosing "cc" in the second Select should display "cca, ccb, ccc" in the third Select. This linkage should not be static; variations in the "c -> cc" subtree should reflect changes in the "d -> cc" subtree.

Now onto the queries:

1) How can I implement this functionality using Dojo? While I would have no trouble doing so with Vanilla JS, incorporating object stores and similar elements has me stumped.

2) How do I integrate References into this intricate structure? I could handle them in a basic JSON scenario, but the complexity of this setup leaves me perplexed.

3) What approach should I take when presenting the structure? Should I represent it as a single JSON object or split it into multiple segments?

Thank you in advance!

a
    aa
        aaa
        aab
    ab
        aba
    ac
        aca
        acb
        acc
b
    ba
    bb
    bc
        bca
c
    ca
        caa
        cab
    cb
        cba
        cbb
    cc
        cca
        ccb
        ccc
d
    <ref to aa subtree>
    <ref to bc subtree>
    <ref to ca subtree>

Answer №1

If you want to represent it as an object, here's a simple example:

var myData = {
    a: {
        aa: ['aaa', 'aab'],
        ab: ['aba'],
        ac: ['aca', 'acb', 'acc']
    },
    b: {
        ba: null,
        bb: null,
        bc: ['bca']
    },
    c: {
        ca: ['caa', 'cab'],
        cb: ['cba', 'cbb'],
        cc: ['cca', 'ccb', 'ccc']
    },
    d: {}
};
myData.d.aa = myData.a.aa;
myData.d.bc = myData.b.bc;
myData.d.ca = myData.c.ca;

To implement this, you can start by creating a function that retrieves the labels of each level like so:

var getObjectData = function(object) {
    var data = [];
    for (var key in object) {
        if (object.hasOwnProperty(key)) {
            data.push({
                name: key,
                value: object[key]
            });
        }
    }
    return data;
};
var getData = function(obj) {
    var data = [];
    if (obj === undefined) {
        return getObjectData(myData);
    } else if (obj instanceof Array) {
        return obj.map(function(value) {
            return {
                name: value,
                value: null
            };
        });
    } else {
        return getObjectData(obj);
    }
    return data;
};

This function will provide the necessary data structure for your store, with labels and object structures for each level.

Afterwards, you can set up the following code:

registry.byId("level1").setStore(new ObjectStore({
     objectStore: new Memory({
        data: getData(),
        idProperty: 'name'
    })
}));
var getNextLevel = function(value) {
    var data = getData(this.store.objectStore.get(value).value);
    if (this.nextLevel) {
        if (data.length > 0) {
            domStyle.set(registry.byId(this.nextLevel).domNode, "display", "inline-table");
            registry.byId(this.nextLevel).setStore(new ObjectStore({
                objectStore: new Memory({
                    data: data,
                    idProperty: 'name'
                })
            }));
        } else {
            domStyle.set(registry.byId(this.nextLevel).domNode, "display", "none");   
        }
    }
};
registry.byId("level1").on("change", getNextLevel);
registry.byId("level2").on("change", getNextLevel);

With this setup, the data from each level will be fetched and populated when a selection is made, updating the store of the next level accordingly.

Remember, this is just a starting point and more initialization steps may be required. However, it offers a good framework for handling this scenario effectively.

You can view an example implementation here: http://jsfiddle.net/ekoronra/

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

"Exploring the world of jest.doMock: A guide to mocking

Here is the code snippet I am testing: ... import data from '../data/mock.json'; // function is async export const something = async () => { try { ... if (!data) { throw 'error is here!'; } ...

Mapping data using MapState is a useful technique that can help organize and access data

Is it possible to use ...mapstate to fetch data from the store instead of directly accessing it like this.$store.state.workermanage.staff.staff? Any suggestions are appreciated, thank you. persons: this.$store.state.workermanage.staff.staff ...

Converting a grayscale image matrix into an image using Node.js

I need help displaying an 8-bit grayscale image matrix (values between 0 and 255) from a C program on a website. Do I need to convert the image within the C program before displaying it? What is the best way to achieve this? ...

Error encountered: The fiber texture failed to load due to a component becoming suspended during the response to synchronous input

I'm encountering an issue while attempting to load a texture through the TextureLoader: const texture = useLoader(TextureLoader, '/textures/texture.png') The error message I receive from react is as follows: ERROR A component suspended w ...

What is the process for determining the estimated location of a queued event within a JavaScript Engine's event queue?

Imagine a multitude of events being created and added to the event queue of a Javascript engine. Are there any techniques or recommended practices in the industry to predict the order in which a specific event will be added to the queue? Any knowledge or ...

Efficiently loading data using lazy loading in jsTree

I have been facing a challenge with dynamically loading the nodes of a jtree upon expansion. The limited documentation I could find is located towards the end of this specific page. Several solutions involve creating nodes individually using a loop, simil ...

What happens when 'grid' property is undefined in modal?

I encountered an issue with my modal where I wanted to display pre-selected rows, but kept getting a 'cannot read 'grid' of undefined' error. The UI Grids are defined within the modal, and I have declared two Grid APIs with different na ...

Combining react-md with material-ui components: A step-by-step guide

Is it possible to combine react-md and material-ui components together? I am attempting to utilize components from both libraries, however they seem to conflict with each other's styles. Do you have any suggestions or solutions? ...

Issue: A file that has been uploaded completely ignores the req.on() method in NodeJS

I am currently using MEAN.IO to create a web application. Right now, I am working on implementing an image uploader feature. I have decided to use angular-file-upload for this purpose, and it seems to be functioning well. However, I am facing an issue on ...

Encountering a window undefined error while using React-leaflet

I'm currently working on creating a map using react-leaflet and server-side react. To tackle the "ReferenceError: window is not defined" issue, I've implemented a map component and experimented with utilizing the useEffect hook for dynamic loadin ...

How can I retrieve the path to a specific subnode using Javascript/JSON?

What is the best way to obtain a JSON path that leads to a specific child node within an object? For example: var data = { key1: { children: { key2:'value', key3:'value', key4: { ... } ...

Getting the id of a single object in a MatTable

I'm currently working on an angular 8 application where I've implemented angular material with MatTableDatasource. My goal is to retrieve the id from the selected object in my list: 0: {id: "e38e3a37-eda5-4010-d656-08d81c0f3353", family ...

Comparing textboxes on separate web pages to validate forms using AJAX on the server side

I am exploring the creation of a straightforward server-side form validation system using ajax. The objective is to verify if the data inputted by the user matches the data stored on another page. For instance, if a user enters the number 10 in a textbox ...

How can jQuery obtain the value of an ID or class that does not exist?

My current task involves checking if an object has the same id on the DOM because I am working on developing a modal window library. The issue arises when I try to execute the following code in the browser's console: $("#anonexistingid"); It return ...

Looking to dynamically display users added to an array in a table using Angular and JavaScript?

As a newcomer to javascript and angularjs, I recently attempted to build a table that would display all users in an array dynamically as new users are added through a form. However, each time I run my code, I encounter the error message "Fill out the entir ...

The absence of AudioPlayer may be responsible for the compilation failure on Vercel

Here is the latest code snippet import { useState, useEffect, useRef } from "react"; import { FaPlay, FaPause, FaForward, FaBackward } from "react-icons/fa"; export default function Player() { const [isPlaying, setIsPlaying] = useState(false); const [ ...

What steps can be taken to address the problem of the "incorrect referer value" when sending a form?

I am experiencing some challenges with accessing a certain forum as my IP address has been blocked. It seems that another user may have violated the rules and their ISP or IP address is similar to mine, causing me to be impacted. I attempted to post comm ...

Cause a malfunction in the triggering function of jQuery

$(document).ready(function(){ jQuery("#but1").bind("click",function(e){ alert(e.name); }); jQuery("#but1").trigger({name:'Dmy', surname:'My'}); }); The information doesn't seem to be getting passed correctly a ...

LESS — transforming data URIs with a painting mixin

Trying to create a custom mixin for underlining text, similar to a polyfill for CSS3 text-decoration properties (line, style, color) that are not yet supported by browsers. The idea is to draw the proper line on a canvas, convert it to a data-uri, and the ...

How to implement a Typescript interface without necessarily implementing the parent interfaces

Within my current project, I have defined the following interfaces: interface foo { fooProperty: number; fooFunction(): void; } interface bar extends foo { barProperty: string; barFunction(): void; } Now, I am interested in creating a class like ...