DynamicForm.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. Ext.define('uas.form.DynamicForm', {
  2. extend: 'Ext.container.Container',
  3. xtype: 'dynamicform',
  4. requires: [
  5. 'Ext.layout.container.Absolute',
  6. 'Ext.tab.Panel',
  7. 'Ext.form.field.Text'
  8. ],
  9. layout: "absolute",
  10. cls: 'dynamicform',
  11. viewModel: {
  12. parent: null,
  13. data: {
  14. },
  15. stores: {
  16. }
  17. },
  18. /**
  19. * 是否校验通过
  20. */
  21. isValid: function () {
  22. return this.getInvalidFields().length == 0;
  23. },
  24. /**
  25. * 校验错误字段
  26. */
  27. getInvalidFields: function () {
  28. var me = this, fields = me.query('field[bind]');
  29. return fields.filter(function (field) {
  30. return !field.validate();
  31. });
  32. },
  33. /**
  34. * 表单校验错误信息
  35. */
  36. getErrors: function () {
  37. var me = this, fields = me.query('field[bind]'), errors = [];
  38. fields.forEach(function (field) {
  39. if (!field.validate()) {
  40. field.getErrors().forEach(function (e) {
  41. errors.push({
  42. field: field.fieldLabel,
  43. error: e
  44. });
  45. });
  46. }
  47. });
  48. return errors;
  49. },
  50. /**
  51. * 显示表单错误信息
  52. */
  53. showError: function () {
  54. var me = this, errors = me.getErrors();
  55. Ext.create('Ext.window.Window', {
  56. autoShow: true,
  57. closeAction: 'destroy',
  58. width: 400,
  59. height: 300,
  60. renderTo: me.getEl(),
  61. title: '操作错误提示',
  62. ui: 'form',
  63. scrollable: true,
  64. modal: true,
  65. layout: 'fit',
  66. items: [{
  67. xtype: 'grid',
  68. ui: 'form',
  69. columnLines: true,
  70. menubar: [{
  71. text: '重新编辑',
  72. handler: function () {
  73. this.up('window').close();
  74. }
  75. }],
  76. columns: [{
  77. text: '字段',
  78. width: 100,
  79. dataIndex: 'field',
  80. align: 'center'
  81. }, {
  82. text: '错误原因',
  83. flex: 1,
  84. dataIndex: 'error',
  85. align: 'center'
  86. }],
  87. store: {
  88. fields: ['field', 'error'],
  89. data: errors
  90. }
  91. }]
  92. });
  93. },
  94. /**
  95. * 获取数据
  96. * <pre>
  97. * 直接从viewModel获取
  98. * </pre>
  99. * @param {boolean} dirtyOnly 只取dirty
  100. */
  101. getValues: function (dirtyOnly) {
  102. var me = this, data = this.getViewModel().getData(), values = {};
  103. Ext.Object.each(data, function (entity, entityValue) {
  104. if ('Ext.data.Store' == Ext.getClassName(entityValue)) {
  105. // 分录表
  106. var records = {}, rowIndex = 0;
  107. entityValue.each(function (record) {
  108. if (!dirtyOnly || record.isDirty()) {
  109. var rowData = Ext.clone(dirtyOnly ? record.getChanges() : record.getData());
  110. delete rowData.id;
  111. if (!Ext.Object.isEmpty(rowData)) {
  112. records[rowIndex] = rowData;
  113. }
  114. }
  115. rowIndex++;
  116. });
  117. if (!Ext.Object.isEmpty(records)) {
  118. values[entity] = records;
  119. }
  120. } else if (Ext.isObject(entityValue)) {
  121. // 单据头表
  122. var record = {};
  123. Ext.Object.each(entityValue, function (field, value) {
  124. var item = me.down('#' + entity + "_" + field);
  125. if (item && !item.isDisabled() && item.isVisible() && (!dirtyOnly || item.isDirty())) {
  126. record[field] = value;
  127. }
  128. });
  129. if (!Ext.Object.isEmpty(record)) {
  130. values[entity] = record;
  131. }
  132. }
  133. });
  134. return values;
  135. },
  136. /**
  137. * 当数据提交后,清除dirty等状态
  138. */
  139. clearState: function () {
  140. var me = this, fields = me.query('field[dirty]');
  141. fields.forEach(function (field) {
  142. field.resetOriginalValue();
  143. });
  144. var data = this.getViewModel().getData();
  145. Ext.Object.each(data, function (entity, entityValue) {
  146. if ('Ext.data.Store' == Ext.getClassName(entityValue)) {
  147. entityValue.each(function (record) {
  148. if (record.isDirty()) {
  149. record.clearState();
  150. }
  151. });
  152. }
  153. });
  154. }
  155. });