FormUtil.js 7.4 KB

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