VATigers
5 Nov 2009, 2:57 PM
Hello guys,
I have been working on a requirement to add and delete rows from grid dynamically. I created a extension called 'ButtonColumn' which would be rendered as buttons. In my requirement, I am using the column to delete the records .
It seems, I am getting the wrong dataindex when I click delete. If I add few records and then delete and add some rows, the rowIndex gets messed up.
Here is the custom button column extension, I created. I am also attaching the html file.
//create the plugins namespace
Ext.fmPlugins = {};
Ext.fmPlugins.parseInt = function (str, defVal) {
var val = parseInt(str);
return isNaN(val) ? defVal : val;
}
Ext.fmPlugins.override(Ext.grid.GridPanel, {
onClick: function (e) {
Ext.grid.GridPanel.prototype.onClick.base.apply(this, arguments);
var target = e.getTarget();
if (target.tagName === "BUTTON" && !target.isDisabled) {
var colIndex = Ext.fmPlugins.parseInt(target.getAttribute("col"), -1);
if (colIndex >= 0) {
var col = this.getColumnModel().columns[colIndex];
var fn = col['processCellClick'];
if (Ext.isFunction(fn)) {
var rowIndex = Ext.fmPlugins.parseInt(target.getAttribute("row"), -1);
fn.call(col, rowIndex, colIndex, e);
}
}
}
}
});
Ext.fmPlugins.ButtonColumn = Ext.extend(Ext.grid.Column, {
buttonClass: '',
constructor: function (config) {
config = config || {};
this.text = config.text;
this.buttonClass = config.buttonClass;
Ext.fmPlugins.ButtonColumn.superclass.constructor.call(this, config);
this.addEvents('buttonColumnInfoNeeded', 'buttonClick');
Ext.util.Observable.apply(this, arguments);
this.renderer = this.buttonColumnRenderer.createDelegate(this);
if (!Ext.fmPlugins.ButtonColumn.enabledBtnTemplate) {
Ext.fmPlugins.ButtonColumn.enabledBtnTemplate = this.createButtonTemplate(true);
}
if (!Ext.fmPlugins.ButtonColumn.disabledBtnTemplate) {
Ext.fmPlugins.ButtonColumn.disabledBtnTemplate = this.createButtonTemplate(false);
}
},
buttonColumnRenderer: function (value, metadata, record, rowIndex, colIndex, store) {
var eventArgs = {
colIndex: colIndex,
rowIndex: rowIndex,
enable: true,
record: record
};
var disabled = this.fireEvent('buttonColumnInfoNeeded', this, eventArgs);
var templateValues = {
cls: this.buttonClass || '',
value: this.text,
rowIndex: rowIndex,
colIndex: colIndex
};
return this.getButtonColumnHTML(disabled, rowIndex, colIndex);
}
,
processCellClick: function (rowIndex, colIndex, e) {
alert(rowIndex);
this.fireEvent('buttonClick', this, rowIndex);
}
,
getButtonColumnHTML: function (disabled, rowIndex, colIndex) {
var templateValues = {
cls: this.buttonClass || '',
value: this.text,
rowIndex: rowIndex,
colIndex: colIndex
};
if (disabled) {
return Ext.fmPlugins.ButtonColumn.disabledBtnTemplate.apply(templateValues);
}
return Ext.fmPlugins.ButtonColumn.enabledBtnTemplate.apply(templateValues);
}
,
createButtonTemplate: function (enabled) {
var templateArray = new Array();
var disabledClass = '';
var disabled = '';
var readOnlyText = '';
if (!enabled) {
var disabledClass = 'x-item-disabled';
var disabled = 'disabled';
var readOnlyText = 'readOnly';
}
var templateStart = String.format('<TABLE style="WIDTH: auto" class="x-btn {cls} x-btn-noicon {0}" {1} cellSpacing=0 {2}>', disabledClass, disabled, readOnlyText);
templateArray.push(templateStart);
templateArray.push('<TBODY class="x-btn-small x-btn-icon-small-left">');
templateArray.push('<TR>');
templateArray.push('<TD class=x-btn-tl><I> </I></TD>');
templateArray.push('<TD class=x-btn-tc></TD>');
templateArray.push('<TD class=x-btn-tr><I> </I></TD>');
templateArray.push('</TR><TR>');
templateArray.push('<TD class=x-btn-ml><I> </I></TD>');
templateArray.push('<TD class=x-btn-mc>');
templateArray.push('<EM unselectable="on">');
templateArray.push('<BUTTON class="x-btn-text" type="button" row="{rowIndex}" col="{colIndex}">{value}</BUTTON>');
templateArray.push('</EM>');
templateArray.push('</TD>');
templateArray.push('<TD class=x-btn-mr><I> </I></TD>');
templateArray.push('</TR> <TR>');
templateArray.push('<TD class=x-btn-bl><I> </I></TD>');
templateArray.push('<TD class=x-btn-bc></TD>');
templateArray.push('<TD class=x-btn-br><I> </I></TD>');
templateArray.push('</TR></TBODY></TABLE>');
var tpl = new Ext.Template(templateArray);
tpl.compile();
return tpl;
}
});
Ext.apply(Ext.fmPlugins.ButtonColumn.prototype, Ext.util.Observable.prototype);
//Registering a xtype with the control.
Ext.grid.Column.types['buttoncolumn'] = Ext.fmPlugins.ButtonColumn;
Guys, I am really tired working with this. Would really appreciate any advice.
Thanks in advance.
I have been working on a requirement to add and delete rows from grid dynamically. I created a extension called 'ButtonColumn' which would be rendered as buttons. In my requirement, I am using the column to delete the records .
It seems, I am getting the wrong dataindex when I click delete. If I add few records and then delete and add some rows, the rowIndex gets messed up.
Here is the custom button column extension, I created. I am also attaching the html file.
//create the plugins namespace
Ext.fmPlugins = {};
Ext.fmPlugins.parseInt = function (str, defVal) {
var val = parseInt(str);
return isNaN(val) ? defVal : val;
}
Ext.fmPlugins.override(Ext.grid.GridPanel, {
onClick: function (e) {
Ext.grid.GridPanel.prototype.onClick.base.apply(this, arguments);
var target = e.getTarget();
if (target.tagName === "BUTTON" && !target.isDisabled) {
var colIndex = Ext.fmPlugins.parseInt(target.getAttribute("col"), -1);
if (colIndex >= 0) {
var col = this.getColumnModel().columns[colIndex];
var fn = col['processCellClick'];
if (Ext.isFunction(fn)) {
var rowIndex = Ext.fmPlugins.parseInt(target.getAttribute("row"), -1);
fn.call(col, rowIndex, colIndex, e);
}
}
}
}
});
Ext.fmPlugins.ButtonColumn = Ext.extend(Ext.grid.Column, {
buttonClass: '',
constructor: function (config) {
config = config || {};
this.text = config.text;
this.buttonClass = config.buttonClass;
Ext.fmPlugins.ButtonColumn.superclass.constructor.call(this, config);
this.addEvents('buttonColumnInfoNeeded', 'buttonClick');
Ext.util.Observable.apply(this, arguments);
this.renderer = this.buttonColumnRenderer.createDelegate(this);
if (!Ext.fmPlugins.ButtonColumn.enabledBtnTemplate) {
Ext.fmPlugins.ButtonColumn.enabledBtnTemplate = this.createButtonTemplate(true);
}
if (!Ext.fmPlugins.ButtonColumn.disabledBtnTemplate) {
Ext.fmPlugins.ButtonColumn.disabledBtnTemplate = this.createButtonTemplate(false);
}
},
buttonColumnRenderer: function (value, metadata, record, rowIndex, colIndex, store) {
var eventArgs = {
colIndex: colIndex,
rowIndex: rowIndex,
enable: true,
record: record
};
var disabled = this.fireEvent('buttonColumnInfoNeeded', this, eventArgs);
var templateValues = {
cls: this.buttonClass || '',
value: this.text,
rowIndex: rowIndex,
colIndex: colIndex
};
return this.getButtonColumnHTML(disabled, rowIndex, colIndex);
}
,
processCellClick: function (rowIndex, colIndex, e) {
alert(rowIndex);
this.fireEvent('buttonClick', this, rowIndex);
}
,
getButtonColumnHTML: function (disabled, rowIndex, colIndex) {
var templateValues = {
cls: this.buttonClass || '',
value: this.text,
rowIndex: rowIndex,
colIndex: colIndex
};
if (disabled) {
return Ext.fmPlugins.ButtonColumn.disabledBtnTemplate.apply(templateValues);
}
return Ext.fmPlugins.ButtonColumn.enabledBtnTemplate.apply(templateValues);
}
,
createButtonTemplate: function (enabled) {
var templateArray = new Array();
var disabledClass = '';
var disabled = '';
var readOnlyText = '';
if (!enabled) {
var disabledClass = 'x-item-disabled';
var disabled = 'disabled';
var readOnlyText = 'readOnly';
}
var templateStart = String.format('<TABLE style="WIDTH: auto" class="x-btn {cls} x-btn-noicon {0}" {1} cellSpacing=0 {2}>', disabledClass, disabled, readOnlyText);
templateArray.push(templateStart);
templateArray.push('<TBODY class="x-btn-small x-btn-icon-small-left">');
templateArray.push('<TR>');
templateArray.push('<TD class=x-btn-tl><I> </I></TD>');
templateArray.push('<TD class=x-btn-tc></TD>');
templateArray.push('<TD class=x-btn-tr><I> </I></TD>');
templateArray.push('</TR><TR>');
templateArray.push('<TD class=x-btn-ml><I> </I></TD>');
templateArray.push('<TD class=x-btn-mc>');
templateArray.push('<EM unselectable="on">');
templateArray.push('<BUTTON class="x-btn-text" type="button" row="{rowIndex}" col="{colIndex}">{value}</BUTTON>');
templateArray.push('</EM>');
templateArray.push('</TD>');
templateArray.push('<TD class=x-btn-mr><I> </I></TD>');
templateArray.push('</TR> <TR>');
templateArray.push('<TD class=x-btn-bl><I> </I></TD>');
templateArray.push('<TD class=x-btn-bc></TD>');
templateArray.push('<TD class=x-btn-br><I> </I></TD>');
templateArray.push('</TR></TBODY></TABLE>');
var tpl = new Ext.Template(templateArray);
tpl.compile();
return tpl;
}
});
Ext.apply(Ext.fmPlugins.ButtonColumn.prototype, Ext.util.Observable.prototype);
//Registering a xtype with the control.
Ext.grid.Column.types['buttoncolumn'] = Ext.fmPlugins.ButtonColumn;
Guys, I am really tired working with this. Would really appreciate any advice.
Thanks in advance.