| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- Ext.define('uas.form.DynamicForm', {
- extend: 'Ext.container.Container',
- xtype: 'dynamicform',
- requires: [
- 'Ext.layout.container.Absolute',
- 'Ext.tab.Panel',
- 'Ext.form.field.Text'
- ],
- layout: "absolute",
- cls: 'dynamicform',
- viewModel: {
- parent: null,
- data: {
- },
- stores: {
- }
- },
- /**
- * 是否校验通过
- */
- isValid: function () {
- return this.getInvalidFields().length == 0;
- },
- /**
- * 校验错误字段
- */
- getInvalidFields: function () {
- var me = this, fields = me.query('field[bind]');
- return fields.filter(function (field) {
- return !field.validate();
- });
- },
- /**
- * 表单校验错误信息
- */
- getErrors: function () {
- var me = this, fields = me.query('field[bind]'), errors = [];
- fields.forEach(function (field) {
- if (!field.validate()) {
- field.getErrors().forEach(function (e) {
- errors.push({
- field: field.fieldLabel,
- error: e
- });
- });
- }
- });
- return errors;
- },
- /**
- * 显示表单错误信息
- */
- showError: function () {
- var me = this, errors = me.getErrors();
- Ext.create('Ext.window.Window', {
- autoShow: true,
- closeAction: 'destroy',
- width: 400,
- height: 300,
- renderTo: me.getEl(),
- title: '操作错误提示',
- ui: 'form',
- scrollable: true,
- modal: true,
- layout: 'fit',
- items: [{
- xtype: 'grid',
- ui: 'form',
- columnLines: true,
- menubar: [{
- text: '重新编辑',
- handler: function () {
- this.up('window').close();
- }
- }],
- columns: [{
- text: '字段',
- width: 100,
- dataIndex: 'field',
- align: 'center'
- }, {
- text: '错误原因',
- flex: 1,
- dataIndex: 'error',
- align: 'center'
- }],
- store: {
- fields: ['field', 'error'],
- data: errors
- }
- }]
- });
- },
- /**
- * 获取数据
- * <pre>
- * 直接从viewModel获取
- * </pre>
- * @param {boolean} dirtyOnly 只取dirty
- */
- getValues: function (dirtyOnly) {
- var me = this, data = this.getViewModel().getData(), values = {};
- Ext.Object.each(data, function (entity, entityValue) {
- if ('Ext.data.Store' == Ext.getClassName(entityValue)) {
- // 分录表
- var records = {}, rowIndex = 0;
- entityValue.each(function (record) {
- if (!dirtyOnly || record.isDirty()) {
- var rowData = Ext.clone(dirtyOnly ? record.getChanges() : record.getData());
- delete rowData.id;
- if (!Ext.Object.isEmpty(rowData)) {
- records[rowIndex] = rowData;
- }
- }
- rowIndex++;
- });
- if (!Ext.Object.isEmpty(records)) {
- values[entity] = records;
- }
- } else if (Ext.isObject(entityValue)) {
- // 单据头表
- var record = {};
- Ext.Object.each(entityValue, function (field, value) {
- var item = me.down('#' + entity + "_" + field);
- if (item && !item.isDisabled() && item.isVisible() && (!dirtyOnly || item.isDirty())) {
- record[field] = value;
- }
- });
- if (!Ext.Object.isEmpty(record)) {
- values[entity] = record;
- }
- }
- });
- return values;
- },
- /**
- * 当数据提交后,清除dirty等状态
- */
- clearState: function () {
- var me = this, fields = me.query('field[dirty]');
- fields.forEach(function (field) {
- field.resetOriginalValue();
- });
- var data = this.getViewModel().getData();
- Ext.Object.each(data, function (entity, entityValue) {
- if ('Ext.data.Store' == Ext.getClassName(entityValue)) {
- entityValue.each(function (record) {
- if (record.isDirty()) {
- record.clearState();
- }
- });
- }
- });
- }
- });
|