| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- Ext.define('saas.util.FormUtil', {
- BaseUtil: Ext.create('saas.util.BaseUtil'),
- // 请求页面组件接口模板
- baseUrl: 'http://192.168.0.181:8560/api/ui/co_view/config?name={viewName}',
- // 模板替换正则
- urlRe: /(.*){viewName}(.*)/g,
-
- /**
- * 获得form的字段配置
- * @param form: form组件
- * @param url: url
- */
- setItems: function(form) {
- console.log(form);
- var me = this,
- viewName = form.viewName,
- defaultItems = form.defaultItems.map(function(a) {return a;}),
- formModel = form.getViewModel(),
- url = me.baseUrl.replace(me.urlRe, '$1' + viewName);
-
- this.BaseUtil.request({url, async: false})
- .then(function(response) {
- var res = Ext.decode(response.responseText);
- if(res.success) {
- var config = res.data || true, items = defaultItems || [];
- if(config) {
- var cusItems = config.items || [];
- Ext.Array.each(cusItems, function(cusItem) {
- var item = items.find(function(item) {
- return item.name == cusItem.name;
- });
- Ext.apply(item, cusItem);
- });
- Ext.Array.each(items, function(item) {
- var bind = item.bind,
- bindName = bind,
- defaultValue = item.defaultValue;
-
- if(bindName && !Ext.isString(bindName)) {
- bindName = bindName.value;
- }
- if(bindName) {
- bindName = bindName.replace(/[{|}]/g, '');
- }
- // 设置初始值
- if(defaultValue) {
- formModel.set(bindName, defaultValue);
- }
- // 设置必填
- if(1==1) {
- item.fieldLabel += '*';
- }
- // 如果是从表为其绑定store
- if(item.xtype == 'detailGridField') {
- item.bind = {
- store: '{_detailStore}'
- };
- formModel.set('_detailStore', Ext.create('Ext.data.Store', {
- fields: item.columns ? item.columns.filter(function(c) {
- return !!c.dataIndex;
- }).map(function(c) {
- var type = "string";
- if(c.xtype!=""){
- if(c.xtype=="numbercolumn"){
- type = "float"
- }
- }
- return {name: c.dataIndex,type:type};
- }) : [],
- data: []
- }));
- }
- });
- }
- return form.addItems(items);
- }else {
- return []
- }
- })
- .then(function(items) {
- form.fireEvent('afterSetItems', form, items);
- })
- .then(function() {
- me.loadData(form);
- })
- .catch(function(response) {
- console.error(response);
- });
- },
- loadData: function(form) {
- var me = this;
-
- if(form.initId && form.initId!=0) {
- var url = form._readUrl + form.initId,async=false;
- me.BaseUtil.request({url,async })
- .then(function(response) {
- var res = Ext.decode(response.responseText);
- if(res.success) {
- form.setFormData({
- main: res.data.main,
- detail: res.data.items
- });
- }
- })
- .catch(function(response) {
- console.error(response);
- });
- }
- }
- });
|