gljSelect.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /**
  2. * Created by Zhong on 2017/8/25.
  3. */
  4. let gljSelOprObj = {
  5. distTypeTree: null,
  6. parentNodeIds: {},
  7. treeObj:null,
  8. rootNode: null,//分类树根节点
  9. workBook: null,
  10. selectedList: [],//选中的工料机
  11. setting: {
  12. header: [
  13. {headerName:"选择", headerWidth: 40, dataCode: "select", hAlign: "center", vAlign: "center"},
  14. {headerName:"编码",headerWidth:80,dataCode:"code", dataType: "String", formatter: "@", hAlign: "left", vAlign: "center"},
  15. {headerName:"名称",headerWidth:120,dataCode:"name", dataType: "String", hAlign: "left", vAlign: "center"},
  16. {headerName:"规格型号",headerWidth:80,dataCode:"specs", dataType: "String", hAlign: "center", vAlign: "center"},
  17. {headerName:"单位",headerWidth:80,dataCode:"unit", dataType: "String", hAlign: "center", vAlign: "center"},
  18. {headerName:"单价",headerWidth:80,dataCode:"basePrice", dataType: "Number", formatter: "0.00", hAlign: "right", vAlign: "center"},
  19. {headerName:"类型",headerWidth:80,dataCode:"gljType", dataType: "String", hAlign: "center", vAlign: "center"}
  20. ]
  21. },
  22. setProp: function (prop, value, gljList) {
  23. for(let i = 0, len = gljList.length; i < len; i++){
  24. gljList[i][prop] = value;
  25. }
  26. },
  27. sortGlj: function(gljList) {
  28. gljList.sort(function(a, b){
  29. let rst = 0;
  30. if (a.code > b.code) rst = 1
  31. else if (a.code < b.code) rst = -1;
  32. return rst;
  33. });
  34. },
  35. switchToGljId: function (gljList) {
  36. for(let glj of gljList){
  37. glj.gljId = glj.ID;
  38. delete glj.ID;
  39. }
  40. },
  41. initClassTree: function (type, treeData) {
  42. let me = this;
  43. if (me.treeObj) {
  44. me.treeObj.destroy();
  45. me.parentNodeIds = {};
  46. }
  47. zTreeHelper.createTree(treeData, gljSelTreeOprObj.setting, "selGljTree", me);
  48. let rootNode = me.treeObj.getNodes()[0];
  49. if(rootNode && rootNode.isParent && rootNode.isFirstNode){
  50. me.rootNode = rootNode;
  51. }
  52. if(me.rootNode){
  53. me.treeObj.selectNode(me.rootNode);
  54. if ((me.stdGljList && me.stdGljList.length > 0) ||
  55. (me.complementaryGljList && me.complementaryGljList.length > 0)) {
  56. gljSelTreeOprObj.setting.callback.onClick(null, 'selGljTree', me.rootNode);
  57. }
  58. }
  59. },
  60. buildSheet: function (container) {
  61. let me = gljSelOprObj;
  62. me.workBook = sheetCommonObj.buildSheet(container, me.setting, 30);
  63. sheetCommonObj.spreadDefaultStyle(me.workBook);
  64. me.workBook.getSheet(0).setColumnWidth(0, 20, GC.Spread.Sheets.SheetArea.rowHeader);
  65. me.workBook.getSheet(0).setFormatter(-1, 1, "@", GC.Spread.Sheets.SheetArea.viewport);
  66. me.workBook.getSheet(0).bind(GC.Spread.Sheets.Events.EditStarting, me.onCellEditStart);
  67. me.workBook.getSheet(0).bind(GC.Spread.Sheets.Events.ClipboardPasting, me.onClipboardPasting);
  68. me.workBook.bind(GC.Spread.Sheets.Events.ButtonClicked, me.onButtonClicked);//复选框点击事件
  69. me.bindBtnOpr($('#gljSelY'));
  70. },
  71. setGLJItems: function(stdGLJs, complementaryGLJs) {
  72. this.stdGljList = stdGLJs;
  73. //兼容多单价,计算补充定额价格时,套多单价人材机的时候,默认取第一个价格
  74. for(let sGlj of this.stdGljList){
  75. if(sGlj.priceProperty && typeof sGlj.priceProperty.price1 !== 'undefined'){
  76. sGlj.basePrice = sGlj.priceProperty.price1;
  77. }
  78. }
  79. this.complementaryGljList = complementaryGLJs;
  80. this.switchToGljId(this.stdGljList);
  81. this.switchToGljId(this.complementaryGljList);
  82. this.setProp('type', 'std', this.stdGljList);
  83. this.setProp('type', 'complementary', this.complementaryGljList);
  84. this.sortGlj(this.stdGljList);
  85. this.sortGlj(this.complementaryGljList);
  86. },
  87. initLibOptions: function (libData) {
  88. const html = libData.reduce((acc, lib) => acc += `<option ${lib.isDefault ? 'selected="selected"' : ''} value="${lib.gljLibId}">${lib.name}</option>`, '');
  89. $('#glj-lib-select').html(html);
  90. },
  91. // 初始化选择页面
  92. initView: async function (gljLibId, isInitial) {
  93. try {
  94. $.bootstrapLoading.start();
  95. const { libData, treeData, distTypeTree, stdGLJ, complementaryGLJs } = await this.getViewData(gljLibId);
  96. if (isInitial) {
  97. this.initLibOptions(libData);
  98. $('#gljSearchKeyword').val('');
  99. setTimeout(() => $('#selGlj').modal('show'), 200);
  100. }
  101. if (!this.workBook) {
  102. this.buildSheet($('#gljSelSheet')[0]);
  103. }
  104. this.distTypeTree = distTypeTree;
  105. this.setGLJItems(stdGLJ, complementaryGLJs);
  106. this.selectedList = [].concat(rationGLJOprObj.cache['_GLJ_' + rationGLJOprObj.currentRationItem.ID]);
  107. this.showGljList = [];
  108. this.setShowGljList(this.stdGljList, true);
  109. if (treeData.std.length) {
  110. this.initClassTree('std', treeData.std);
  111. } else if (treeData.comple.length) {
  112. this.initClassTree('comple', treeData.comple);
  113. } else {
  114. throw '没有有效的分类树。';
  115. }
  116. this.filterDatasAndShow();
  117. } catch (err) {
  118. console.log(err);
  119. alert(err);
  120. } finally {
  121. $.bootstrapLoading.end();
  122. }
  123. },
  124. getViewData: async function (gljLibId) {
  125. const url = `/rationGlj/getGLJData/${commonConstants.COMPILATION}/${gljLibId}/true`;
  126. const data = await ajaxGet(url);
  127. return data.datas;
  128. },
  129. onClipboardPasting: function (sender, args) {
  130. args.cancel = true;
  131. },
  132. onCellEditStart: function (sender, args) {
  133. args.cancel = true;
  134. },
  135. onButtonClicked: function (sender, args) {
  136. let me = gljSelOprObj;
  137. let val = args.sheet.getValue(args.row, args.col);
  138. let thisGlj = me.currentCache[args.row];
  139. thisGlj.isChecked = val;
  140. if(args.sheet.isEditing()){
  141. args.sheet.endEdit(true);
  142. }
  143. else{
  144. //设置选中
  145. if(val === true){
  146. let isExist = false;
  147. for(let i = 0, len = me.selectedList.length; i < len; i++){
  148. if(me.selectedList[i].gljId === thisGlj.gljId){
  149. isExist = true;
  150. break;
  151. }
  152. }
  153. if(!isExist){
  154. thisGlj.consumeAmt = 0;
  155. me.selectedList.push(_.cloneDeep(thisGlj));
  156. }
  157. }
  158. else if(val === false){
  159. for(let i = 0, len = me.selectedList.length; i < len; i++){
  160. if(me.selectedList[i].gljId === thisGlj.gljId){
  161. me.selectedList.splice(i, 1);
  162. break;
  163. }
  164. }
  165. }
  166. }
  167. },
  168. setShowGljList: function (gljList, clearChecked) {
  169. //初始为所有工料机
  170. let me = this;
  171. let curRationGlj = rationGLJOprObj.cache['_GLJ_' + rationGLJOprObj.currentRationItem.ID];
  172. for(let i = 0; i < gljList.length; i++){
  173. //去除与已添加的组成物重复的条目
  174. let isExist = false;
  175. for(let j = 0; j < curRationGlj.length; j++){
  176. if(curRationGlj[j].gljId === gljList[i].gljId){
  177. isExist = true;
  178. break;
  179. }
  180. }
  181. if(!isExist){
  182. if(clearChecked){
  183. gljList[i].isChecked = false;
  184. }
  185. }
  186. else {
  187. gljList[i].isChecked = true;
  188. }
  189. me.showGljList.push(gljList[i]);
  190. }
  191. },
  192. filterDatasAndShow: function () {
  193. const gljLib = $('#glj-lib-select').val();
  194. let me = gljSelOprObj;
  195. //选择改变,数据重新筛选显示
  196. me.showGljList = [];
  197. if (gljLib === commonConstants.COMPLEMENTARY_LIB) {
  198. me.setShowGljList(me.complementaryGljList);
  199. } else {
  200. me.setShowGljList(me.stdGljList);
  201. }
  202. //搜索匹配
  203. let searchStr = $('#gljSearchKeyword').val();
  204. if(searchStr && searchStr.trim() != ''){
  205. let reg = new RegExp(searchStr);
  206. me.showGljList = _.filter(me.showGljList, function (data) {
  207. return reg.test(data.code) || reg.test(data.name);
  208. });
  209. }
  210. //me.sortGlj(me.showGljList);
  211. //重新显示
  212. me.showGljItems(me.showGljList, me.gljCurTypeId);
  213. if (me.currentOprParent = 1) {
  214. if(me.parentNodeIds["_pNodeId_" + me.gljCurTypeId]){
  215. me.currentCache = me.getParentCache(me.parentNodeIds["_pNodeId_" + me.gljCurTypeId]);
  216. }
  217. else{
  218. me.currentCache = [];
  219. }
  220. } else {
  221. me.currentCache = me.getCache();
  222. }
  223. },
  224. getParentCache: function (nodes) {
  225. let me = gljSelOprObj, rst = [];
  226. for(let i = 0; i < me.showGljList.length; i++){
  227. if(nodes.indexOf(me.showGljList[i].gljClass) !== -1){
  228. rst.push(me.showGljList[i]);
  229. }
  230. }
  231. rst.sort(function (a, b) {
  232. let rst = 0;
  233. if(a.code > b.code) rst = 1;
  234. else if(a.code < b.code)rst = -1;
  235. return rst;
  236. });
  237. return rst;
  238. },
  239. getCache: function() {
  240. let me = gljSelOprObj, rst = [];
  241. for (let i = 0; i < me.showGljList.length; i++) {
  242. if (me.showGljList[i].gljClass == me.gljCurTypeId) {
  243. rst.push(me.showGljList[i]);
  244. }
  245. }
  246. return rst;
  247. },
  248. showGljItems: function(data, type) {
  249. let me = gljSelOprObj;
  250. if (me.workBook) {
  251. let cacheSection = [];
  252. let pArr = me.parentNodeIds["_pNodeId_" + type];
  253. for (let i = 0; i < data.length; i++) {
  254. if (pArr && pArr.indexOf(data[i].gljClass) >= 0) {
  255. cacheSection.push(data[i]);
  256. } else if (type == data[i].gljClass) {
  257. cacheSection.push(data[i]);
  258. }
  259. }
  260. //sheetCommonObj.cleanSheet(me.workBook.getSheet(0), me.setting, -1);
  261. sheetsOprObj.showDataForGljSel(me.workBook.getSheet(0), me.setting, cacheSection, rationGLJOprObj.distTypeTree);
  262. me.workBook.getSheet(0).setRowCount(cacheSection.length);
  263. cacheSection = null;
  264. }
  265. },
  266. //组成物窗口按钮操作
  267. bindBtnOpr: function (conf) {//确定、取消、关闭按钮
  268. let me = gljSelOprObj, that = rationGLJOprObj;
  269. conf.click(function () {
  270. that.cache['_GLJ_' + that.currentRationItem.ID] = me.selectedList;
  271. that.updateRationItem(function () {
  272. that.sheet.getParent().focus();
  273. sheetCommonObj.cleanData(that.sheet, that.setting, -1);
  274. that.showGljItems(that.currentRationItem.ID);
  275. $('#selGlj').modal('hide');
  276. });
  277. });
  278. }
  279. };
  280. let gljSelTreeOprObj = {
  281. setting: {
  282. view: {
  283. expandSpeed: "",
  284. selectedMulti: false
  285. },
  286. edit: {
  287. enable: false,
  288. editNameSelectAll: true,
  289. showRemoveBtn: true,
  290. showRenameBtn: true,
  291. removeTitle: "删除节点",
  292. renameTitle: "更改名称"
  293. },
  294. data: {
  295. keep: {
  296. parent:true,
  297. leaf:true
  298. },
  299. key: {
  300. children: "items",
  301. name: "Name"
  302. },
  303. simpleData: {
  304. enable: false,
  305. idKey: "ID",
  306. pIdKey: "ParentID",
  307. rootPId: -1
  308. }
  309. },
  310. callback:{
  311. onClick: function(event,treeId,treeNode) {
  312. let me = gljSelOprObj, gljTypeId = treeNode.ID;
  313. if(me.gljCurTypeId !== treeNode.ID){
  314. me.gljCurTypeId = treeNode.ID;
  315. if (me.parentNodeIds["_pNodeId_" + treeNode.ID]) {
  316. me.currentOprParent = 1;
  317. me.currentCache = me.getParentCache(me.parentNodeIds["_pNodeId_" + treeNode.ID]);
  318. } else {
  319. me.currentCache = me.getCache();
  320. }
  321. }
  322. me.showGljItems(me.showGljList, gljTypeId);
  323. }
  324. }
  325. }
  326. };
  327. $(document).ready(function () {
  328. $('#glj-lib-select').change(function () {
  329. const gljLibId = $(this).val();
  330. gljSelOprObj.initView(gljLibId, false);
  331. });
  332. /* $('#gljSearchKeyword').change(function () {
  333. gljSelOprObj.filterDatasAndShow();
  334. });
  335. $('#gljSearchKeyword').bind('keypress', function (e) {
  336. if(e.keyCode === 13){
  337. $(this).blur();
  338. return false;
  339. }
  340. }); */
  341. $('#gljSearchKeyword').on('keyup', _.debounce(function () {
  342. gljSelOprObj.filterDatasAndShow();
  343. }, 300));
  344. $('#selGlj').on('shown.bs.modal', function () {
  345. if (gljSelOprObj.workBook) {
  346. gljSelOprObj.workBook.refresh();
  347. }
  348. });
  349. });