FlowPortal-BPM——获取树状结构的物料分类
/// <summary> /// 获取物料分类-树形结构 /// </summary> /// <returns></returns> public JObject getMaterialBasicClass(HttpContext context) { string sql = "select pk_marbasclass, code, name, pk_parent from V_NCC_MaterialBasicClass where name not like '%停用%' and code not in('T','Z') order by pk_marbasclass"; SqlConnection con = new SqlConnection(); con.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["BLDB"].ConnectionString; SqlDataAdapter ada = new SqlDataAdapter(); SqlCommandBuilder sb = new SqlCommandBuilder(ada); ada.SelectCommand = new SqlCommand(sql, con); DataSet ds = new DataSet(); ada.Fill(ds); DataTable tb = ds.Tables[0]; string key, MID, text, leaf, searchType = "11"; //↓↓↓一级分类 List<object> Leafchildren_1 = new List<object>(); for (int i = 0; i < tb.Rows.Count; i++) { if (tb.Rows[i]["pk_parent"].ToString() == "~") { //↓↓↓二级分类 List<object> Leafchildren_2 = new List<object>(); for (int j = 0; j < tb.Rows.Count; j++) { if (tb.Rows[j]["pk_parent"].ToString() == tb.Rows[i]["pk_marbasclass"].ToString()) { //↓↓↓三级分类 List<object> Leafchildren_3 = new List<object>(); for (int k = 0; k < tb.Rows.Count; k++) { if (tb.Rows[k]["pk_parent"].ToString() == tb.Rows[j]["pk_marbasclass"].ToString()) { //↓↓↓四级分类 List<object> Leafchildren_4 = new List<object>(); for (int l = 0; l < tb.Rows.Count; l++) { if (tb.Rows[l]["pk_parent"].ToString() == tb.Rows[k]["pk_marbasclass"].ToString()) { DataRow item_4 = tb.Rows[l]; key = item_4["pk_marbasclass"].ToString(); MID = item_4["code"].ToString(); text = item_4["name"].ToString(); leaf = "true"; Leafchildren_4.Add(new { key, MID, leaf, text, searchType }); } } //↑↑↑四级分类 DataRow item_3 = tb.Rows[k]; key = item_3["pk_marbasclass"].ToString(); MID = item_3["code"].ToString(); text = item_3["name"].ToString(); if (Leafchildren_4.Count > 0) { leaf = "false"; Leafchildren_3.Add(new { key, MID, leaf, text, searchType, Leafchildren_4 }); } else { leaf = "true"; Leafchildren_3.Add(new { key, MID, leaf, text, searchType }); } } } //↑↑↑三级分类 DataRow item_2 = tb.Rows[j]; key = item_2["pk_marbasclass"].ToString(); MID = item_2["code"].ToString(); text = item_2["name"].ToString(); if (Leafchildren_3.Count > 0) { leaf = "false"; Leafchildren_2.Add(new { key, MID, leaf, text, searchType, Leafchildren_3 }); } else { leaf = "true"; Leafchildren_2.Add(new { key, MID, leaf, text, searchType }); } } } //↑↑↑二级分类 DataRow item_1 = tb.Rows[i]; key = item_1["pk_marbasclass"].ToString(); MID = item_1["code"].ToString(); text = item_1["name"].ToString(); if (Leafchildren_2.Count > 0) { leaf = "false"; Leafchildren_1.Add(new { key, MID, leaf, text, searchType, Leafchildren_2 }); } else { leaf = "true"; Leafchildren_1.Add(new { key, MID, leaf, text, searchType }); } } } //↑↑↑一级分类 //↓↓↓根目录 List<object> GchildrenG = new List<object>(); bool expanded = true; text = "产品分类"; key = "All"; string pk_marbasclass = "%"; string success = "True"; if (Leafchildren_1.Count > 0) { leaf = "false"; GchildrenG.Add(new { pk_marbasclass, key, expanded, leaf, text, searchType, Leafchildren_1 }); } else { leaf = "true"; GchildrenG.Add(new { pk_marbasclass, key, expanded, leaf, text, searchType }); } //↑↑↑根目录 //标签名替换 return JObject.Parse(JsonConvert.SerializeObject(new { GchildrenG, success }).Replace("GchildrenG", "children").Replace("Leafchildren_1", "children").Replace("Leafchildren_2", "children").Replace("Leafchildren_3", "children").Replace("Leafchildren_4", "children")); }