FormUtil.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. Ext.define('saas.util.FormUtil', {
  2. BaseUtil: Ext.create('saas.util.BaseUtil'),
  3. // 请求页面组件接口模板
  4. baseUrl: 'http://192.168.0.181:8560/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(response) {
  22. var res = Ext.decode(response.responseText);
  23. if(res.success) {
  24. var config = res.data || true, items = defaultItems || [];
  25. if(config) {
  26. var cusItems = config.items || [];
  27. Ext.Array.each(cusItems, function(cusItem) {
  28. var item = items.find(function(item) {
  29. return item.name == cusItem.name;
  30. });
  31. Ext.apply(item, cusItem);
  32. });
  33. Ext.Array.each(items, function(item) {
  34. var bind = item.bind,
  35. bindName = bind,
  36. defaultValue = item.defaultValue;
  37. if(bindName && !Ext.isString(bindName)) {
  38. bindName = bindName.value;
  39. }
  40. if(bindName) {
  41. bindName = bindName.replace(/[{|}]/g, '');
  42. }
  43. // 设置初始值
  44. if(defaultValue) {
  45. formModel.set(bindName, defaultValue);
  46. }
  47. // 设置必填
  48. if(item.allowBlank==false){
  49. // TODO 需要判断类型
  50. item.beforeLabelTextTpl = "<font color=\"red\" style=\"position:relative; top:2px;right:2px; font-weight: bolder;\">*</font>";
  51. }
  52. // 如果是从表为其绑定store
  53. if(item.xtype == 'detailGridField') {
  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. formModel.set('_detailBindFields', cnames);
  61. item.bind = {
  62. store: '{_detailStore}'
  63. };
  64. formModel.set('_detailStore', Ext.create('Ext.data.Store', {
  65. fields: item.columns ? item.columns.filter(function(c) {
  66. return !!c.dataIndex;
  67. }).map(function(c) {
  68. var type = "string";
  69. if(c.xtype!=""){
  70. if(c.xtype=="numbercolumn"){
  71. type = "float"
  72. }
  73. }
  74. return {name: c.dataIndex,type:type};
  75. }) : [],
  76. data: []
  77. }));
  78. }
  79. });
  80. }
  81. return form.addItems(items);
  82. }else {
  83. return []
  84. }
  85. })
  86. .then(function(items) {
  87. form.fireEvent('afterSetItems', form, items);
  88. })
  89. .then(function() {
  90. me.loadData(form);
  91. })
  92. .catch(function(response) {
  93. console.error(response);
  94. });
  95. },
  96. loadData: function(form) {
  97. var me = this;
  98. if(form.initId && form.initId!=0) {
  99. var url = form._readUrl + form.initId,async=false;
  100. me.BaseUtil.request({url,async })
  101. .then(function(response) {
  102. var res = Ext.decode(response.responseText);
  103. if(res.success) {
  104. form.setFormData({
  105. main: res.data.main,
  106. detail: res.data.items
  107. });
  108. }
  109. })
  110. .catch(function(response) {
  111. console.error(response);
  112. });
  113. }else{
  114. //取后台编号
  115. var code;
  116. Ext.Ajax.request({
  117. url: 'http://192.168.253.58:8900/number/getMaxnumber',
  118. params: {
  119. caller:form.caller
  120. },
  121. async:false,
  122. method: 'POST',
  123. success: function(response, opts) {
  124. var res = Ext.decode(response.responseText);
  125. if(res.success){
  126. code = res.data.code
  127. }
  128. },
  129. failure: function(response, opts) {}
  130. });
  131. var viewModel = form.getViewModel();
  132. var detailStore = viewModel.get('_detailStore');
  133. var detno = 0,datas=[];
  134. Ext.Array.each(new Array(10), function() {
  135. detno += 1;
  136. var data = {};
  137. data[form._detnoColumn] = detno;
  138. datas.push(data);
  139. })
  140. detailStore.loadData(datas);
  141. if(code){
  142. viewModel.set(form._codeField,code);
  143. }
  144. }
  145. }
  146. });