FormUtil.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. Ext.define('saas.util.FormUtil', {
  2. statics: {
  3. setItems: function(form) {
  4. let me = this,
  5. defaultItems = form.defaultItems;
  6. let items = [];
  7. items = me.applyItemsGroup(defaultItems || []);
  8. items = me.initItems(items);
  9. form.configItems = items;
  10. items = me.applyDefaultItems(form, items);
  11. form.removeAll();
  12. form.addItems(items);
  13. form.fireEvent('afterSetItems', form, items);
  14. me.loadData(form);
  15. },
  16. applyItemsGroup: function(items) {
  17. let groups = [],
  18. groupCount = 0,
  19. newItems = [];
  20. Ext.Array.each(items, function(it, index) {
  21. let item = Object.assign({}, it),
  22. groupName = item.group;
  23. if(!!groupName) {
  24. let idx = groups.findIndex(function(g) {
  25. return g.title == groupName;
  26. }),group;
  27. if(idx == -1) {
  28. group = {
  29. title: groupName,
  30. count: 1
  31. };
  32. groups.push(group);
  33. }else {
  34. group = groups[idx];
  35. group.count++;
  36. }
  37. }
  38. newItems.push(item);
  39. });
  40. Ext.Array.sort(newItems, function(a, b) {
  41. let gs = groups.concat([{
  42. title: '_nogroup'
  43. }]);
  44. a.group = a.group || '_nogroup';
  45. let v1 = gs.findIndex(function(g) {
  46. return g.title == a.group;
  47. })
  48. let v2 = gs.findIndex(function(g) {
  49. return g.title == b.group;
  50. })
  51. return v1 - v2;
  52. });
  53. Ext.Array.each(groups, function(g) {
  54. let idx = newItems.findIndex(function(i) {
  55. return i.group == g.title;
  56. });
  57. g.index = idx;
  58. });
  59. Ext.Array.each(groups, function(group, index) {
  60. let formIndex = group.index;
  61. delete group.index;
  62. Ext.Array.insert(newItems, formIndex + index, [{
  63. xtype: 'separatefield',
  64. name: 'group' + (++groupCount),
  65. html: group.title,
  66. fieldLabel: group.title || '分组' + groupCount
  67. }]);
  68. });
  69. return newItems;
  70. },
  71. initItems: function(items) {
  72. let itemCount = detailCount = 1, newItems = [];
  73. Ext.Array.each(items, function(it, i) {
  74. let item = Object.assign({}, it);
  75. if(item.xtype == 'detailGridField') {
  76. let columns = item.columns,
  77. colCount = 1;
  78. Ext.Array.each(columns, function(col, j) {
  79. if((col.hidden || col.width == 0 || !col.dataIndex) && (!col.hasOwnProperty('initHidden') || col.initHidden)) {
  80. Ext.applyIf(col, {
  81. index: -1,
  82. initHidden: true
  83. });
  84. }else {
  85. Ext.applyIf(col, {
  86. text: '',
  87. hidden: false,
  88. index: colCount++,
  89. allowBlank: true,
  90. width: 100,
  91. initHidden: false
  92. });
  93. }
  94. });
  95. if(!columns[columns.length - 1].flex) {
  96. columns.push({
  97. dataIndex: '',
  98. initHidden: true,
  99. flex: 1,
  100. allowBlank: true
  101. });
  102. }
  103. Ext.applyIf(item, {
  104. allowBlank: false,
  105. columnWidth: 1,
  106. gname: 'detail' + detailCount,
  107. fieldLabel: '从表' + (detailCount++),
  108. });
  109. }else if(item.xtype == 'hidden') {
  110. Ext.applyIf(item, {
  111. fieldLabel: '',
  112. hidden: true,
  113. initHidden: true,
  114. });
  115. }else if(item.xtype == 'separatefield') {
  116. Ext.applyIf(item, {
  117. fieldLabel: item.html,
  118. columnWidth: 1,
  119. });
  120. }else {
  121. Ext.applyIf(item, {
  122. fieldLabel: '',
  123. columnWidth: 0.25,
  124. });
  125. }
  126. if(item.hidden) {
  127. if(item.initHidden || !item.hasOwnProperty('initHidden')) {
  128. Ext.applyIf(item, {
  129. index: -1,
  130. initHidden: true
  131. });
  132. }else {
  133. Ext.applyIf(item, {
  134. index: itemCount++,
  135. initHidden: false
  136. });
  137. }
  138. }else {
  139. Ext.applyIf(item, {
  140. index: itemCount++,
  141. initHidden: false
  142. });
  143. }
  144. Ext.applyIf(item, {
  145. name: 'item' + i,
  146. hidden: false,
  147. allowBlank: true,
  148. group: undefined,
  149. });
  150. newItems.push(item);
  151. });
  152. Ext.Array.sort(newItems, function(a, b) {
  153. return a.index - b.index;
  154. });
  155. return newItems;
  156. },
  157. /**
  158. * 处理formitems的一些默认配置
  159. */
  160. applyDefaultItems: function(form, items) {
  161. let me = this,
  162. formModel = form.getViewModel();
  163. Ext.Array.each(items, function(item) {
  164. // 设置必填
  165. if(item.allowBlank==false){
  166. // TODO 需要判断类型
  167. item.beforeLabelTextTpl = "<font color=\"red\" style=\"position:relative; top:2px;right:2px; font-weight: bolder;\">*</font>";
  168. }
  169. if(item.xtype == 'textfield') {
  170. Ext.applyIf(item, {
  171. maxLength: 50
  172. });
  173. }else if(item.xtype == 'datefield') {
  174. Ext.applyIf(item, {
  175. editable: false,
  176. format: 'Y-m-d'
  177. });
  178. }else if(item.xtype == 'numberfield') {
  179. Ext.applyIf(item, {
  180. hideTrigger: true, // 隐藏trigger
  181. mouseWheelEnabled: false // 取消滚轮事件
  182. });
  183. // 设置默认值为0
  184. formModel.set(item.name, 0);
  185. }else if(item.xtype == 'condbfindtrigger') {
  186. item.isConField = true;
  187. }else if(item.xtype == 'detailGridField') {
  188. let index = form.detailCount;
  189. let columns = item.columns,
  190. cnames = columns.filter(function(c) {
  191. return c.dataIndex && !c.ignore;
  192. }).map(function(c) {
  193. return c.dataIndex
  194. }),
  195. defaultValueColumns = {};
  196. Ext.Array.each(columns, function(c) {
  197. if(c.dataIndex && c.defaultValue) {
  198. defaultValueColumns[c.dataIndex] = c.defaultValue;
  199. }
  200. // 不可锁定
  201. Ext.applyIf(c, {
  202. lockable: false,
  203. width: 120
  204. });
  205. //必填
  206. Ext.applyIf(c, {
  207. allowBlank: true
  208. });
  209. if(!c.allowBlank){
  210. c.cls = 'x-grid-necessary';
  211. }
  212. if(c.xtype == 'textfield') {
  213. Ext.applyIf(c, {
  214. maxLength: 50
  215. });
  216. }else if(c.xtype == 'datecolumn') {
  217. Ext.applyIf(c, {
  218. format: 'Y-m-d'
  219. });
  220. }else if(c.xtype == 'numbercolumn') {
  221. Ext.applyIf(c, {
  222. align: 'end'
  223. });
  224. }
  225. let editor = c.editor;
  226. if(editor) {
  227. Ext.applyIf(editor, {
  228. selectOnFocus: true
  229. });
  230. if(editor.xtype == 'numberfield') {
  231. Ext.applyIf(editor, {
  232. hideTrigger: true, // 隐藏trigger
  233. mouseWheelEnabled: false // 取消滚轮事件
  234. });
  235. }else if(editor.xtype == 'datefield') {
  236. Ext.apply(editor, {
  237. format: 'Y-m-d'
  238. });
  239. Ext.applyIf(editor, {
  240. editable: false
  241. });
  242. }
  243. }
  244. });
  245. cnames.push(item.detnoColumn);
  246. formModel.set('detail' + index + '.detailBindFields', cnames);
  247. item.bind = {
  248. store: '{detail' + index + '.detailStore}'
  249. };
  250. formModel.set('detail' + index + '.detailStore', Ext.create('Ext.data.Store', {
  251. model:item.storeModel,
  252. data: [],
  253. listeners: {
  254. datachanged: function(s, eOpts) {
  255. let g = form.query('detailGridField')[index];
  256. g.fireEvent('datachanged', g, s, eOpts);
  257. },
  258. // 为新增行设置默认值
  259. add: function(store, records, index, eOpts) {
  260. Ext.Array.each(records, function(r) {
  261. for(k in defaultValueColumns) {
  262. r.set(k, defaultValueColumns[k]);
  263. }
  264. r.commit();
  265. });
  266. }
  267. }
  268. }));
  269. form.detailCount++;
  270. }
  271. });
  272. return items;
  273. },
  274. loadData: function(form) {
  275. let me = this;
  276. form.setLoading(true);
  277. if(form.initId && form.initId!=0) {
  278. let url = form._readUrl + '/' + form.initId;
  279. saas.util.BaseUtil.request({url })
  280. .then(function(res) {
  281. form.setLoading(false);
  282. if(res.success) {
  283. let d = res.data;
  284. let o = {
  285. main: d.main
  286. };
  287. if(d.hasOwnProperty('items')) {
  288. o.detail0 = d.items;
  289. }else {
  290. let idx = 1;
  291. while(d.hasOwnProperty('items' + idx)) {
  292. o['detail' + (idx - 1)] = d['items' + idx];
  293. idx++;
  294. }
  295. }
  296. form.initFormData(o);
  297. form.fireEvent('load', form, o);
  298. }
  299. })
  300. .catch(function(e) {
  301. form.setLoading(false);
  302. saas.util.BaseUtil.showErrorToast('读取单据数据错误: ' + e.message);
  303. });
  304. }
  305. }
  306. }
  307. });