简体   繁体   English

复杂的JSON结构+ Dojo选择

[英]Complex JSON structure + Dojo Selects

I have a data structure and i don't know how to manage it properly. 我有一个数据结构,我不知道如何正确管理它。 I must use Dojo, so i chose JSON to solve this problem. 我必须使用Dojo,因此我选择JSON来解决此问题。 The structure i'm talking about is on the bottom of this post. 我正在谈论的结构在这篇文章的底部。 Go check it before you read the following text. 在阅读以下文本之前,请先检查一下。

The problem: i must have 3 Selects. 问题:我必须有3个选择。 The first select will contain the "first level" (a, b, c). 第一次选择将包含“第一级”(a,b,c)。 The second one will contain the "second level" (aa, ab, ...) and so on. 第二个将包含“第二级”(aa,ab,...),依此类推。

The data in each Select (except the first one) should vary depending on what was chosen in the previous Select. 每个Select(第一个Select除外)中的数据应根据上一个Select中选择的内容而有所不同。

For example, if i choose "a" in the first Select, i should only see the "aa, ab, ac" in the second Select. 例如,如果我在第一个“选择”中选择“ a”,则我应该仅在第二个“选择”中看到“ aa,ab,ac”。 Then if i choose "ac" in the second one, i should only see "aca, acb, acc". 然后,如果我在第二个选项中选择“ ac”,则应该只看到“ aca,acb,acc”。

Note that there are cases when there is no "third level". 请注意,在某些情况下,没有“第三级”。 In this case, the third select should be hidden (OR if it's too hard to manage, just be empty). 在这种情况下,应该隐藏第三个选择(或者,如果太难管理,则为空)。

Now for the references. 现在供参考。 If i choose "d" in the first Select, i should only see "aa, bc, ca" as options of the second Select. 如果我在第一个“选择”中选择“ d”,则应该仅将“ aa,bc,ca”作为第二个“选择”的选项。 Then, if i choose "cc" in the second Select, i should see "cca, ccb, ccc" in the third Select. 然后,如果我在第二个Select中选择“ cc”,则应该在第三个Select中看到“ cca,ccb,ccc”。 This should not be static of course. 当然,这不应该是静态的。 In my example, if the "c -> cc" subtree will change, the contents of "d -> cc" should change too. 在我的示例中,如果“ c-> cc”子树将更改,则“ d-> cc”的内容也应更改。

Now for the questions: 现在开始提问:

1) How do i make something like that work using Dojo? 1)如何使用Dojo进行类似的工作? If it was Vanilla JS it would be obvious for me, but i can't figure how to make it work with all these object stores and such things. 如果是Vanilla JS,这对我来说将是显而易见的,但是我无法弄清楚如何使其与所有这些对象存储库以及类似的东西一起使用。

2) How do i make References work with that kind of structure? 2)我如何使参考使用这种结构? I could make it work on a simple JSON, but when it gets so complex i'm completely lost. 我可以使它在简单的JSON上工作,但是当它变得如此复杂时,我完全迷失了。

3) How do i present the structure? 3)我如何呈现结构? As a single JSON? 作为单个JSON? Or should i split it into several parts? 还是应该将其拆分为几个部分?

Thanks 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>

Well, you can represent it as an object, for 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;

But anyways, the implementation will be quite complex as well. 但是无论如何,实现也会非常复杂。 First of all I would suggest writing a function that gets the labels of each level, depending on the parameter. 首先,我建议编写一个函数,该函数根据参数获取每个级别的标签。 For example: 例如:

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 will return the proper data that should be available inside the store, containing the label ("a", "b", "c", ...) and the object structure of the next level. 这将返回商店内应该可用的正确数据,其中包含标签(“ a”,“ b”,“ c”,...)和下一级的对象结构。

Then you can write something like this: 然后,您可以编写如下内容:

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);

This will, retrieve the data (using the functions earlier) when the value changes and it will then populate the store of the next level with that data. 当值更改时,这将检索数据(使用先前的函数),然后将使用该数据填充下一级别的存储。

This is not a complete solution yet though. 但是,这还不是一个完整的解决方案。 Because you still need to do some intialization, but it gives you an idea of how you could approach this situation. 因为您仍然需要进行一些初始化,但是它使您对如何解决这种情况有了一个了解。

An example: http://jsfiddle.net/ekoronra/ 范例: http//jsfiddle.net/ekoronra/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM