FormUtil.js 6.1 KB

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