0

在使用 dojo 工具包和djit.tree小部件时,我收到以下错误:

dojo.data.ItemFileWriteStore: Invalid item argument 

在我的 javascript 函数中,它从我的 php 服务器加载 json 格式的数据,并dijit.tree使用 aItemFileWriteStore 和 a将其显示在 a 中ForestStoreModel。我的json数据如下:

{
    "identifier": "id",
    "label": "name",
    "items": [{
        "type": "parent",
        "name": "Adresse des Finanzamts",
        "id": "1020right00",
        "children": [{
            "type": "Leaf",
            "name": "Relationship type: 1:1",
            "id": "Adresse des Finanzamts1:1"
        }, {
            "type": "parent",
            "name": "Left",
            "id": "Adresse des Finanzamts010",
            "children": [{
                "type": "Leaf",
                "name": "Table name: Finanzamtdaten",
                "id": "1014left23432"
            }, {
                "type": "Leaf",
                "name": "Field name: fkFinanzamtAdresse:INTEGER",
                "id": "1018lef423t"
            }]
        }, {
            "type": "parent",
            "name": "Right",
            "id": "Adresse des Finanzamts111",
            "children": [{
                "type": "Leaf",
                "name": "Table name: Adresse",
                "id": "105right234"
            }, {
                "type": "Leaf",
                "name": "Field name: adresseId:INTEGER",
                "id": "106right2223"
            }]
        }]
    }, {
        "type": "parent",
        "name": "Adresse des Mitarbeiters",
        "id": "1013right00",
        "children": [{
            "type": "Leaf",
            "name": "Relationship type: 1:1",
            "id": "Adresse des Mitarbeiters1:1"
        }, {
            "type": "parent",
            "name": "Left",
            "id": "Adresse des Mitarbeiters010",
            "children": [{
                "type": "Leaf",
                "name": "Table name: Mitarbeiter",
                "id": "100left23432"
            }, {
                "type": "Leaf",
                "name": "Field name: fkAdresse:INTEGER",
                "id": "1012lef423t"
            }]
        }, {
            "type": "parent",
            "name": "Right",
            "id": "Adresse des Mitarbeiters111",
            "children": [{
                "type": "Leaf",
                "name": "Table name: Adresse",
                "id": "105right234"
            }, {
                "type": "Leaf",
                "name": "Field name: adresseId:INTEGER",
                "id": "106right2223"
            }]
        }]
    }]
}

代码在服务器端使用json_encode().

 var treeObject = dijit.byId("relationStructure");
            treeObject.model.store.clearOnClose = true;
            treeObject.model.store.close();
            var newStore = new dojo.data.ItemFileWriteStore({
                data: data
            });

            treeObject.model.store = newStore;
            treeObject.model.query = {"type": "parent"};
            treeObject.rootId = "root";
            treeObject.rootLabel = "Table content";
            treeObject.childrenAttrs = ["children"];
            treeObject.showRoot = false;

            treeObject.refreshModel();

现在json数据:

  {"identifier":"id","label":"name","items":[{"type":"parent","name":"Finanzamtdaten des Mitarbeiters","id":"1022right000","children":[{"type":"Leaf","name":"Relationship type: 1:1","id":"Finanzamtdaten des Mitarbeiters1:1"},{"type":"parent","name":"Left","id":"Finanzamtdaten des Mitarbeiters0100","children":[{"type":"Leaf","name":"Table name: Mitarbeiter","id":"100left234320"},{"type":"Leaf","name":"Field name: fkFinanzamtdaten:INTEGER","id":"1019lef423t0"}]},{"type":"parent","name":"Right","id":"Finanzamtdaten des Mitarbeiters1110","children":[{"type":"Leaf","name":"Table name: Finanzamtdaten","id":"1014right2340"},{"type":"Leaf","name":"Field name: finanzamtdatenId:INTEGER","id":"1015right22230"}]}]},{"type":"parent","name":"Adresse des Mitarbeiters","id":"1013right001","children":[{"type":"Leaf","name":"Relationship type: 1:1","id":"Adresse des Mitarbeiters1:1"},{"type":"parent","name":"Left","id":"Adresse des Mitarbeiters0101","children":[{"type":"Leaf","name":"Table name: Mitarbeiter","id":"100left234321"},{"type":"Leaf","name":"Field name: fkAdresse:INTEGER","id":"1012lef423t1"}]},{"type":"parent","name":"Right","id":"Adresse des Mitarbeiters1111","children":[{"type":"Leaf","name":"Table name: Adresse","id":"105right2341"},{"type":"Leaf","name":"Field name: adresseId:INTEGER","id":"106right22231"}]}]}]}
4

1 回答 1

1

您的数据存在 id 冲突。有两个具有相同 id 的项目105right234106right2223

"dojo.data.ItemFileWriteStore:  The json data provided by the creation arguments 
is malformed.  Items within the list have identifier: [id].  
Value collided: [105right234]"

如果你想让同一个项目在结构中出现两次,那么你需要看看_reference结构是如何使用的。

http://dojotoolkit.org/reference-guide/1.8/dojo/data/ItemFileReadStore.html#input-data-format


您的第二组数据可以很好地加载到商店中。

以下代码对于 Dojo 小部件来说不是很好的做法。

treeObject.model.store = newStore;
treeObject.model.query = {"type": "parent"};
treeObject.rootId = "root";
treeObject.rootLabel = "Table content";
treeObject.childrenAttrs = ["children"];
treeObject.showRoot = false;

Dojo 使用 myWidget.set('model', new model); 的约定。这允许自定义设置器逻辑。有关更多信息,请参阅此链接。

http://dojotoolkit.org/reference-guide/1.8/quickstart/writingWidgets.html#custom-setters-getters

我创建了另一个使用您的数据并创建树的小提琴。

http://jsfiddle.net/cswing/5Yjx9/

于 2013-02-13T14:35:18.930 回答