3

我希望你能帮助我,我有这样的结构:

- 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"]}
    ]}
4

2 回答 2

1

I got it! you need to order recursively the list, because it's rendering in the exact order you extracted from your db..

    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;
    }

`

Then, for each root item, recursively find the next levels:

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);
        }
    }
}
于 2011-10-10T15:16:30.280 回答
1

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

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);
    }

}

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

用法:

var result1 = someEnumerable.OrderByTreeStructure("SomeIDProperty", "SomeParentIDProperty");
var result2 = someDbContext.SomeTable.OrderByTreeStructure("ID", "ParentID");
于 2013-07-19T01:12:17.393 回答