FormUtil.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. console.log(form);
  14. var me = this,
  15. viewName = form.viewName,
  16. defaultItems = form.defaultItems.map(function(a) {return a;}),
  17. formModel = form.getViewModel(),
  18. url = me.baseUrl.replace(me.urlRe, '$1' + viewName);
  19. this.BaseUtil.request({url, async: false})
  20. .then(function(response) {
  21. var res = Ext.decode(response.responseText);
  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. var bind = item.bind,
  34. bindName = bind,
  35. defaultValue = item.defaultValue;
  36. if(bindName && !Ext.isString(bindName)) {
  37. bindName = bindName.value;
  38. }
  39. if(bindName) {
  40. bindName = bindName.replace(/[{|}]/g, '');
  41. }
  42. // 设置初始值
  43. if(defaultValue) {
  44. formModel.set(bindName, defaultValue);
  45. }
  46. // 设置必填
  47. if(1==1) {
  48. item.fieldLabel += '*';
  49. }
  50. // 如果是从表为其绑定store
  51. if(item.xtype == 'detailGridField') {
  52. item.bind = {
  53. store: '{_detailStore}'
  54. };
  55. formModel.set('_detailStore', Ext.create('Ext.data.Store', {
  56. fields: item.columns ? item.columns.filter(function(c) {
  57. return !!c.dataIndex;
  58. }).map(function(c) {
  59. var type = "string";
  60. if(c.xtype!=""){
  61. if(c.xtype=="numbercolumn"){
  62. type = "float"
  63. }
  64. }
  65. return {name: c.dataIndex,type:type};
  66. }) : [],
  67. data: []
  68. }));
  69. }
  70. });
  71. }
  72. return form.addItems(items);
  73. }else {
  74. return []
  75. }
  76. })
  77. .then(function(items) {
  78. form.fireEvent('afterSetItems', form, items);
  79. })
  80. .then(function() {
  81. me.loadData(form);
  82. })
  83. .catch(function(response) {
  84. console.error(response);
  85. });
  86. },
  87. loadData: function(form) {
  88. var me = this;
  89. if(form.initId && form.initId!=0) {
  90. var url = form._readUrl + form.initId,async=false;
  91. me.BaseUtil.request({url,async })
  92. .then(function(response) {
  93. var res = Ext.decode(response.responseText);
  94. if(res.success) {
  95. form.setFormData({
  96. main: res.data.main,
  97. detail: res.data.items
  98. });
  99. }
  100. })
  101. .catch(function(response) {
  102. console.error(response);
  103. });
  104. }
  105. }
  106. });