FormUtil.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. if(columns[columns.length - 1].flex != 1) {
  101. columns.push({
  102. flex: 1,
  103. allowBlank: true
  104. });
  105. }
  106. cnames.push(item.detnoColumn);
  107. formModel.set('detail' + index + '.detailBindFields', cnames);
  108. item.bind = {
  109. store: '{detail' + index + '.detailStore}'
  110. };
  111. formModel.set('detail' + index + '.detailStore', Ext.create('Ext.data.Store', {
  112. model:item.storeModel,
  113. data: [],
  114. listeners: {
  115. add: function(store, records, index, eOpts) {
  116. Ext.Array.each(records, function(r) {
  117. for(k in defaultValueColumns) {
  118. r.set(k, defaultValueColumns[k]);
  119. }
  120. r.commit();
  121. });
  122. }
  123. }
  124. }));
  125. form.detailCount++;
  126. }
  127. });
  128. }
  129. return form.addItems(items);
  130. }else {
  131. return []
  132. }
  133. })
  134. .then(function(items) {
  135. form.fireEvent('afterSetItems', form, items);
  136. })
  137. .then(function() {
  138. me.loadData(form);
  139. })
  140. .catch(function(response) {
  141. console.error(response);
  142. });
  143. },
  144. loadData: function(form) {
  145. var me = this;
  146. form.setLoading(true);
  147. if(form.initId && form.initId!=0) {
  148. var url = form._readUrl + form.initId;
  149. me.BaseUtil.request({url })
  150. .then(function(res) {
  151. form.setLoading(false);
  152. if(res.success) {
  153. var d = res.data;
  154. var o = {
  155. main: d.main
  156. };
  157. if(d.hasOwnProperty('items')) {
  158. o.detail0 = d.items;
  159. }else {
  160. var idx = 1;
  161. while(d.hasOwnProperty('items' + idx)) {
  162. o['detail' + (idx - 1)] = d['items' + idx];
  163. idx++;
  164. }
  165. }
  166. form.initFormData(o);
  167. }
  168. })
  169. .catch(function(response) {
  170. form.setLoading(false);
  171. console.error(response);
  172. });
  173. }else{
  174. //取后台编号
  175. me.BaseUtil.request({
  176. url: '/api/commons/number/getMaxnumber',
  177. headers: {
  178. "Content-Type": 'application/x-www-form-urlencoded;charset=UTF-8'
  179. },
  180. params: {
  181. caller:form.caller
  182. },
  183. method: 'POST',
  184. }).then(function(res) {
  185. form.setLoading(false);
  186. if(res.success){
  187. var code = res.data;
  188. var viewModel = form.getViewModel();
  189. var detailGrids = form.query('detailGridField');
  190. if(code){
  191. var o = {};
  192. o[form._codeField] = code;
  193. var formData = {
  194. main: o
  195. };
  196. Ext.Array.each(detailGrids, function(grid, index) {
  197. var detno = 0;
  198. var detnoColumn = grid.detnoColumn;
  199. var datas = [];
  200. Ext.Array.each(new Array(10), function() {
  201. detno += 1;
  202. var data = {};
  203. data[detnoColumn] = detno;
  204. datas.push(data);
  205. })
  206. formData['detail' + index] = datas;
  207. });
  208. form.initFormData(formData);
  209. }
  210. }
  211. }).catch(function() {
  212. form.setLoading(false);
  213. })
  214. }
  215. }
  216. });