PDA

View Full Version : TreeLoader loading custom json format



emredagli
2 Nov 2009, 11:16 PM
Hii,
I try to load following json string format for my column tree:


{"success":true,"messageCode":0,"resultSet":[
{
"id": 0,
"status": 0,
"total": 1,
"leaf": true,
"uiProvider": "col"
},
...
]
}

And before showing tree I need to check messageCode. Therefore, all tree data are under the "resultSet" property. (like json reader's "root" property)

Is it possible to load tree with this format?
Thanks.

emredagli
2 Nov 2009, 11:27 PM
Actually I try following code,
But the code looks like trying to pass rectangle into a triangle... :">

ModifiedTreeLoader:


// beforeAddingNodesFunction
// Bu fonksiyon node tree'ye eklenmeden önce set etmek istedigimiz bir property'si varsa onlari set ediyor.
// setAllLeaf: null ise leaf ozelligine dokunulmuyor. true ise hepsi leaf true, false ise hepsi leaf false oluyor.
//{setAllLeaf:null, addChildren:false, addExpanded:false, addEmptyIconCls:false, setAllDisabled:null, removeNodeText:null}
function beforeAddingNodesFunction(obje, options) {
var cond1 = (options.setAllLeaf !== null);
for (var i = 0; i < obje.length; i++) {
if (cond1)
{ obje[i].leaf = options.setAllLeaf; }

if (options.addExpanded)
{ obje[i].expanded = true; }

if (options.addExpandedOnlyLeaf) {
if (obje[i].children) {
if (obje[i].children.length == 0)
{ obje[i].expanded = true; }
}
}

if (options.addEmptyIconCls)
{ obje[i].cls = "x-tree-noicon"; } //"x-tree-noicon"

if (obje[i].expired == 1)
{ obje[i].cls = "x-tree-node-expired"; }

if (options.mapToValue) {
obje[i].text = options.mapToValue[obje[i].text];
}
/*
if (options.setAllDisabled!==null)
{obje[i].disabled = options.setAllDisabled;}
*/
if (obje[i].children) {
beforeAddingNodesFunction(obje[i].children, options);
}
else {
if (options.addChildren)
{ obje[i].children = []; }
}
}
}

// JSON ICIN YAPTIGIM ARAMA
// Bu method Tree'de NA elemanini bir Obje'ye eklemek icin kullaniliyor.
function insertandConvertObectToArray(object, insertedObject, index) {
var sonuc = new Array();
var i = 0, v;
for (ii in object) {
if (i == index) {
sonuc[i] = insertedObject;
i++;
}
if (object.hasOwnProperty(ii)) {
v = object[ii];
switch (typeof v) {
case 'undefined':
case 'function':
case 'unknown':
break;
case 'object':
if (v) {
sonuc[i] = v;
i++;
}
break;
}
}
}
return sonuc;
};

// Bu fonksiyon gene tree'lerde donen JSON objesinin "resultSet" propertysi altindakileri almak icin kullaniliyor.
// Fonksiyona girilen tag altindaki obje aliniyor.
function getSubObject(obje, tag) {
var i, v;

for (i in obje) {
if (i == tag)
return obje.hasOwnProperty(i) ? obje[i] : null;
if (obje.hasOwnProperty(i)) {
v = obje[i];
switch (typeof v) {
case 'undefined':
case 'function':
case 'unknown':
break;
case 'object':
if (v) {
var sonuc = getSubObject(obje, tag);
if (sonuc)
return sonuc;
}
break;
}
}
}
return null;
};


ModifiedTreeLoader = function(config) {
ModifiedTreeLoader.superclass.constructor.call(this, config);
}

Ext.extend(ModifiedTreeLoader, Ext.tree.TreeLoader, {
rootNodeTag: "resultSet",
objectNA: null,
beforeAddingNodes: null, //{setAllLeaf:null, addChildren:false, addExpanded:false, addEmptyIconCls:false},
//addingEmptyChild: false,
// private
runCallback: function(cb, scope, args){
if(Ext.isFunction(cb)){
if (args === undefined){
args = new Array();
}
cb.apply(scope, args);
}
},
processResponse: function(response, node, callback, scope) {
var json = response.responseText;
//try {
var o = Ext.util.JSON.decode(json); //eval("("+json+")");

// Burasi sonrada eklendi.
if (o.message == "Access_Denied_Unsufficent_User_Permission")
{
showAccessDeniedMessageBox();
o[this.rootNodeTag] = new Array();
//return;
}

if(this.rootNodeTag)
{
o = o[this.rootNodeTag]; //getSubObject(o, this.rootNodeTag);
if (!o) {o = new Array();}

if (this.objectNA)
{ o = insertandConvertObectToArray(o, this.objectNA, 0); }

if (this.beforeAddingNodes)
{ beforeAddingNodesFunction(o, this.beforeAddingNodes); }
}
// alttaki satira sanirim gerek yok...
response.responseText = Ext.util.JSON.encode(o);

node.beginUpdate();
for(var i = 0, len = o.length; i < len; i++){
var n = this.createNode(o[i]);
if(n){
node.appendChild(n);
}
}

node.endUpdate();
this.runCallback(callback, scope || node, [node]);

//} catch (e) {
// this.handleFailure(response);
//}
}
});


Not elegant...