| | |
| | | { |
| | | try |
| | | { |
| | | // åæ°åæ¥è¯¢ï¼é¿å
SQL注å
¥ |
| | | string sql1 = string.Format("select hitemid,hnumber,hname,hparentid,hlevel,hhelpcode from Gy_MaterType where HUSEORGID = '" + HOrgID + "' order by hhelpcode"); |
| | | |
| | | ds = oCN.RunProcReturn(sql1, "Gy_MaterType"); |
| | | |
| | | List<TreeModel> treeModels = new List<TreeModel>(); |
| | | TreeModel first = new TreeModel(); |
| | | first.id = "0"; |
| | | first.title = "ç©æå类设置"; |
| | | treeModels.Add(first); |
| | | |
| | | // 1. 建ç«ç¶IDå°åèç¹åè¡¨çæ å°ï¼ä¸æ¬¡æ§éåï¼ |
| | | var parentDict = new Dictionary<string, List<DataRow>>(); |
| | | foreach (DataRow row in ds.Tables[0].Rows) |
| | | { |
| | | var HLevel = (int)row["hlevel"]; |
| | | if (HLevel == 1) |
| | | { |
| | | TreeModel tree = new TreeModel(); |
| | | tree.id = row["hitemid"].ToString(); |
| | | tree.title = row["hname"].ToString(); |
| | | treeModels[0].children.Add(tree); |
| | | } |
| | | string parentId = row["hparentid"].ToString(); |
| | | if (!parentDict.ContainsKey(parentId)) |
| | | parentDict[parentId] = new List<DataRow>(); |
| | | parentDict[parentId].Add(row); |
| | | } |
| | | getTreeByLevel(ds.Tables[0], treeModels[0].children, 2); |
| | | |
| | | // 2. æå»ºæ ¹èç¹ |
| | | var root = new TreeModel |
| | | { |
| | | id = "0", |
| | | title = "ç©æå类设置", |
| | | children = new List<TreeModel>() |
| | | }; |
| | | |
| | | // 3. éå½æå»ºæ ï¼ä»ç¶ID为 "0" çèç¹å¼å§ï¼åè®¾æ ¹èç¹çhparentid为0ï¼ |
| | | BuildTree(root, parentDict); |
| | | |
| | | // åºååè¿å |
| | | objJsonResult.code = "1"; |
| | | objJsonResult.count = 1; |
| | | objJsonResult.Message = "Sucessï¼"; |
| | | objJsonResult.data = Newtonsoft.Json.JsonConvert.SerializeObject(treeModels); |
| | | objJsonResult.data = Newtonsoft.Json.JsonConvert.SerializeObject(new List<TreeModel> { root }); |
| | | return objJsonResult; |
| | | } |
| | | catch (Exception e) |
| | |
| | | } |
| | | } |
| | | |
| | | public void getTreeByLevel(DataTable dt, List<TreeModel> tree, int num) |
| | | // éå½æå»ºæ çè¾
婿¹æ³ |
| | | private void BuildTree(TreeModel parentNode, Dictionary<string, List<DataRow>> parentDict) |
| | | { |
| | | for (int m = 0; m < tree.Count; m++) |
| | | if (parentDict.TryGetValue(parentNode.id, out var childRows)) |
| | | { |
| | | tree[m].children = new List<TreeModel>(); |
| | | for (int i = 0; i < dt.Rows.Count; i++)//ç¬¬ä¸æ¬¡å¾ªç¯ï¼å¾å°æææ ¹èç¹çåé |
| | | foreach (DataRow row in childRows) |
| | | { |
| | | var HLevel = (int)dt.Rows[i]["hlevel"]; |
| | | var HParentID = dt.Rows[i]["hparentid"].ToString(); |
| | | if (HLevel == num && HParentID==tree[m].id) |
| | | var childNode = new TreeModel |
| | | { |
| | | TreeModel tbjson = new TreeModel(); |
| | | tbjson.id = dt.Rows[i]["hitemid"].ToString(); |
| | | tbjson.title = dt.Rows[i]["hname"].ToString(); |
| | | tree[m].children.Add(tbjson); |
| | | } |
| | | } |
| | | for (int i = 0; i < tree[m].children.Count; i++) |
| | | { |
| | | getTreeByLevel(dt, tree[m].children, num+1);//忬¡ç¨åéå»å¾ªç¯ï¼æ¿åºåéçåé |
| | | id = row["hitemid"].ToString(), |
| | | title = row["hname"].ToString(), |
| | | children = new List<TreeModel>() |
| | | }; |
| | | parentNode.children.Add(childNode); |
| | | // éå½æå»ºåèç¹çåæ |
| | | BuildTree(childNode, parentDict); |
| | | } |
| | | } |
| | | |
| | | } |
| | | #endregion |
| | | } |