1. 程式人生 > >ajax 給 easyui-tree 賦值

ajax 給 easyui-tree 賦值

html

   <ul id="Menu" class="easyui-tree" style="width: 100%; padding: 0 0 0 0; margin: 0 0 0 0;"
            data-options="fit:true,dnd:false">
        </ul>

js
  function BuildMenu() {
            $.ajax({
                url: "ashx/MainMenuHandler.ashx",
                success: function (ret) {
                    treedata = eval("(" + ret + ")");
                   
                    $("#Menu").tree({ data: treedata, onClick: function (node) {
                        if (node.url != "") {
                           // OpenTab(node.text, node.url);   開啟選單
                        }
                        else if (node.motion != "") {
                            eval(node.motion);
                        }
                    }
                    });

                },
                error: function (err) {
                    alert(err);
                }
            });         
        }
ashx
  private string CreateMenu(int Level, string ParentMenuCode, DBOperate dbo)
    {

        string strResult = "[";
        string strMysql = "select * from vUserMenu where loginname=@loginname and  ParentMenuCode=@ParentMenuCode order by orderindex";

        SqlParameter[] pas ={ 
                DBOperate.MakeInParam("@loginname", CurrentUser.LoginName),
                DBOperate.MakeInParam("@ParentMenuCode",ParentMenuCode)};
        
        DataTable dt = dbo.ExecuteTable(strMysql, pas, CommandType.Text);

        if (CurrentUser.OrgCountyID == "")
        { 
        }
        int i = 0;
        foreach (DataRow row in dt.Rows)
        {
            string strItem = "'id':'{0}','text':'{1}','iconCls':'{2}','children':{3},'url':'{4}','motion':'{5}'";

            string strId = row["menucode"].ToString();//Level.ToString() + i.ToString(); 
            string strUrl = Lib.EncodeUrlParas(row["url"].ToString());
            strUrl = strUrl.Replace("$sys_dir$", virtualPath);
            string strMenuName = row["MenuName"].ToString();
            string strMenuCode = row["MenuCode"].ToString();
            string strCLS = row["ICON"].ToString();
            string strMotion = row["functionScript"].ToString();
            string strChild = "[]";
            if (Level < 1)
            {
                strChild = CreateMenu(Level + 1, strMenuCode, dbo); //有子選單,遞迴呼叫               
            }
            
            strItem = string.Format(strItem, new string[] { strId, strMenuName, strCLS,strChild,strUrl,strMotion });
            
            if (dt.Rows.IndexOf(row)>0)
                strResult += ",";

            strResult += "{" + strItem + "}";
        }
        strResult  +="]";            
        return strResult;
    }