idealDic.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*----------------------------------------------------------------------------\
  2. | IdealDic |
  3. |-----------------------------------------------------------------------------|
  4. | Created by dongping_li |
  5. |-----------------------------------------------------------------------------|
  6. | 缓存系统字典项的处理文件 |
  7. | 系统如果字典项目特别多,可以使用此处提供的功能,在系统登录后将字典项缓存到页面内, |
  8. | 在具体页面,直接调用此处提供的功能取得需要的字典即可. |
  9. |-----------------------------------------------------------------------------|
  10. | 2007-01-19 | Original Version Posted. |
  11. |-----------------------------------------------------------------------------|
  12. | Created 2007-01-19 | All changes are in the log above. | Updated 2008-07-09 |
  13. \----------------------------------------------------------------------------*/
  14. /**
  15. * 页面字典项的容器
  16. */
  17. var dicContainer={
  18. //初始化方法
  19. init:function(){
  20. if(dicInit.length==0) return;
  21. for(var i=0;i<dicInit.length;i++)
  22. {
  23. var dic=dicInit[i];
  24. var dicObj=new IdealDic(dic .id,dic.name);
  25. if(dic.child.length==0) return;
  26. for(var j=0;j<dic.child.length;j++){
  27. var dicItemObj=dic.child[j];
  28. dicObj.add(new dicItem(dicItemObj[0],dicItemObj[1]),dic.child);
  29. }
  30. }
  31. },
  32. //得到某一字典中字典项的名称
  33. getDicItemName:function(dicCode,itemCode){
  34. var dicObj=dicContainer[dicCode].children;
  35. for(var i=0;i<dicObj.length;i++){
  36. var itemObj=dicObj[i];
  37. if(itemObj.code==itemCode){
  38. return itemObj.name;
  39. }
  40. }
  41. }
  42. };
  43. /*
  44. * 存储字典项目键值对的容器
  45. */
  46. var dicNameContainer={};
  47. /**
  48. * 字典初始化数据
  49. */
  50. var dicInit=dicConfig.dicInitData;
  51. /**
  52. * 字典类
  53. * @param:id 数据库字典表中的内码
  54. * @param:name 字典名称
  55. */
  56. function IdealDic(id,name,selectedCode)
  57. {
  58. this.id=id;
  59. this.name=name;
  60. this.selectedCode=selectedCode;
  61. this.children=[];
  62. this.childrenJson=null;
  63. dicContainer[this.id]=this;
  64. }
  65. /**
  66. * 增加一个字典项目
  67. * @param:item 字典项目实例
  68. */
  69. IdealDic.prototype.add=function(item,itemJson)
  70. {
  71. item.parentNode=this;
  72. this.children[this.children.length]=item;
  73. this.childrenJson=itemJson;
  74. }
  75. /**
  76. * 设置选中字典项
  77. * @param selectedCode 选中字典项的code
  78. */
  79. IdealDic.prototype.setSelected=function(selectedCode)
  80. {
  81. this.selectedCode=selectedCode;
  82. }
  83. /**
  84. * 输出字典项目
  85. *
  86. */
  87. IdealDic.prototype.toString=function(selectName)
  88. {
  89. var str="";
  90. if(this.children.length > 0)
  91. {
  92. var sb=[];
  93. for(var i=0;i<this.children.length;i++)
  94. {
  95. sb[i]=this.children[i].toString(selectName);
  96. }
  97. return sb.join("");
  98. }else{
  99. //update by hongxin_ma 如果该字典存在上级编码,但是没有下级数据,默认加一个option
  100. return "<option value=\"-999\">无类别</option>";
  101. }
  102. }
  103. /**
  104. * 输出字典项目
  105. *
  106. */
  107. IdealDic.prototype.toQueryString=function(selectName)
  108. {
  109. var str="";
  110. if(this.children.length > 0)
  111. {
  112. var sb=[];
  113. var queryAll="<option value=\"\">全部</option>";
  114. for(var i=0;i<this.children.length;i++)
  115. {
  116. sb[i]=this.children[i].toString(selectName);
  117. }
  118. return queryAll+sb.join("");
  119. }else{
  120. //update by hongxin_ma 如果该字典存在上级编码,但是没有下级数据,默认加一个option
  121. return "<option value=\"-999\">无类别</option>";
  122. }
  123. }
  124. /**
  125. * 输出字典项目
  126. *
  127. */
  128. IdealDic.prototype.toEachChild=function(selectName)
  129. {
  130. var str="";
  131. if(this.children.length > 0)
  132. {
  133. var sb=[];
  134. var queryAll="";
  135. for(var i=0;i<this.children.length;i++)
  136. {
  137. sb[i]=this.children[i].toChild(selectName);
  138. }
  139. return sb;
  140. }else{
  141. //update by hongxin_ma 如果该字典存在上级编码,但是没有下级数据,默认加一个option
  142. return "";
  143. }
  144. }
  145. /**
  146. * 字典项目类
  147. * @param:code 字典项目代码
  148. * @param:name 字典项目名称
  149. */
  150. function dicItem(code,name)
  151. {
  152. dicNameContainer[code]=name;
  153. this.code=code;
  154. this.name=name;
  155. }
  156. dicItem.prototype.toString=function(selectName)
  157. {
  158. var str="<option value=\"" + this.code +"\" ";
  159. if(this.parentNode.selectedCode!=null||this.name==selectName)
  160. {
  161. if(this.code==this.parentNode.selectedCode||this.name==selectName)
  162. {
  163. str+="selected=\"true\" ";
  164. }
  165. }
  166. str+=">"+ this.name +"</option>";
  167. return str;
  168. };
  169. dicItem.prototype.toChild=function(selectName)
  170. {
  171. return this.name;
  172. };
  173. dicItem.prototype.toValue=function() {
  174. return this.name;
  175. };
  176. IdealDic.prototype.toValue=function(selectName)
  177. {
  178. var sb='';
  179. for(var i=0;i<this.children.length;i++)
  180. {
  181. if(selectName == this.children[i].code){
  182. sb=this.children[i].toValue();
  183. }
  184. }
  185. return sb;
  186. };