FormUtil.js 9.8 KB

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