2011-08-26 162 views
3

我希望你能帮助我,我有这样的结构:的jqGrid的TreeGrid排序问题

- root A 
    -child_A1 
     -child_A1_1 
     -child_A1_2 
     -child_A1_3 
    -child_A2 
     -child_A2_1 
     -child_A2_2 
     -child_A2_3 

- root B 
    - child_B1 
     -child_B1_1 
     -child_B1_2 
     -child_B1_3 

但是,当我表现出的TreeGrid的数据,这样表示:

- root A 
    -child_A1 

    -child_A2 
     -child_A1_1 

- root B 
    - child_B1 
     -child_B1_1 
     -child_B1_2 
     -child_B1_3 
     -child_A1_2 
     -child_A1_3 
     -child_A2_1 
     -child_A2_2 
     -child_A2_3 

任何人知道为什么..?请大家帮忙,我搜索关于此错误的信息,但不有运气....

这里是我的JavaScript:

<script type="text/javascript"> 
$(document).ready(function() { 
    var lastsel; 
    $(function() { 
     jQuery('#tree').jqGrid({ 
      url: '/Ubicacion/TreeGrid/', 
      datatype: 'json', 
      height: 250, 
      colNames: ['Nombre', 'Descripcion'], 
      colModel: [ 
         { name: 'Nombre', index: 'Nombre', width: 100, sortable: true, editable: true, edittype: "text"}, 
         { name: 'Descripcion', index: 'Descripcion', width: 80, editable: true, edittype: "text" } 
         ], 
      caption: 'Listado de Ubicaciones', 
      treeGridModel: 'adjacency', 
      sortname: 'Nombre', 
      loadonce: true, 
      height: 'auto', 
      width: '500', 
      pager: "#pager", 
      treeGrid: true, 
      ExpandColumn: 'Id', 
      ExpandColClick: true, 
     }); 
    }); 
}); 

这里是我用来生成服务器端功能JSON字符串:

public ActionResult TreeGrid(string sidx, string sord, int? page, int? rows) 
    { 
     List<Ubicacion> ubicacion = new List<Ubicacion>(); 
     ubicacion = UbicacionRepository.GetAll().ToList<Ubicacion>(); 

     int pageIndex = Convert.ToInt32(page) - 1; 
     int totalrecords = ubicacion.Count(); 

     JsonResult json = new JsonResult(); 
     json.JsonRequestBehavior = JsonRequestBehavior.AllowGet; 

     json.Data = new 
     { 
      sidx = "Nombre", 
      sord = "asc", 
      page = page, 
      records = totalrecords, 
      rows = (from ubi in ubicacion 
        select new 
        { 
         cell = new string[] 
          { 
           ubi.Nombre, 
           ubi.Descripcion, 
           ubi.Nivel.ToString(), 
           ubi.IdPadre == 0 ? "null" : ubi.IdPadre.ToString(), 
           ubi.Nivel < 2 ? "false" : "true", 
           "false", 
           "true" 
          } 
        }) 
     }; 
     return json; 
    } 

而这里所产生的JSON:

{"total":1,"page":null,"records":18,"rows":[ 
     {"cell":["Parent A","ubicacion","0","null","false","false","true"]}, 
     {"cell":["Child A1","ubicacion","1","1","false","false","true"]}, 
     {"cell":["Child A2","ubicacion","1","1","false","false","true"]}, 
     {"cell":["Child A1_1","ubicacion","2","2","true","false","true"]}, 
     {"cell":["Parent B","ubicacion","0","null","false","false","true"]}, 
     {"cell":["Child B1","ubicacion","1","5","false","false","true"]}, 
     {"cell":["Child B1_1","ubicacion","2","6","true","false","true"]}, 
     {"cell":["Child B1_2","ubicacion","2","6","true","false","true"]}, 
     {"cell":["Child B1_3","ubicacion","2","6","true","false","true"]}, 
     {"cell":["Child A1_2","ubicacion","2","2","true","false","true"]}, 
     {"cell":["Child_A1_3","ubicacion","2","2","true","false","true"]}, 
     {"cell":["Child A2_1","ubicacion","2","3","true","false","true"]}, 
     {"cell":["Child A2_2","ubicacion","2","3","true","false","true"]}, 
     {"cell":["Child A2_3","ubicacion","2","3","true","false","true"]} 
    ]} 

回答

1

我明白了!你需要递归地对列表进行排序,因为它呈现在你从数据库中提取的确切顺序..

private static List<MENU> Listado = new List<MENU>(); 
    private static List<MENU> lstOrdenada = new List<MENU>(); 

    public List<MENU> MenuRecursivo() 
    { 
     //the whole list of MENU 
     Listado = (from m in db.MENU where m.men_eliminado == "N" select m).ToList(); 
     // a list where we'll put the ordered items 
     lstOrdenada = new List<MENU>(); 

     foreach (MENU item in Listado.Where(x => x.ID_MENU == x.id_menu_padre).ToList()) // in my case, only the root items match this condition 

     { 
      lstOrdenada.Add(item); 
      GMenuHijo(item.ID_MENU, ref lstOrdenada); 
     } 
     return lstOrdenada; 
    } 

`

然后,对每个根项目,递归寻找下一个层次:

private static void GMenuHijo(int idPadre, ref List<MENU> lst) 
{ 
    List<MENU> listado2 = Listado.Where(x => x.id_menu_padre == idPadre && x.ID_MENU != x.id_menu_padre).ToList(); 
    if (listado2.Count > 0) 
    { 
     foreach (MENU item in listado2) 
     { 
      lst.Add(item); 
      GMenuHijo(item.ID_MENU, ref lst); 
     } 
    } 
} 
1

我遇到了同样的问题。看起来jqGrid希望数据已经在树结构中排序(即它不会在客户端执行排序),但我可能是错的。以下是我创建的一些扩展,用于执行包含具有指定ID和父ID属性的对象的通用IEnumerable的树排序。对象将在父标识属性中为null的位置放在根目录下。

public static class TreeSortExtensions 
{ 
    public static IEnumerable<T> OrderByTreeStructure<T>(
     this IEnumerable<T> source, 
     string objectIDProperty, 
     string parentIDPropery) 
    { 
     IEnumerable<T> result = source; 

     if (!string.IsNullOrEmpty(objectIDProperty) && !string.IsNullOrEmpty(parentIDPropery)) 
     { 
      result = source.GetChildrenOfTreeNode(null, objectIDProperty, parentIDPropery, true); 
     } 

     return result; 
    } 

    public static IEnumerable<T> GetChildrenOfTreeNode<T>(
     this IEnumerable<T> source, 
     object parent, 
     string property, 
     string parentProperty, 
     bool recurse) 
    { 
     if (!string.IsNullOrEmpty(property) && !string.IsNullOrEmpty(parentProperty)) 
     { 
      IEnumerable<T> children; 
      if (parent == null) 
      { 
       children = source.Where(x => x.GetPropertyValue(parentProperty) == null); 
      } 
      else 
      { 
       var parentIDValue = parent.GetPropertyValue(property); 
       children = source.Where(x => (x.GetPropertyValue(parentProperty) != null) && 
              (x.GetPropertyValue(parentProperty).Equals(parentIDValue))); 
      } 

      foreach (T child in children) 
      { 
       yield return child; 

       if (recurse) 
       { 
        var grandChildren = source.GetChildrenOfTreeNode(child, property, parentProperty, true).ToArray(); 
        foreach (T grandchild in grandChildren) 
        { 
         yield return grandchild; 
        } 
       } 
      } 
     } 
    } 

    public static object GetPropertyValue(this object obj, string property) 
    { 
     return obj.GetType().GetProperty(property).GetValue(obj, null); 
    } 

} 

请注意,“父”参数是对象类型而不是T.这允许将null作为根级别对象的父级传递。

用法:

var result1 = someEnumerable.OrderByTreeStructure("SomeIDProperty", "SomeParentIDProperty"); 
var result2 = someDbContext.SomeTable.OrderByTreeStructure("ID", "ParentID");