Ext.define('saas.util.FormUtil', { statics: { // 请求页面组件接口模板 baseUrl: '/api/ui/co_view/config?name={viewName}', // 模板替换正则 urlRe: /(.*){viewName}(.*)/g, setItems: function(form) { let me = this, defaultItems; defaultItems = form.defaultItems; me.clearItems(defaultItems); me.applyCusItemConfig(form) .then(function(res) { if(res.success) { let config = res.data || true, items = defaultItems || []; if(config) { let cusItems = config.items || []; Ext.Array.each(cusItems, function(cusItem) { let item = items.find(function(item) { return item.name == cusItem.name; }); Ext.apply(item, cusItem); }); items = me.applyDefaultItems(form, items); items = me.applyItemsGroup(items); } form.removeAll(); return form.addItems(items); }else { return [] } }) .then(function(items) { form.fireEvent('afterSetItems', form, items); }) .then(function() { me.loadData(form); }) .catch(function(response) { saas.util.BaseUtil.showErrorToast('加载数据错误:' + response.message); console.error(response); }); }, clearItems: function(items) { for(let i = items.length - 1; i >= 0; i--) { let item = items[i]; if(item.group == '_nogroup') { item.group = undefined; } if(item.xtype == 'container') { items.splice(i, 1); } } }, /** * 获得form的客户自定义字段配置 * @param form: form组件 * @param url: url */ applyCusItemConfig: function(form) { let me = this, viewName = form.viewName, defaultItems = form.defaultItems, url = me.baseUrl.replace(me.urlRe, '$1' + viewName); return saas.util.BaseUtil.request({url, async: false}); }, /** * 处理formitems的一些默认配置 */ applyDefaultItems: function(form, items) { let me = this, formModel = form.getViewModel(); Ext.Array.each(items, function(item) { // 设置必填 if(item.allowBlank==false){ // TODO 需要判断类型 item.beforeLabelTextTpl = "*"; } if(item.xtype == 'textfield') { Ext.applyIf(item, { maxLength: 50 }); } if(item.xtype == 'datefield') { Ext.applyIf(item, { editable: false, format: 'Y-m-d' }); } if(item.xtype == 'numberfield') { Ext.applyIf(item, { hideTrigger: true, // 隐藏trigger mouseWheelEnabled: false // 取消滚轮事件 }); // 设置默认值为0 formModel.set(item.name, 0); } // 如果是从表为其绑定store if(item.xtype == 'detailGridField') { let index = form.detailCount; let columns = item.columns, cnames = columns.filter(function(c) { return c.dataIndex && !c.ignore; }).map(function(c) { return c.dataIndex }), defaultValueColumns = {}; Ext.Array.each(columns, function(c) { if(c.dataIndex && c.defaultValue) { defaultValueColumns[c.dataIndex] = c.defaultValue; } // 不可锁定 Ext.applyIf(c, { lockable: false, width: 120 }); //必填 Ext.applyIf(c, { allowBlank: true }); if(!c.allowBlank){ c.cls = 'x-grid-necessary'; } if(c.xtype == 'textfield') { Ext.applyIf(c, { maxLength: 50 }); }else if(c.xtype == 'datecolumn') { Ext.applyIf(c, { format: 'Y-m-d' }); }else if(c.xtype == 'numbercolumn') { Ext.applyIf(c, { align: 'end' }); } let editor = c.editor; if(editor) { Ext.applyIf(editor, { selectOnFocus: true }); if(editor.xtype == 'numberfield') { Ext.applyIf(editor, { hideTrigger: true, // 隐藏trigger mouseWheelEnabled: false // 取消滚轮事件 }); }else if(editor.xtype == 'datefield') { Ext.apply(editor, { format: 'Y-m-d' }); Ext.applyIf(editor, { editable: false }); } } }); if(columns[columns.length - 1].flex != 1) { columns.push({ flex: 1, allowBlank: true }); } cnames.push(item.detnoColumn); formModel.set('detail' + index + '.detailBindFields', cnames); item.bind = { store: '{detail' + index + '.detailStore}' }; formModel.set('detail' + index + '.detailStore', Ext.create('Ext.data.Store', { model:item.storeModel, data: [], listeners: { datachanged: function(s, eOpts) { let g = form.query('detailGridField')[index]; g.fireEvent('datachanged', g, s, eOpts); }, // 为新增行设置默认值 add: function(store, records, index, eOpts) { Ext.Array.each(records, function(r) { for(k in defaultValueColumns) { r.set(k, defaultValueColumns[k]); } r.commit(); }); } } })); form.detailCount++; } }); return items; }, applyItemsGroup: function(items) { let groups = []; Ext.Array.each(items, function(item, index) { let groupName = item.group; if(!!groupName) { let idx = groups.findIndex(function(g) { return g.title == groupName; }),group; if(idx == -1) { group = { title: groupName, count: 1 }; groups.push(group); }else { group = groups[idx]; group.count++; } } }); Ext.Array.sort(items, function(a, b) { let gs = groups.concat([{ title: '_nogroup' }]); a.group = a.group || '_nogroup'; let v1 = gs.findIndex(function(g) { return g.title == a.group; }) let v2 = gs.findIndex(function(g) { return g.title == b.group; }) return v1 - v2; }); Ext.Array.each(groups, function(g) { let idx = items.findIndex(function(i) { return i.group == g.title; }); g.index = idx; }); Ext.Array.each(groups, function(group, index) { let formIndex = group.index; delete group.index; Ext.Array.insert(items, formIndex + index, [{ xtype: 'container', userCls: 'x-field-separator', height: 24, html: group.title, columnWidth: 1, ignore: true, isValid: function() { return true; }, isDirty: function() { return false; } }]); }); return items; }, loadData: function(form) { let me = this; form.setLoading(true); if(form.initId && form.initId!=0) { let url = form._readUrl + '/' + form.initId; saas.util.BaseUtil.request({url }) .then(function(res) { form.setLoading(false); if(res.success) { let d = res.data; let o = { main: d.main }; if(d.hasOwnProperty('items')) { o.detail0 = d.items; }else { let idx = 1; while(d.hasOwnProperty('items' + idx)) { o['detail' + (idx - 1)] = d['items' + idx]; idx++; } } form.initFormData(o); form.fireEvent('load', form, o); } }) .catch(function(response) { form.setLoading(false); console.error(response); saas.util.BaseUtil.showErrorToast(response.message); }); }else{ //取后台编号 saas.util.BaseUtil.request({ url: '/api/commons/number/getMaxnumber', headers: { "Content-Type": 'application/x-www-form-urlencoded;charset=UTF-8' }, params: { caller:form.caller }, method: 'POST', }).then(function(res) { form.setLoading(false); if(res.success){ let code = res.data; let viewModel = form.getViewModel(); let detailGrids = form.query('detailGridField'); if(code){ let o = {}; o[form._codeField] = code; let formData = {main: {}}; Ext.apply(formData.main, o); Ext.Array.each(detailGrids, function(grid, index) { let detno = 0; let detnoColumn = grid.detnoColumn; let datas = []; let emptyRows = grid.emptyRows; Ext.Array.each(new Array(emptyRows), function() { detno += 1; let data = {}; data[detnoColumn] = detno; datas.push(data); }) formData['detail' + index] = datas; }); return formData; }else { throw new Error('请求单据编号错误'); } }else { return { main: {}, } } }).then(function(formData) { let initData = form.initData; if(initData) { Ext.apply(initData.main, formData.main); form.setFormData(initData); form.fireEvent('load', form, initData); }else { form.initFormData(formData); form.fireEvent('load', form, formData); } }).catch(function(res) { form.clearDirty(); saas.util.BaseUtil.showErrorToast(res.message); form.setLoading(false); }) } } } });