FormUtil.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. form.setLoading(true);
  99. if(form.initId && form.initId!=0) {
  100. var url = form._readUrl + form.initId;
  101. me.BaseUtil.request({url })
  102. .then(function(response) {
  103. form.setLoading(false);
  104. var res = Ext.decode(response.responseText);
  105. if(res.success) {
  106. form.setFormData({
  107. main: res.data.main,
  108. detail: res.data.items
  109. });
  110. }
  111. })
  112. .catch(function(response) {
  113. form.setLoading(false);
  114. console.error(response);
  115. });
  116. }else{
  117. //取后台编号
  118. me.BaseUtil.request({
  119. url: 'http://192.168.253.58:8900/number/getMaxnumber',
  120. params: {
  121. caller:form.caller
  122. },
  123. method: 'POST',
  124. }).then(function(response) {
  125. form.setLoading(false);
  126. var res = Ext.decode(response.responseText);
  127. if(res.success){
  128. var code = res.data.code;
  129. var viewModel = form.getViewModel();
  130. var detailStore = viewModel.get('_detailStore');
  131. var detno = 0,datas=[];
  132. Ext.Array.each(new Array(10), function() {
  133. detno += 1;
  134. var data = {};
  135. data[form._detnoColumn] = detno;
  136. datas.push(data);
  137. })
  138. detailStore.loadData(datas);
  139. if(code){
  140. viewModel.set(form._codeField,code);
  141. }
  142. }
  143. }).catch(function() {
  144. form.setLoading(false);
  145. })
  146. }
  147. }
  148. });