GetDicData.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System.Collections.Generic;
  2. using System.Data;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Web;
  6. namespace SiteCore.Handler
  7. {
  8. public class GetDicData : BaseHandler, IHttpHandler
  9. {
  10. public void ProcessRequest(HttpContext context)
  11. {
  12. context.Response.ContentType = "application/jason";
  13. if (UrlParmsCheck("pType"))
  14. {
  15. string pType = GetString("pType").ToLower();
  16. string selId = GetString("selId");
  17. int pId = GetInt("pId");
  18. int cur = GetInt("cur");
  19. DataTable dt = null;
  20. switch (pType)
  21. {
  22. case "industry":
  23. {
  24. dt = WebCache.GetCacheDicData(DicType.Industry, "", "industry_cache");
  25. break;
  26. }
  27. case "trade":
  28. {
  29. dt = WebCache.GetCacheDicData(DicType.TradeType, "", "trade_cache");
  30. break;
  31. }
  32. case "goodstype":
  33. {
  34. dt = WebCache.GetCacheDicData(DicType.GoodsType, "", "goodstype_cache");
  35. break;
  36. }
  37. case "producttype":
  38. {
  39. int mId = GetInt("mId");
  40. dt = WebCache.GetDicData(DicType.ProductType, "MerchantID=" + mId);
  41. break;
  42. }
  43. }
  44. if (dt == null) { returnErrorMsg("找不到数据,访问发生错误"); return; }
  45. IList<string> selIds = new List<string>();
  46. if (selId != "")//加载己选中的值
  47. {
  48. DataRow[] tmpdrs = dt.Select("ID=" + selId);
  49. if (tmpdrs != null && tmpdrs.Length > 0)
  50. {
  51. string path = tmpdrs[0]["Path"].ToString();
  52. path = path.TrimStart('|') + selId;
  53. selIds = path.Split('|');
  54. }
  55. else
  56. {
  57. selId = "";
  58. }
  59. }
  60. DataView dv = new DataView(dt);
  61. if (dv.Count > 0)
  62. {
  63. int idx = 0;
  64. IList<string> resultList = new List<string>();
  65. string tag = "";
  66. //如果不是第二次触发&加载己有的值
  67. //if (pId == 0 && selIds.Count > 0) pId = Convert.ToInt32(selIds[0]);
  68. TreeBind(dt, pId, ref resultList, ref tag, selIds, idx);
  69. //if (selIds.Count > 0) tag = string.Join(",", selIds.ToArray()) + "," + selId;
  70. //sel-各级选中的ID,cur-当前选择的第几级触发事件
  71. returnSuccess("{" + string.Format("{0},\"sel\":\"{1}\",\"cur\":{2},\"selId\":\"{3}\"", string.Join(",", resultList.ToArray()), tag.TrimEnd(','), cur, selId) + "}");
  72. }
  73. return;
  74. }
  75. returnErrorMsg("数据读取出错");
  76. }
  77. private void TreeBind(DataTable dt, object parentId, ref IList<string> resultList, ref string tag, IList<string> selIds, int idx)
  78. {
  79. DataView dv = new DataView(dt);
  80. if (parentId.ToString() == "") return;
  81. dv.RowFilter = "ParentID=" + parentId;
  82. string tmp = string.Empty;
  83. if (dv.Count > 0)
  84. {
  85. if (selIds.Count > idx)
  86. {
  87. tmp = selIds[idx];//下一次加载的ParentID
  88. tag += selIds[idx] + ",";//当前默认选中
  89. resultList.Add(string.Format("\"sel{0}\":[{1}]", selIds[idx], DvToJson(dv)));
  90. }
  91. else
  92. {
  93. tmp = dv[0]["ID"].ToString();//下一次加载的ParentID
  94. tag += tmp + ",";//当前默认选中
  95. resultList.Add(string.Format("\"sel{0}\":[{1}]", tmp, DvToJson(dv)));
  96. }
  97. TreeBind(dt, tmp, ref resultList, ref tag, selIds, idx + 1);
  98. }
  99. }
  100. private string DvToJson(DataView dv)
  101. {
  102. StringBuilder json = new StringBuilder();
  103. foreach (DataRowView drv in dv)
  104. {
  105. json.Append("{");
  106. json.AppendFormat("\"name\":\"{0}\",\"value\":\"{1}\" ", drv["Name"], drv["ID"]);
  107. json.Append("},");
  108. }
  109. return json.ToString().TrimEnd(',');
  110. }
  111. }
  112. }