PDA

View Full Version : Accessing the RowIndex in the template of a templatecolumn



radamanth
7 Sep 2010, 4:05 AM
Hi everyone.
I'm converting all my renderer function into Xtemplate for my grid.

In one of my renderer (a checkbox one), I need to have the rowIndex variable concatened to the checkbox name attribute.

In a renderer function it's easy to get it (it s one of the argument), but I can't find how to do the same thing in the Xtemplate.

I tried this :

var checkboxTpl = new Ext.XTemplate(
'<div style="width:100%;height:100%;padding: 3px 3px 3px 3px;">',
'<tpl if="publication">',
"<input type=\"checkbox\" name=\"check{this.rowIndex}\" id=\"checkbox\" value=\"{idPublication}_0_1\"/>",
'</tpl>',
'</div>',
{
compiled: true,
disableFormats: true
}
);

Has anyone any idea ?

Many thanks in advance.

Regards.

Condor
7 Sep 2010, 4:23 AM
{#} {xindex} or {xcount}

xindex/# is 0-based and xcount is 1-based.

Not applicable.

Animal
7 Sep 2010, 4:37 AM
It might be nice to be able to access that. Would be an FR:




Ext.grid.TemplateColumn = Ext.extend(Ext.grid.Column, {
/**
* @cfg {String/XTemplate} tpl
* An {@link Ext.XTemplate XTemplate}, or an XTemplate <i>definition string</i> to use to process a
* {@link Ext.data.Record Record}'s {@link Ext.data.Record#data data} to produce a column's rendered value.
*/
constructor: function(cfg){
Ext.grid.TemplateColumn.superclass.constructor.call(this, cfg);
var tpl = (!Ext.isPrimitive(this.tpl) && this.tpl.compile) ? this.tpl : new Ext.XTemplate(this.tpl);
this.renderer = function(value, p, r, rowIndex, colIndex){
tpl.rowIndex = rowIndex;
tpl.colIndex = colIndex;
return tpl.apply(r.data);
};
this.tpl = tpl;
}
});


But your code is wrong, that's not how you access arbitrary Javascript code. That is documented.

Well, there's the water, horsey!

Animal
7 Sep 2010, 4:38 AM
{#} {xindex} or {xcount}

xindex/# is 0-based and xcount is 1-based.

It's not an XTemplate looping over an Array, so it won't have them.

radamanth
7 Sep 2010, 4:43 AM
Hi Condor.
Thanks for your answer.
But {#} {xindex} or {xcount} works if you are in a <tpl for> loop.

In my case, the template are use as column template in a Grid and I wanted to concat the rowIndex of the record.



I'm trying to see if I can get something out of the {parent} variable.

Animal
7 Sep 2010, 4:46 AM
Helloooooooooooooooooooooooooooooo!

Condor
7 Sep 2010, 4:48 AM
Or you could use:

Ext.grid.TemplateColumn = Ext.extend(Ext.grid.Column, {
constructor: function(cfg){
Ext.grid.TemplateColumn.superclass.constructor.call(this, cfg);
var tpl = (!Ext.isPrimitive(this.tpl) && this.tpl.compile) ? this.tpl : new Ext.XTemplate(this.tpl);
this.renderer = function(value, p, r, rowIndex, colIndex){
return tpl.apply(Ext.apply({rowIndex: rowIndex, colIndex: colIndex}, r.data));
};
this.tpl = tpl;
}
});
(as long as you don't have rowIndex or colIndex fields)

Animal
7 Sep 2010, 4:48 AM
I thought of that, but there's the outside chance of collisions with field names.

radamanth
7 Sep 2010, 4:49 AM
Thanks a Lot Animal.

I think I'm gonna keep the renderer function and modify it to use a template instead of string concat.


It might be nice to be able to access that. Would be an FR:




Ext.grid.TemplateColumn = Ext.extend(Ext.grid.Column, {
/**
* @cfg {String/XTemplate} tpl
* An {@link Ext.XTemplate XTemplate}, or an XTemplate <i>definition string</i> to use to process a
* {@link Ext.data.Record Record}'s {@link Ext.data.Record#data data} to produce a column's rendered value.
*/
constructor: function(cfg){
Ext.grid.TemplateColumn.superclass.constructor.call(this, cfg);
var tpl = (!Ext.isPrimitive(this.tpl) && this.tpl.compile) ? this.tpl : new Ext.XTemplate(this.tpl);
this.renderer = function(value, p, r, rowIndex, colIndex){
tpl.rowIndex = rowIndex;
tpl.colIndex = colIndex;
return tpl.apply(r.data);
};
this.tpl = tpl;
}
});


But your code is wrong, that's not how you access arbitrary Javascript code. That is documented.

Well, there's the water, horsey!