示例#1
0
        /// <summary>
        /// 递归方式,获取子菜单,以json的格式
        /// </summary>
        /// <param name="ds"></param>
        /// <returns></returns>
        public static string GetJsonForTreeByDataset_Menu(DataSet ds, string where, int type, string oderby)
        {
            DataTable     dt   = ds.Tables[0];
            StringBuilder json = new StringBuilder();

            json.Append("[");
            foreach (DataRow dr in dt.Rows)
            {
                BLL.T_Menu bll_menu = new BLL.T_Menu();

                json.Append("{\"id\":" + dr["id"].ToString());

                json.Append(",\"text\":\"" + dr["name"].ToString() + "\"");
                json.Append(",\"_sort\":\"" + dr["_sort"].ToString() + "\"");
                json.Append(",\"isDeleted\":\"" + dr["isDeleted"].ToString() + "\"");
                json.Append(",\"icon\":\"" + "icon-man" + "\"");
                json.Append(",\"state\":\"true\"");
                json.Append(",\"url\":\"" + dr["url"].ToString() + "\"");
                json.Append(",\"description\":\"" + dr["description"].ToString() + "\"");
                json.Append(",\"pid\":\"" + dr["pid"].ToString() + "\"");
                json.Append(",\"attributes\":{\"iconCls\":\"" + "icon-man" + "\"" + ",\"url\":\"" + dr["url"].ToString() + "\"" + "}");

                //获取下一级
                DataSet   dsChilds = bll_menu.GetLevelListByPId(Convert.ToInt32(dr["id"].ToString()), false, where, oderby);
                DataTable dtChilds = dsChilds.Tables[0];
                //下一级含有在权限范围之内的
                if (dtChilds.Rows.Count > 0)
                {
                    //根据id获取子菜单
                    if (dtChilds != null && dtChilds.Rows.Count > 0)
                    {
                        json.Append(",\"children\":");
                        //子菜单递归拼接
                        json.Append(GetJsonForTreeByDataset_Menu(dsChilds, where, type, oderby));
                    }
                }

                json.Append("},");
            }
            if (dt.Rows.Count > 0 && json.ToString().Substring(json.Length - 1, 1) == ",")
            {
                json.Remove(json.Length - 1, 1);
            }
            json.Append("]");
            return(json.ToString());
        }
示例#2
0
        /// <summary>
        /// 把dataset数据转换成treegrid 需要的json的格式
        /// </summary>
        /// <param name="ds"></param>
        /// <returns></returns>
        public static string DataSet2JsonForTree_Menu(DataSet ds, string where, string orderby)
        {
            DataTable     dt    = ds.Tables[0];
            StringBuilder json  = new StringBuilder();
            String        pname = "";

            json.Append("[");
            foreach (DataRow dr in dt.Rows)
            {
                BLL.T_Menu bll_menu = new BLL.T_Menu();
                json.Append("{\"id\":" + dr["id"].ToString());
                json.Append(",\"text\":\"" + dr["name"].ToString() + "\"");
                json.Append(",\"state\":\"true\"");
                json.Append(",\"icon\":\"" + "" + "\"");
                json.Append(",\"url\":\"" + dr["url"].ToString() + "\"");
                json.Append(",\"description\":\"" + dr["description"].ToString() + "\"");
                json.Append(",\"pid\":\"" + dr["pid"] + "\"");

                //获取上一级菜单名称pname
                if (dr["pid"] != null && dr["pid"].ToString() != "")
                {
                    pname = bll_menu.GetModel(Convert.ToInt32(dr["pid"])).name;
                }
                json.Append(",\"pname\":\"" + pname + "\"");
                //json.Append(",\"url\":\"" + dr["url"] + "\"");
                DataTable dtChildren = new DataTable();

                //调用D层方法获取dataTable
                DataSet   dsChilds = bll_menu.GetLevelListByPId(Convert.ToInt32(dr["id"].ToString()), false, where, orderby);
                DataTable dtChilds = dsChilds.Tables[0];
                if (dtChilds != null && dtChilds.Rows.Count > 0)
                {
                    json.Append(",\"children\":");
                    json.Append(DataSet2JsonForTree_Menu(dsChilds, where, orderby));
                }
                json.Append("},");
            }
            if (dt.Rows.Count > 0)
            {
                json.Remove(json.Length - 1, 1);
            }
            json.Append("]");
            return(json.ToString());
        }