bills.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /**
  2. * Created by Mai on 2017/4/17.
  3. */
  4. $(document).ready(function () {
  5. var tempType = 3;
  6. var PostData = function (url, data, successCallback, errorCallback) {
  7. $.ajax({
  8. type:"POST",
  9. url: url,
  10. data: {'data': JSON.stringify(data)},
  11. dataType: 'json',
  12. cache: false,
  13. timeout: 50000,
  14. success: function(result){
  15. if (result.error === 0) {
  16. successCallback(result.data);
  17. } else {
  18. alert('error: ' + result.message);
  19. if (errorCallback) {
  20. errorCallback();
  21. }
  22. }
  23. },
  24. error: function(jqXHR, textStatus, errorThrown){
  25. alert('error ' + textStatus + " " + errorThrown);
  26. if (errorCallback) {
  27. errorCallback();
  28. }
  29. }
  30. });
  31. };
  32. var FormatUpdateData = function (data) {
  33. var updateData = {};
  34. updateData['user_id'] = userID;
  35. updateData['tempType'] = tempType;
  36. updateData['updateData'] = data;
  37. return updateData;
  38. };
  39. var RefreshBaseActn = function (tree) {
  40. var showButton = function (show, btn) {
  41. if (show) {
  42. btn.show();
  43. } else {
  44. btn.hide();
  45. }
  46. };
  47. showButton(tree.selected && tree.selected.canUpLevel(), $('#upLevel'));
  48. showButton(tree.selected && tree.selected.canDownLevel(), $('#downLevel'));
  49. showButton(tree.selected && tree.selected.canUpMove(), $('#upMove'));
  50. showButton(tree.selected && tree.selected.canDownMove(), $('#downMove'));
  51. showButton(tree.selected ? true : false, $('#delete'));
  52. }
  53. var RefreshBillsData = function (datas) {
  54. datas.forEach(function (data) {
  55. bills.forEach(function (billsData) {
  56. if (data.data.ID === billsData.ID) {
  57. $.extend(true, billsData, data.data);
  58. }
  59. })
  60. })
  61. }
  62. var bills;
  63. var tree = idTree.createNew({id: 'ID', pid: 'ParentID', nid: 'NextSiblingID', rootId: -1});
  64. var billsSpread = new GC.Spread.Sheets.Workbook($('#billsSpread')[0], { sheetCount: 1 });
  65. var controller = TREE_SHEET_CONTROLLER.createNew(tree, billsSpread.getActiveSheet(), TEMPLATE_BILLS_SETTING);
  66. GC.Spread.Common.CultureManager.culture("zh-cn");
  67. billsSpread.options.tabStripVisible = false;
  68. billsSpread.options.scrollbarMaxAlign = true;
  69. controller.bind('refreshBaseActn', RefreshBaseActn);
  70. billsSpread.bind(GC.Spread.Sheets.Events.EditEnded, function (sender, info) {
  71. var node = controller.tree.findNode(info.sheet.getTag(info.row, info.col));
  72. var fieldName = controller.setting.cols[info.col].data.field;
  73. var data = {type: 'update', data: {ID: node.getID()}};
  74. data.data[fieldName] = info.editingText;
  75. var updateData = FormatUpdateData([data]);
  76. PostData('/template/bills/updateBillsTemplate', updateData, function (data) {
  77. node.data[fieldName] = info.editingText;
  78. controller.refreshTreeNode([node], false);
  79. }, function () {
  80. controller.refreshTreeNode([node], false);
  81. });
  82. });
  83. billsSpread.bind(GC.Spread.Sheets.Events.ClipboardPasted, function (e, info) {
  84. var node, iRow, iCol, curRow, curCol, datas = [], data, fieldName, updateData;
  85. for (iRow = 0; iRow < info.cellRange.rowCount; iRow ++) {
  86. curRow = info.cellRange.row + iRow;
  87. node = controller.tree.findNode(info.sheet.getTag(curRow, 0));
  88. if (node) {
  89. data = {type: 'update', data: {ID: node.getID()}};
  90. for (iCol = 0; iCol < info.cellRange.colCount; iCol++) {
  91. curCol = info.cellRange.col + iCol;
  92. fieldName = controller.setting.cols[curCol].data.field;
  93. data.data[fieldName] = info.sheet.getText(curRow, curCol);
  94. }
  95. datas.push(data);
  96. }
  97. };
  98. PostData('/template/bills/updateBillsTemplate', FormatUpdateData(datas), function (data) {
  99. RefreshBillsData(data);
  100. controller.showTreeData();
  101. }, function () {
  102. controller.showTreeData();
  103. })
  104. });
  105. PostData('/template/bills/getBillsTemplate', {tempType: tempType}, function (data) {
  106. bills = data;
  107. tree.loadDatas(bills);
  108. controller.showTreeData();
  109. RefreshBaseActn(tree);
  110. }, function () {
  111. controller.showTreeData();
  112. RefreshBaseActn(tree);
  113. });
  114. $('#insert').click(function () {
  115. PostData('/template/bills/getNewBillsTemplateID', {count: 1}, function (data) {
  116. var selected = controller.tree.selected, updateData;
  117. controller.tree.maxNodeID(data.lowID - 1);
  118. controller.tree.rangeNodeID(data.highID);
  119. if (selected) {
  120. updateData = FormatUpdateData(controller.tree.getInsertData(selected.getParentID(), selected.getNextSiblingID()));
  121. } else {
  122. updateData = FormatUpdateData(controller.tree.getInsertData());
  123. }
  124. if (updateData.updateData.length > 0) {
  125. PostData('/template/bills/updateBillsTemplate', updateData, function (data) {
  126. controller.insert();
  127. RefreshBillsData(data);
  128. controller.showTreeData();
  129. });
  130. } else {
  131. alert('新增节点失败, 请重试.');
  132. }
  133. });
  134. });
  135. $('#delete').click(function () {
  136. var selected = controller.tree.selected, updateData;
  137. if (selected) {
  138. updateData = FormatUpdateData(controller.tree.getDeleteData(selected));
  139. PostData('/template/bills/updateBillsTemplate', updateData, function (data) {
  140. controller.delete();
  141. RefreshBillsData(data);
  142. controller.showTreeData();
  143. });
  144. }
  145. });
  146. $('#upLevel').click(function () {
  147. var selected = controller.tree.selected, updateData;
  148. if (selected) {
  149. updateData = FormatUpdateData(selected.getUpLevelData());
  150. PostData('/template/bills/updateBillsTemplate', updateData, function (data) {
  151. controller.upLevel();
  152. RefreshBillsData(data);
  153. controller.showTreeData();
  154. });
  155. }
  156. });
  157. $('#downLevel').click(function () {
  158. var selected = controller.tree.selected, updateData;
  159. if (selected) {
  160. updateData = FormatUpdateData(selected.getDownLevelData());
  161. PostData('/template/bills/updateBillsTemplate', updateData, function (data) {
  162. controller.downLevel();
  163. RefreshBillsData(data);
  164. controller.showTreeData();
  165. });
  166. }
  167. });
  168. $('#upMove').click(function () {
  169. var selected = controller.tree.selected, updateData;
  170. if (selected) {
  171. updateData = FormatUpdateData(selected.getUpMoveData());
  172. PostData('/template/bills/updateBillsTemplate', updateData, function (data) {
  173. controller.upMove();
  174. RefreshBillsData(data);
  175. controller.showTreeData();
  176. });
  177. }
  178. });
  179. $('#downMove').click(function () {
  180. var selected = controller.tree.selected, updateData;
  181. if (selected) {
  182. updateData = FormatUpdateData(selected.getDownMoveData());
  183. PostData('/template/bills/updateBillsTemplate', updateData, function (data) {
  184. controller.downMove();
  185. RefreshBillsData(data);
  186. controller.showTreeData();
  187. });
  188. }
  189. });
  190. });