Nullity
9 Jul 2007, 7:12 AM
And for the trifecta (in addition to InlineTextField and InlineTextArea), here is InlineComboBox:
Ext.form.InlineComboBox = function(config) {
Ext.form.InlineComboBox.superclass.constructor.call(this, config);
};
Ext.extend(Ext.form.InlineComboBox, Ext.form.ComboBox, {
inlineClass: 'x-form-inline-field',
disabledClass: 'x-form-inline-field-disabled',
saveOnlyOnChange: true,
confirmSave: false,
confirmText: 'The data has been successfully saved.',
doSave : function() {
var cfg = this.autoSave;
this.params = (this.name || this.id) + '=' + this.getRawValue();
if (this.hiddenName) {
this.params += '&' + this.hiddenName + '=' + this.hiddenField.value;
}
if (typeof cfg == 'object') {
this.method = cfg.method || 'POST';
this.callback = (!cfg.callback) ? {success: Ext.emptyFn, failure: Ext.emptyFn} :
{success: cfg.callback.success || cfg.callback, failure: cfg.callback.failure || Ext.emptyFn};
this.scope = cfg.scope || this.callback;
if (this.confirmSave === true) {
var success = function() {
Ext.MessageBox.alert('Success', this.confirmText);
}.createDelegate(this);
this.callback.success = success.createSequence(this.callback.success);
}
var p = (cfg.params) ? cfg.params : '';
if (p) {
if (typeof p == 'object') {
this.params += '&' + Ext.urlEncode(p);
}
else if (typeof p == 'string' && p.length) {
this.params += '&' + p;
}
}
this.url = (this.method == 'POST') ? cfg.url : cfg.url + '?' + this.params;
}
else if (typeof cfg == 'string') {
this.method = 'POST';
this.url = (this.method == 'POST') ? cfg : cfg + '?' + this.params;
}
Ext.Ajax.request({
url: this.url,
method: this.method,
success: this.callback.success,
failure: this.callback.failure,
params: this.params,
scope: this.scope
});
}
});
Ext.override(Ext.form.InlineComboBox, {
onRender : Ext.form.ComboBox.prototype.onRender.createSequence(function() {
this.lastValue = '';
this.lastRawValue = '';
this.el.addClass(this.inlineClass);
this.trigger.setDisplayed(false);
}),
onFocus : Ext.form.ComboBox.prototype.onFocus.createSequence(function() {
this.el.removeClass(this.inlineClass);
this.trigger.setDisplayed(true);
}),
triggerBlur : Ext.form.ComboBox.prototype.triggerBlur.createSequence(function() {
if (this.isValid() && !this.el.hasClass(this.inlineClass)) {
this.el.addClass(this.inlineClass);
this.trigger.setDisplayed(false);
if (!this.store.getAt(this.view.getSelectedIndexes()[0]) || !this.getRawValue()) {
this.selectedIndex = -1;
if (this.hiddenField) {
this.hiddenField.value = '';
}
this.value = '';
}
if (this.autoSave && (this.saveOnlyOnChange === false || this.getValue() != this.lastValue || this.getRawValue() != this.lastRawValue)) {
this.doSave();
}
this.lastValue = this.getValue();
this.lastRawValue = this.getRawValue();
}
})
});
... and the CSS (same as the other Inline fields):
.x-form-inline-field, textarea.x-form-inline-field {
background: transparent;
border: 1px solid transparent;
cursor: pointer;
overflow: hidden;
}
.x-form-inline-field-disabled {
color: #000000;
cursor: default;
opacity: 1.0;
-moz-opacity: 1.0;
filter: alpha(opacity=100);
}
This does not have the functionality of hitting the ESC key to cancel editing as the other Inline fields do. Due to the nature of the ComboBox field, things work a bit differently and I haven't yet put the time into getting that to work. Everything else works though.
UPDATE 1 (9/10/2007): Updated code to version 1.2. Added new options: 'confirmSave' and 'confirmText'. The first, 'confirmSave' is a boolean which when true, will display a MessageBox containing the text set by 'confirmText' (optional) when the changes are saved successfully. Also, the 'autoSave' object now supports a 'scope' setting.
UPDATE 2 (9/11/2007): Updated code for version 1.3. Changed the way 'confirmSave' was handled to fix a bug. Refactored some code to be cleaner.
Ext.form.InlineComboBox = function(config) {
Ext.form.InlineComboBox.superclass.constructor.call(this, config);
};
Ext.extend(Ext.form.InlineComboBox, Ext.form.ComboBox, {
inlineClass: 'x-form-inline-field',
disabledClass: 'x-form-inline-field-disabled',
saveOnlyOnChange: true,
confirmSave: false,
confirmText: 'The data has been successfully saved.',
doSave : function() {
var cfg = this.autoSave;
this.params = (this.name || this.id) + '=' + this.getRawValue();
if (this.hiddenName) {
this.params += '&' + this.hiddenName + '=' + this.hiddenField.value;
}
if (typeof cfg == 'object') {
this.method = cfg.method || 'POST';
this.callback = (!cfg.callback) ? {success: Ext.emptyFn, failure: Ext.emptyFn} :
{success: cfg.callback.success || cfg.callback, failure: cfg.callback.failure || Ext.emptyFn};
this.scope = cfg.scope || this.callback;
if (this.confirmSave === true) {
var success = function() {
Ext.MessageBox.alert('Success', this.confirmText);
}.createDelegate(this);
this.callback.success = success.createSequence(this.callback.success);
}
var p = (cfg.params) ? cfg.params : '';
if (p) {
if (typeof p == 'object') {
this.params += '&' + Ext.urlEncode(p);
}
else if (typeof p == 'string' && p.length) {
this.params += '&' + p;
}
}
this.url = (this.method == 'POST') ? cfg.url : cfg.url + '?' + this.params;
}
else if (typeof cfg == 'string') {
this.method = 'POST';
this.url = (this.method == 'POST') ? cfg : cfg + '?' + this.params;
}
Ext.Ajax.request({
url: this.url,
method: this.method,
success: this.callback.success,
failure: this.callback.failure,
params: this.params,
scope: this.scope
});
}
});
Ext.override(Ext.form.InlineComboBox, {
onRender : Ext.form.ComboBox.prototype.onRender.createSequence(function() {
this.lastValue = '';
this.lastRawValue = '';
this.el.addClass(this.inlineClass);
this.trigger.setDisplayed(false);
}),
onFocus : Ext.form.ComboBox.prototype.onFocus.createSequence(function() {
this.el.removeClass(this.inlineClass);
this.trigger.setDisplayed(true);
}),
triggerBlur : Ext.form.ComboBox.prototype.triggerBlur.createSequence(function() {
if (this.isValid() && !this.el.hasClass(this.inlineClass)) {
this.el.addClass(this.inlineClass);
this.trigger.setDisplayed(false);
if (!this.store.getAt(this.view.getSelectedIndexes()[0]) || !this.getRawValue()) {
this.selectedIndex = -1;
if (this.hiddenField) {
this.hiddenField.value = '';
}
this.value = '';
}
if (this.autoSave && (this.saveOnlyOnChange === false || this.getValue() != this.lastValue || this.getRawValue() != this.lastRawValue)) {
this.doSave();
}
this.lastValue = this.getValue();
this.lastRawValue = this.getRawValue();
}
})
});
... and the CSS (same as the other Inline fields):
.x-form-inline-field, textarea.x-form-inline-field {
background: transparent;
border: 1px solid transparent;
cursor: pointer;
overflow: hidden;
}
.x-form-inline-field-disabled {
color: #000000;
cursor: default;
opacity: 1.0;
-moz-opacity: 1.0;
filter: alpha(opacity=100);
}
This does not have the functionality of hitting the ESC key to cancel editing as the other Inline fields do. Due to the nature of the ComboBox field, things work a bit differently and I haven't yet put the time into getting that to work. Everything else works though.
UPDATE 1 (9/10/2007): Updated code to version 1.2. Added new options: 'confirmSave' and 'confirmText'. The first, 'confirmSave' is a boolean which when true, will display a MessageBox containing the text set by 'confirmText' (optional) when the changes are saved successfully. Also, the 'autoSave' object now supports a 'scope' setting.
UPDATE 2 (9/11/2007): Updated code for version 1.3. Changed the way 'confirmSave' was handled to fix a bug. Refactored some code to be cleaner.