2011-03-29 41 views
1

我有一個dojo.data.ItemFileWriteStore對象,其中的項目包含'children'屬性,該屬性又包含一個children項目數組。我正在存儲一棵樹,並且我需要從此樹中獲取葉節點。我已經寫了一個fetch方法,但它不起作用,給它一個查詢「children:[]」我如何獲取具有children.length 0(葉節點)的數據存儲項目?沒有添加屬性的空數組,如'葉':我的項目布爾顯然會工作,但我寧願沒有額外的屬性。查詢dojo.data.ItemFileWriteStore的屬性屬性(並不那麼容易)

dojo.require(dojo.data.ItemFileWriteStore); 

// A tree node with no children, these are the kind I want returned 
// from the query! 
var rootItem = { 
     children: [] 
}; 

var treeStore = new dojo.data.ItemFileWriteStore({ 
    data: { 
     items: [rootItem] 
    } 
}); 

//when dojo reaches one of its inner filtering methods 
//there is a point where it calls dojo.some() to see 
//which elements in the array to return which match the 
//given items attribute, this is where it fails 

treeStore.fetch({ 
    query: {children: []}, 
    queryOptions: {deep: true}, 
    onComplete: function(leafItems) { 
     // All the items with no children here... 
    } 
}); 

我也試過嵌套屬性的功能無濟於事:

treeStore.fetch({ 
    query: { 
     children: function(store, item){ 
       return store.getValue(item, 'children').length == 0; 
     } 
    }, 
    queryOptions: {deep: true}, 
    onComplete: function(leafItems) { 
     // All the items with no children here... 
    } 
}); 
+0

我對數據存儲中的層次數據不太感興趣,但據說Dojo數據存儲支持分層數據。您必須打開分層選項或其他內容。谷歌它,你會發現它。 – 2011-03-30 04:44:31

回答

1
baseStore.fetch({ 
       query:{id:'*'}, 
       onComplete:function(a,b,c){ 
        dojo.forEach(a,function(item,index){ 
         console.log(item.children); 
        }) 
       } 
      }) 

如果item.children是不確定的則是那些在「項目」是你想要的。 如果您不希望單獨對此操作使用單獨的提取操作,那麼請在商店中使用名爲_arrayOfAllItems的私有屬性來獲取平面數據存儲,您可以在上面執行上述相同條件。

+0

這是我能想到的唯一解決方案。 – Salami 2011-07-31 01:50:49