dinge.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /**
  2. * Created by Syusuke on 2017/3/17.
  3. */
  4. function getQueryString(key){
  5. var reg = new RegExp("(^|&)"+key+"=([^&]*)(&|$)");
  6. var result = window.location.search.substr(1).match(reg);
  7. return result?decodeURIComponent(result[2]):null;
  8. }
  9. //---------------------------------------------------页面跳转
  10. var params = {}
  11. $("#dinge").click(function(){
  12. $(this).attr('href', "/rationLibEditor/rationLib" + "?params=" + JSON.stringify(params))
  13. })
  14. $("#gongliao").click(function(){
  15. $(this).attr('href', "/rationLibEditor/gongliao" + "?params=" + JSON.stringify(params))
  16. });
  17. //----------------------------------------------------页面初始化
  18. $(document).ready(function(){
  19. initParam();
  20. mkRationItemSpread();
  21. });
  22. function initParam(){
  23. var rationLibName = getQueryString("repository");//获取定额库参数
  24. if(rationLibName) {
  25. params.realLibName = rationLibName;
  26. getRationTree();
  27. } else{
  28. params = JSON.parse(getQueryString("params"));
  29. getRationTree();
  30. }
  31. }
  32. function addRootNode(){
  33. //$.ajax({
  34. // type:"POST",
  35. // url:"api/getRationTree",
  36. // data:{"rationLibName": params.realLibName},
  37. // dataType:"json",
  38. // cache:false,
  39. // timeout:20000,
  40. // success:function(result,textStatus,status){
  41. // if(status.status == 200) createRationTree(result.data);//根据返回的全部定额章节对象,构架树。
  42. // else{
  43. // //treeObj = $.fn.zTree.init($("#treeDemo"), setting, zNodes);
  44. // //saveTempTree();
  45. // }
  46. // },
  47. // error:function(err){
  48. // alert(err.responseText.error)
  49. // }
  50. //})
  51. if (treeObj) {
  52. var newNodes = [], isSilent = false;
  53. newNodes.push({ id: 20, pId:-1, nId:-1, name:"新增节点",isParent:true, children:[]});
  54. treeObj.addNodes(null, -1, newNodes, isSilent);
  55. } else {
  56. //treeObj = $.fn.zTree.init($("#treeDemo"), setting, treeArr);
  57. }
  58. }
  59. //---------------------------------------------------初始化章节树界面
  60. function getRationTree(){
  61. $.ajax({
  62. type:"POST",
  63. url:"api/getRationTree",
  64. data:{"rationLibName": params.realLibName},
  65. dataType:"json",
  66. cache:false,
  67. timeout:20000,
  68. success:function(result,textStatus,status){
  69. if(status.status == 200) createRationTree(result.data);//根据返回的全部定额章节对象,构架树。
  70. else{
  71. //treeObj = $.fn.zTree.init($("#treeDemo"), setting, zNodes);
  72. //saveTempTree();
  73. }
  74. },
  75. error:function(err){
  76. alert(err.responseText.error)
  77. }
  78. })
  79. }
  80. //根据返回的节点集合构建树节点
  81. function createRationTree(obj){
  82. var treeArr;
  83. treeArr = makeNodes(obj);
  84. var maxIDNode = obj.reduce(function(a,b){
  85. return (a.id> b.id)?a:b;
  86. });
  87. newCount = maxIDNode.id;
  88. treeObj = $.fn.zTree.init($("#treeDemo"), setting, treeArr);
  89. }
  90. function makeNodes(obj){
  91. var arr=[];
  92. arr = obj.filter(function(x){
  93. return x.parentId==0
  94. })
  95. arr.forEach(function(x){
  96. x.id = x.sectionId;
  97. x.pId = x.parentId;
  98. x.nId = x.nextSiblingId;
  99. x.name = x.name;
  100. x.isParent = true
  101. x.lev = 0
  102. });
  103. arr = sortArray(arr);
  104. for(var i=0;i<arr.length;i++){
  105. var L1 = [];
  106. L1 = obj.filter(function(x){
  107. return x.parentId ==arr[i].id;
  108. });
  109. L1.forEach(function(x){
  110. x.id = x.sectionId;
  111. x.pId = x.parentId;
  112. x.nId = x.nextSiblingId;
  113. x.name = x.name;
  114. x.isParent = true
  115. x.lev = 1
  116. });
  117. L1=sortArray(L1);
  118. for(var j=0; j<L1.length;j++){
  119. var L2 = [];
  120. L2 =obj.filter(function(x){
  121. return x.parentId == L1[j].id;
  122. })
  123. L2.forEach(function(x){
  124. x.id = x.sectionId;
  125. x.pId = x.parentId;
  126. x.nId = x.nextSiblingId;
  127. x.name = x.name;
  128. x.isParent = false;
  129. x.lev = 2
  130. })
  131. L2=sortArray(L2)
  132. L1[j].children = L2;
  133. }
  134. arr[i].children = L1;
  135. }
  136. return arr;
  137. }
  138. function sortArray(arr){
  139. var a = [];
  140. for(var i=0;i<arr.length;i++)
  141. if (arr[i].nextSiblingId == -1)
  142. a.push(arr[i])
  143. for(; a.length < arr.length;){
  144. for(var j=0;j<arr.length;j++) {
  145. if (arr[j].nextSiblingId == a[0].sectionId)
  146. {a.unshift(arr[j]); break;}
  147. }
  148. }
  149. return a ;
  150. }
  151. //新建的定额库保存模板节点
  152. function saveTempTree(){
  153. var N = []
  154. for(i=0;i<zNodes.length;i++){
  155. var node ={}
  156. node.sectionId = zNodes[i].id;
  157. node.parentId = zNodes[i].pId;
  158. node.nextSiblingId = zNodes[i].nId;
  159. node.name =zNodes[i].name;
  160. N.push(node);
  161. }
  162. var rationTempTree = JSON.stringify(N)
  163. $.ajax({
  164. type:"POST",
  165. url:"api/saveTempRationTree",
  166. data:{"rationName":rationLibName,"rationTempTree":rationTempTree},
  167. dataType:"json",
  168. cache:false,
  169. timeout:1000,
  170. success:function(result,textStatus,status){},
  171. error:function(){}
  172. })
  173. }
  174. //--------------------------------------------------------树处理事件
  175. var newCount = 13;
  176. //新增树节点
  177. function addHoverDom(treeId, treeNode) {
  178. var sObj = $("#" + treeNode.tId + "_span");
  179. if (treeNode.editNameFlag || $("#addBtn_"+treeNode.tId).length>0||(treeNode.level==2)) return;
  180. var addStr = "<span class='button add' id='addBtn_" + treeNode.tId
  181. + "' title='add node' onfocus='this.blur();'></span>";
  182. sObj.after(addStr);
  183. var btn = $("#addBtn_"+treeNode.tId);
  184. if (btn) btn.bind("click", function(){
  185. var zTree = $.fn.zTree.getZTreeObj("treeDemo");
  186. if((treeNode.level==0)){
  187. var newNode = zTree.addNodes(treeNode, {id:(++newCount), pId:treeNode.id,nId:-1,isParent:true, name:"请输入章节名称",children:[]});
  188. }
  189. else{
  190. var newNode = zTree.addNodes(treeNode, {id:(++newCount), pId:treeNode.id,nId:-1,isParent:false, name:"请输入章节名称"});
  191. }
  192. saveNewSection(newNode[0]);
  193. var pnode = newNode[0].getPreNode()
  194. if(pnode){
  195. pnode.nId = newNode[0].id;
  196. saveNewSection(pnode);
  197. }
  198. return false;
  199. });
  200. };
  201. //保存新增的节点
  202. function saveNewSection(n){
  203. var sec={};
  204. sec.sectionId = n.id;
  205. sec.parentId = n.pId;
  206. sec.nextSiblingId = n.nId;
  207. sec.name = n.name;
  208. var section = JSON.stringify(sec);
  209. $.ajax({
  210. type:"POST",
  211. url:"api/addSection",
  212. data:{"rationLibName":params.realLibName,"rationSection":section},
  213. dataType:"json",
  214. cache:false,
  215. timeout:1000,
  216. success:function(result,textStatus,status){
  217. },
  218. error:function(){
  219. }
  220. })
  221. }
  222. //编辑树节点事件(添加节点到数据库)
  223. function onRename(e, treeId, treeNode, isCancel) {
  224. saveNewSection(treeNode);
  225. }
  226. function onRemove(e, treeId, treeNode) {
  227. var id = treeNode.id;
  228. var pNodes = treeNode.getParentNode().children;
  229. for(var i=0;i<pNodes.length;i++){
  230. if(pNodes[i].nId==id){
  231. pNodes[i].nId = -1;
  232. saveNewSection(pNodes[i]);
  233. }
  234. }
  235. $.ajax({
  236. type:"POST",
  237. url:"api/deleteSection",
  238. data:{"rationLibName":params.realLibName,"sectionID": treeNode.id},
  239. dataType:"json",
  240. cache:false,
  241. timeout:1000,
  242. success:function(result,textStatus,status){
  243. //if(result){
  244. // caseDeleteTreeNode(result.data)
  245. //}
  246. },
  247. error:function(){
  248. }
  249. })
  250. removeSection(id)
  251. $("#rationTbody").html("");
  252. $("#rationGLJTbody").html("");
  253. }
  254. //--------------------------------------------------------定额spreadjs
  255. var spSetting_ration = {
  256. spType:"Ration",
  257. header:[
  258. {headerName:"编码",headerWidth:120,data:"rationCode"},
  259. {headerName:"名称",headerWidth:400,data:"rationName"},
  260. {headerName:"单位",headerWidth:120,data:"unit"},
  261. {headerName:"基价",headerWidth:120,data:"basePrice"},
  262. {headerName:"显示名称(以%s表示参数)",headerWidth:450,data:"caption"},
  263. {headerName:"取费专业",headerWidth:120,data:"feeType"}
  264. ],
  265. view:{
  266. comboBox:[
  267. {row:-1,col:2,rowCount:-1,colCount:1}
  268. ],
  269. lockedCells:[
  270. {row:-1,col:3,rowCount:-1, colCount:1}
  271. ]
  272. }
  273. };
  274. var spSetting_rationGLJ = {
  275. spType:"RationGLJ",
  276. header:[
  277. {headerName:"编码",headerWidth:160},
  278. {headerName:"名称",headerWidth:400},
  279. {headerName:"单位",headerWidth:160},
  280. {headerName:"单位基价",headerWidth:160},
  281. {headerName:"定额消耗",headerWidth:160},
  282. {headerName:"类型",headerWidth:160},
  283. {headerName:"操作",headerWidth:130}
  284. ],
  285. view:{
  286. comboBox:[],
  287. lockedCells:[
  288. {row:-1,col:1,rowCount:-1, colCount:1},
  289. {row:-1,col:2,rowCount:-1, colCount:1},
  290. {row:-1,col:3,rowCount:-1, colCount:1},
  291. {row:-1,col:5,rowCount:-1, colCount:1},
  292. {row:-1,col:6,rowCount:-1, colCount:1}
  293. ]
  294. }
  295. };
  296. function mkRationItemSpread(){
  297. var rationSpread = $.fn.Spread.init($("#rationItemsSheet"),spSetting_ration);
  298. var rationGLJSpread = $.fn.Spread.init($("#rationGLJSheet"),spSetting_rationGLJ);
  299. }