FormUtil.js 9.2 KB

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