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

わかった!データベースから抽出した正確な順序でレンダリングされるため、リストを再帰的に並べ替える必要があります。

    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);
        }
    }
}
于 2011-10-10T15:16:30.280 に答える