123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344 |
- Ext.define('saas.util.FormUtil', {
- statics: {
- setItems: function(form) {
- let me = this,
- defaultItems = form.defaultItems;
- let items = [];
- items = me.applyItemsGroup(defaultItems || []);
- items = me.initItems(items);
- form.configItems = items;
- items = me.applyDefaultItems(form, items);
- form.removeAll();
- form.addItems(items);
- form.fireEvent('afterSetItems', form, items);
- me.loadData(form);
- },
- applyItemsGroup: function(items) {
- let groups = [],
- groupCount = 0,
- newItems = [];
- Ext.Array.each(items, function(it, index) {
- let item = Object.assign({}, it),
- 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++;
- }
- }
- newItems.push(item);
- });
- Ext.Array.sort(newItems, 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 = newItems.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(newItems, formIndex + index, [{
- xtype: 'separatefield',
- name: 'group' + (++groupCount),
- html: group.title,
- fieldLabel: group.title || '分组' + groupCount
- }]);
- });
- return newItems;
- },
- initItems: function(items) {
- let itemCount = detailCount = 1, newItems = [];
- Ext.Array.each(items, function(it, i) {
- let item = Object.assign({}, it);
- if(item.xtype == 'detailGridField') {
- let columns = item.columns,
- colCount = 1;
- Ext.Array.each(columns, function(col, j) {
- if((col.hidden || col.width == 0 || !col.dataIndex) && (!col.hasOwnProperty('initHidden') || col.initHidden)) {
- Ext.applyIf(col, {
- index: -1,
- initHidden: true
- });
- }else {
- Ext.applyIf(col, {
- text: '',
- hidden: false,
- index: colCount++,
- allowBlank: true,
- width: 100,
- initHidden: false
- });
- }
- });
- if(!columns[columns.length - 1].flex) {
- columns.push({
- dataIndex: '',
- initHidden: true,
- flex: 1,
- allowBlank: true
- });
- }
- Ext.applyIf(item, {
- allowBlank: false,
- columnWidth: 1,
- gname: 'detail' + detailCount,
- fieldLabel: '从表' + (detailCount++),
- });
- }else if(item.xtype == 'hidden') {
- Ext.applyIf(item, {
- fieldLabel: '',
- hidden: true,
- initHidden: true,
- });
- }else if(item.xtype == 'separatefield') {
- Ext.applyIf(item, {
- fieldLabel: item.html,
- columnWidth: 1,
- });
- }else {
- Ext.applyIf(item, {
- fieldLabel: '',
- columnWidth: 0.25,
- });
- }
- if(item.hidden) {
- if(item.initHidden || !item.hasOwnProperty('initHidden')) {
- Ext.applyIf(item, {
- index: -1,
- initHidden: true
- });
- }else {
- Ext.applyIf(item, {
- index: itemCount++,
- initHidden: false
- });
- }
- }else {
- Ext.applyIf(item, {
- index: itemCount++,
- initHidden: false
- });
- }
- Ext.applyIf(item, {
- name: 'item' + i,
- hidden: false,
- allowBlank: true,
- group: undefined,
- });
- newItems.push(item);
- });
- Ext.Array.sort(newItems, function(a, b) {
- return a.index - b.index;
- });
- return newItems;
- },
- /**
- * 处理formitems的一些默认配置
- */
- applyDefaultItems: function(form, items) {
- let me = this,
- formModel = form.getViewModel();
- Ext.Array.each(items, function(item) {
- // 设置必填
- if(item.allowBlank==false){
- // TODO 需要判断类型
- item.beforeLabelTextTpl = "<font color=\"red\" style=\"position:relative; top:2px;right:2px; font-weight: bolder;\">*</font>";
- }
- if(item.xtype == 'textfield') {
- Ext.applyIf(item, {
- maxLength: 50
- });
- }else if(item.xtype == 'datefield') {
- Ext.applyIf(item, {
- editable: false,
- format: 'Y-m-d'
- });
- }else if(item.xtype == 'numberfield') {
- Ext.applyIf(item, {
- hideTrigger: true, // 隐藏trigger
- mouseWheelEnabled: false // 取消滚轮事件
- });
- // 设置默认值为0
- formModel.set(item.name, 0);
- }else if(item.xtype == 'condbfindtrigger') {
- item.isConField = true;
- }else 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
- });
- }
- }
- });
- 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;
- },
- 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(e) {
- form.setLoading(false);
- saas.util.BaseUtil.showErrorToast('读取单据数据错误: ' + e.message);
- });
- }
- }
- }
- });
|