JqG​​rid Treegrid排序问题

时间:2011-08-26 22:03:37

标签: jqgrid jqgrid-asp.net 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"]}
    ]}

2 个答案:

答案 0 :(得分: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 :(得分:1)

我遇到了同样的问题。似乎jqGrid期望数据已经在树结构中排序(即它不在客户端执行排序)但我可能错了。下面是我创建的一些扩展,用于执行包含具有指定ID和父ID属性的对象的通用IEnumerable的树类。父ID属性中的对象将为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);
    }

}

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

用法:

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