dojodijit.tree

dojo.data.ItemFileWriteStore: Invalid item argument in dijit.tree


While working with dojo toolkit and the djit.tree widget, I received the the following error:

dojo.data.ItemFileWriteStore: Invalid item argument 

in my javascript function which loads data in json format from my php server and displays it in a dijit.tree using a ItemFileWriteStore and a ForestStoreModel. My json data is the following:

{
    "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"
            }]
        }]
    }]
}

The code is formatted on server side using 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();

Now json data:

  {"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"}]}]}]}

Solution

  • You have an id collision with your data. There are two items with the same id of 105right234 and 106right2223.

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

    If you want the same item to appear twice in the structure, then you need to look at how the _reference structure is used.

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


    Your second set of data loads fine into the store.

    The following code is not good practice for Dojo widgets.

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

    Dojo uses the convention of myWidget.set('model', new model); This allows for custom setter logic. See this link for more.

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

    I created another fiddle that uses your data and creates a tree.

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