2012-05-14 12 views
0

どのようにajaxを使用してjsonファイルから返された各データに対してaccordianコンテナを作成するために各ループにaを使用しますか?jsonから返されたデータでforeachループからdijit accordianコンテナを作成

私はこれを試しました!それはそれへの道ですか?

dojo.xhrGet({ 
    url:"json/"file_Name".json", 
    handleAs: "json", 
    timeout: 10000, 
    load: function(response,details){ 
     container(response)}, 
    error: function(error_msg,details){ 
     container(error_msg, details); 
    } 

    }); 

     //how do i use the json file to add data to the array arrFruit and then create dijit accordian container for every data in the array// 
    container = function(array, domConstruct) { 

     var arrFruit = ["apples", "kiwis", "pineapples"]; 
     array.forEach(arrFruit, function(item, i){ 
      domConstruct.create("li", {innerHTML: i+1+". "+item}, "properties"); 
     }); 



    }; 
    //the response data from my json file is:- 
    [ 
{ 
    "itemId": 1234, 
    "Name": "Name", 
      }] 

答えて

0

これをItemFileReadStoreを利用して書き直すことをお勧めします。ストアは、IDで項目を引き出すことができるデータコンテナです。これは、あなたのjsonが識別子と(もしあれば)子要素の属性キーの記述で少し変更する必要があることを意味します。

JSON:あなたのコードで、その後

{ 
    identifier: 'itemId', 
    // you dont seem to have child references any but these are defaults 
    childrenAttrs: ['items', 'children'], 
    items: [ 
    { 
    itemId: 1234, 
    name: 'Name' 
    }, { 
     ... 
    } 
    ] 
} 

、代わりにそのような店で.xhr使用.fetchを使用する:

// 1.7+ syntax, pulling in dependencies. 
// We want accordion and some other display widget like contentpane. 
// Also we await domReady before calling our function 
require(["dojo/data/ItemFileReadStore", "dijit/layout/AccordionContainer", "dijit/layout/ContentPane", "dojo/domReady!"], function(itemStore, accordion, contenpane) { 

    // this section is called when loading of itemstore dependencies are done 
    // create store 
    var store = new itemStore({ 
    url:"json/"file_Name".json" 
    }); 
    // create container 
    var acc = new accordion({style:"height: 300px"}, 'someDomNodeId'); 

    // call XHR via .fetch and assign onComplete (opposed to 'load') callback 
    store.fetch({ onComplete: function(items) { 

    // items is an array of all the objects kept in jsonObject.items array 
    items.forEach(function(item) { 
     acc.addChild(new contentpane({ 
      title: "Name for id: " + store.getValue(item, 'itemId'), 
      content: store.getValue(item, 'name') 
     })); 
    }); 
    console.log(acc.getChildren()); // << would print out an array of contentpane widgets 
    }); 
}); 

これはHOWTO :) ある任意の時点で、あなたストアを使用していくつかの項目をフェッチすることができます。いくつかの特定のものを除外したいと言うと、.queryを次のように呼び出します。store.fetch({query: { name: '*foo'/*all item.name ending with foo*/ }, onComplete: function(items) { /*cb func*/});

http://livedocs.dojotoolkit.org/dijit/layout/AccordionContainer#programmatic-example および http://livedocs.dojotoolkit.org/dojo/data/ItemFileReadStore

関連する問題