FormUtil.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. Ext.define('saas.util.FormUtil', {
  2. BaseUtil: Ext.create('saas.util.BaseUtil'),
  3. // 请求页面组件接口模板
  4. baseUrl: '/api/ui/co_view/config?name={viewName}',
  5. // 模板替换正则
  6. urlRe: /(.*){viewName}(.*)/g,
  7. /**
  8. * 获得form的字段配置
  9. * @param form: form组件
  10. * @param url: url
  11. */
  12. setItems: function(form) {
  13. var me = this,
  14. viewName = form.viewName,
  15. defaultItems = form.defaultItems,
  16. brr = [],
  17. formModel = form.getViewModel(),
  18. url = me.baseUrl.replace(me.urlRe, '$1' + viewName);
  19. brr = brr.concat(form.defaultItems);
  20. this.BaseUtil.request({url, async: false})
  21. .then(function(res) {
  22. if(res.success) {
  23. var config = res.data || true, items = defaultItems || [];
  24. if(config) {
  25. var cusItems = config.items || [];
  26. Ext.Array.each(cusItems, function(cusItem) {
  27. var item = items.find(function(item) {
  28. return item.name == cusItem.name;
  29. });
  30. Ext.apply(item, cusItem);
  31. });
  32. Ext.Array.each(items, function(item) {
  33. if(item.xtype == 'datefield') {
  34. Ext.applyIf(item, {
  35. format: 'Y-m-d'
  36. });
  37. }
  38. if(item.xtype == 'numberfield') {
  39. Ext.applyIf(item, {
  40. hideTrigger: true, // 隐藏trigger
  41. mouseWheelEnabled: false // 取消滚轮事件
  42. });
  43. // 设置默认值为0
  44. formModel.set(item.name, 0);
  45. }
  46. // 设置必填
  47. if(item.allowBlank==false){
  48. // TODO 需要判断类型
  49. item.beforeLabelTextTpl = "<font color=\"red\" style=\"position:relative; top:2px;right:2px; font-weight: bolder;\">*</font>";
  50. }
  51. // 如果是从表为其绑定store
  52. if(item.xtype == 'detailGridField') {
  53. var index = form.detailCount;
  54. var columns = item.columns,
  55. cnames = columns.filter(function(c) {
  56. return c.dataIndex && !c.ignore;
  57. }).map(function(c) {
  58. return c.dataIndex
  59. }),
  60. defaultValueColumns = {};
  61. Ext.Array.each(columns, function(c) {
  62. if(c.dataIndex && c.defaultValue) {
  63. defaultValueColumns[c.dataIndex] = c.defaultValue;
  64. }
  65. // 不可锁定
  66. Ext.applyIf(c, {
  67. lockable: false,
  68. width: 120
  69. });
  70. //必填
  71. Ext.applyIf(c, {
  72. allowBlank: true
  73. });
  74. if(!c.allowBlank){
  75. c.cls = 'x-grid-necessary';
  76. }
  77. if(c.xtype == 'datecolumn') {
  78. Ext.applyIf(c, {
  79. format: 'Y-m-d'
  80. });
  81. }else if(c.xtype == 'numbercolumn') {
  82. Ext.applyIf(c, {
  83. align: 'end'
  84. });
  85. }
  86. var editor = c.editor;
  87. if(editor) {
  88. if(editor.xtype == 'numberfield') {
  89. Ext.applyIf(editor, {
  90. hideTrigger: true, // 隐藏trigger
  91. mouseWheelEnabled: false // 取消滚轮事件
  92. });
  93. }else if(editor.xtype == 'datefield') {
  94. Ext.apply(editor, {
  95. format: 'Y-m-d'
  96. });
  97. }
  98. }
  99. });
  100. columns.push({
  101. flex: 1,
  102. allowBlank: true
  103. });
  104. cnames.push(item.detnoColumn);
  105. formModel.set('detail' + index + '.detailBindFields', cnames);
  106. item.bind = {
  107. store: '{detail' + index + '.detailStore}'
  108. };
  109. formModel.set('detail' + index + '.detailStore', Ext.create('Ext.data.Store', {
  110. model:item.storeModel,
  111. data: [],
  112. listeners: {
  113. add: function(store, records, index, eOpts) {
  114. Ext.Array.each(records, function(r) {
  115. for(k in defaultValueColumns) {
  116. r.set(k, defaultValueColumns[k]);
  117. }
  118. r.commit();
  119. });
  120. }
  121. }
  122. }));
  123. form.detailCount++;
  124. }
  125. });
  126. }
  127. return form.addItems(items);
  128. }else {
  129. return []
  130. }
  131. })
  132. .then(function(items) {
  133. form.fireEvent('afterSetItems', form, items);
  134. })
  135. .then(function() {
  136. me.loadData(form);
  137. })
  138. .catch(function(response) {
  139. console.error(response);
  140. });
  141. },
  142. loadData: function(form) {
  143. var me = this;
  144. form.setLoading(true);
  145. if(form.initId && form.initId!=0) {
  146. var url = form._readUrl + form.initId;
  147. me.BaseUtil.request({url })
  148. .then(function(res) {
  149. form.setLoading(false);
  150. if(res.success) {
  151. var d = res.data;
  152. var o = {
  153. main: d.main
  154. };
  155. if(d.hasOwnProperty('items')) {
  156. o.detail0 = d.items;
  157. }else {
  158. var idx = 1;
  159. while(d.hasOwnProperty('items' + idx)) {
  160. o['detail' + (idx - 1)] = d['items' + idx];
  161. idx++;
  162. }
  163. }
  164. form.setFormData(o);
  165. }
  166. })
  167. .catch(function(response) {
  168. form.setLoading(false);
  169. console.error(response);
  170. });
  171. }else{
  172. //取后台编号
  173. me.BaseUtil.request({
  174. url: '/api/commons/number/getMaxnumber',
  175. headers: {
  176. "Content-Type": 'application/x-www-form-urlencoded;charset=UTF-8'
  177. },
  178. params: {
  179. caller:form.caller
  180. },
  181. method: 'POST',
  182. }).then(function(res) {
  183. form.setLoading(false);
  184. if(res.success){
  185. var code = res.data;
  186. var viewModel = form.getViewModel();
  187. var detailGrids = form.query('detailGridField');
  188. if(code){
  189. var o = {};
  190. o[form._codeField] = code;
  191. var formData = {
  192. main: o
  193. };
  194. Ext.Array.each(detailGrids, function(grid, index) {
  195. var detno = 0;
  196. var detnoColumn = grid.detnoColumn;
  197. var datas = [];
  198. Ext.Array.each(new Array(3), function() {
  199. detno += 1;
  200. var data = {};
  201. data[detnoColumn] = detno;
  202. datas.push(data);
  203. })
  204. formData['detail' + index] = datas;
  205. });
  206. form.setFormData(formData);
  207. }
  208. }
  209. }).catch(function() {
  210. form.setLoading(false);
  211. })
  212. }
  213. }
  214. });