2013-08-26 105 views
0

我有一個樹面板,我動態生成根節點根據組合框中選定的組的根節點。基於選擇的組,將從服務器返回JSON並在遍歷JSON對象之後返回。我正在生成樹面板的節點。如果選擇了「MM」,這是所有其他組的父組。根節點將是「移動測量」,其所有子節點將是其他組。動態extjs樹面板生成問題

enter image description here

var groupKey = field.getValue(); 
var groupName = field.getRawValue(); 
var subGroupName = ''; 
var treePanel = Ext.getCmp('permissionsTreePanel'); 
var len = []; 
treePanel.setRootNode(null); 

if(newValue){ 
Ext.Ajax.request({ 
    url:'.../GetAllUserPermissions.php', 
    method:'POST', 
    params:{groups:newValue}, 
    success:function(result, request){ 
     var json = result.responseText; 
     var temp = Ext.decode(json); 
     var obj = {}; 
     var mainGroup = {}; 
     var item = {}; 
     var menu = {}; 
     var children = []; 
     var subGroups =[]; 
     var groups = ''; 
     var module = [{ 
      "text": "Administrator", 
      "leaf": true, 
      "checked": false, 
      "id":"GROUP_ADMIN", 
      "formBind": true, 
     "name":"GROUP_ADMIN"}]; 

     //Ext.Msg.alert('Message',Ext.encode(temp[groupKey])); 

     groupTree = function (group,name) { 

      for(var i=0;i<Object.keys(group).length;i++){ 
       for(var key in group[i]){ 
        for(var n=0;n<Object.keys(group[i][key]).length;n++){ 
         item = { 
          "text": group[i][key][n], 
          "checked": false, 
          "id": key+"_"+group[i][key][n], 
          "name": key+"_"+group[i][key][n], 
          "formBind":true, 
          "leaf": true 
         }; 
         children.push(item); 
        } 
        menu = {"text": key, 
         "leaf": false, 
         "id": key, 
         "children": children       
        }; 

        module.push(menu); 
        children = []; 
       } 
      } 

      obj = {"text":name, 
       "expanded": true, 
       "formBind": true, 
       "children": module 
      }; 

      module = [{ 
       "text": "Administrator", 
       "leaf": true, 
       "checked": false, 
       "id":"GROUP_ADMIN", 
       "formBind": true, 
      "name":"GROUP_ADMIN"}]; 

      return obj; 

     }; 

     if(groupKey != 'MM'){ 

      groupTree(temp[groupKey],groupName); 
      treePanel.setRootNode(obj); 

     } else { 

      treePanel.getRootNode().set("text",groupName);    

      var i = 0; 

      for(var key in temp[groupKey]){ 

       i++; 

       mainGroup = groupTree(temp[groupKey][key],key); 

       obj = {}; 

       treePanel.getRootNode().appendChild(mainGroup); 

       if(i==2){ 
        //break; 
       } 
      } 

      treePanel.getRootNode().expand(); 

      //treePanel.setRootNode(subGroups); 

      Ext.Msg.alert('Message',Ext.encode(subGroupName)); 

     } 
    }, 
    failure:function(result, request){ 
    } 
}); } else { 
Ext.Msg.alert('Message','No value is selected!'); } 

都已經生成的組,但這些事件都不盡如人意。有項目選擇和事件派發有一些錯誤。通過單擊一個節點,另一個節點正在擴展。這些未知行爲導致樹面板出現問題。

您的幫助將不勝感激!

在此先感謝。 :)

回答

0

我得到了答案。生成每個節點的ID存在問題。模塊級節點對於多個模塊節點具有相同的ID。這就是我得到異常行爲的原因。現在它在刪除ID後正常工作。

var groupKey = field.getValue(), 
groupName = field.getRawValue(), 
subGroupName = '', 
treePanel = Ext.getCmp('permissionsTreePanel'), 
len = []; 

treePanel.setRootNode(null); 

if (newValue !== null) { 
Ext.Ajax.request({ 
    url: '.../SOUPAPI/user/GetAllUserPermissions.php', 
    method: 'POST', 
    params: { 
     groups: newValue 
    }, 
    success: function (result, request) { 
     var json = result.responseText, 
      temp = Ext.decode(json), 
      obj = {}, 
      mainGroup = {}, 
      item = {}, 
      menu = {}, 
      children = [], 
      subGroups = [], 
      groups = '', 
      module = [{ 
       "text": "Administrator", 
       "leaf": true, 
       "checked": false, 
       "formBind": true, 
       "name": "GROUP_ADMIN" 
      }]; 

     groupTree = function (group, name, gkey) { 

      for (var i = 0; i < Object.keys(group).length; i++) { 
       for (var key in group[i]) { 
        for (var n = 0; n < Object.keys(group[i][key]).length; n++) { 

         item = { 
          "text": (group[i][key][n]).split("_").join(" "), 
          "checked": false, 
          "id": key + "_" + group[i][key][n], 
          "name": key + "_" + group[i][key][n], 
          "formBind": true, 
          "leaf": true 
         }; 
         children.push(item); 
        } 
        menu = { 
         "text": key.substring(2), 
         "leaf": false, 
         "name": key, 
         "children": children 
        }; 

        module[0].id = gkey + "GROUP_ADMIN"; 
        module.push(menu); 
        children = []; 
        menu = {}; 

        test = module; 
       } 
      } 

      obj = { 
       "text": name, 
       "expanded": true, 
       "formBind": true, 
       "children": module 
      }; 

      module = [{ 
       "text": "Administrator", 
       "leaf": true, 
       "checked": false, 
       "formBind": true, 
       "name": "GROUP_ADMIN" 
      }]; 

      return obj; 

     }; 

     if (groupKey != 'MM') { 

      var group = groupTree(temp[groupKey], groupName, groupKey); 
      treePanel.setRootNode(obj); 

     } else { 

      var rootNode = treePanel.getRootNode(); 
      rootNode.set("text", groupName); 
      for (var key in temp[groupKey]) { 

       mainGroup = groupTree(temp[groupKey][key], key, key); 
       obj = {}; 
       rootNode.appendChild(mainGroup); 
      } 

      rootNode.cascadeBy(function() { 
       for (var l in rootNode.childNodes) { 
        rootNode.childNodes[l].collapse(); 
       } 
      }); 
      rootNode.expand(); 
     } 
    }, 
    failure: function (result, request) {} 
}); 
}