ExtJS: Trying to use custom renderer for each column, scope issue - extjs

so I am trying to have a custom function set as the renderer for each column, however I need to know which column is currently being rendered so I know what to do in the renderer.
I defined and extended the column and created my own custom column. I figured there I could define the custom rendering function for which it's scope would be the calling column. From there I could use this.mode, this.unit, etc. to get what information I need from the column to proceed. This isn't working out however, and I must not be understanding correctly.
Ext.define('column.Custom',
{
extend: 'Ext.grid.column.Column',
alias: 'widget.customcolumn',
listeners: {
beforerender: function(col){
//console.log(col);
}
},
gridRenderer: function(value, cell, record, row, column)
{
console.log(this);
if (record.data.StartDate == 'N/A' ||
record.data.EndDate == 'N/A')
{
cell.style = 'background:#EEE;';
return 'N/A';
}
// the locked grid has it's own renderers, so if we're here, assume we want the normalGrid
column = grid.normalGrid.columns[column];
if (column.mode == 'cool')
{
cell.style = 'background:#CCFFFF;';
//cell.style = 'background:rgb(47, 162, 223);';
}
else if (column.mode == 'heat')
{
cell.style = 'background:#FFCCCC;';
}
....etc
return ret;
}
});
And here is one of my columns.
{
// defaults
xtype: 'customcolumn',
align: 'center',
flex: 0,
width: 90,
sortable: true,
//renderer: this.gridRenderer,
lockable: false,
locked: false,
//hideable: false,
// end default
text: 'Total',
menuText: 'Total',
id: 'TotalSavingsColumn',
dataIndex: 'Total',
mode: 'none',
unit: '%',
renderer: {
fn: this.gridRenderer,
scope: this
}
},
Instead of grabbing the column by the column index, which returns the wrong column index, because of hidden columns, I want to be able to just use this.mode, this.unit, etc.
Any ideas? Thanks for the help.

You can define renderer function in your column.Custom class definition. If you want to set scope to column itself, you can do this in initComponent method:
Ext.define('column.Custom',
{
extend: 'Ext.grid.column.Column',
alias: 'widget.customcolumn',
initComponent: function() {
// set scope property to this, so in renderer this will refers to column itself
this.scope = this;
this.callParent();
},
renderer: function() {
console.log(this);
// your renderer...
}
});
Then you do not need to specify renderer in column config, becaus you have it defined in your column.Custom class:
{
// defaults
xtype: 'customcolumn',
align: 'center',
flex: 0,
width: 90,
sortable: true,
lockable: false,
locked: false,
//hideable: false,
// end default
text: 'Total',
menuText: 'Total',
id: 'TotalSavingsColumn',
dataIndex: 'Total',
mode: 'none',
unit: '%'
},

Related

Getting Error as getEditor undefined

I'm trying to get the value of a cell in a grid using below. In-fact I'm just trying to print it in the console
console.log(Ext.ComponentQuery.query('gridcolumn[itemId=gridId]')[0].getEditor().getStore().findRecord('description', 'Description'));
Grid Code
Ext.define('Examples.grid.fdGrid', {
extend: 'Ext.grid.Panel',
xtype: foodGrid',
forceNewStore: true,
itemId: 'foodGrid',
height: Ext.getBody().getViewSize().height - 200,
autoload: false,
columns: [
{
text: 'Food Distrib',
xtype: 'gridcolumn',
itemId:'gridId',
dataIndex: 'food_distributor',
flex: 1,
renderer: function(value){
if(Ext.isNumber(value)){
var store = this.getEditor().getStore();
return store.findRecord('foodid',value).get('description');
}
return value;
},
editor: {
xtype: 'combobox',
allowBlank: true,
displayField: "description",
valueField: "foodid",
listeners: {
expand: function () {
var call = this.up('foodgrid[itemId=foodGrid]').getSelectionModel().selection.record.data.networkname.trim();
this.store.clearFilter();
this.store.filter({
property: 'call',
value: call,
exactMatch: true
})
}
},
},
}
});
But i'm getting an error as Uncaught TypeError: Cannot read property 'getEditor' of undefined
What's the error please?
Added the Grid Code part, and the column whose value I want to print.
The editor is created when needed (when the first edit occurs). So when the renderer is first called, the editor is not yet available.
What you want to do from inside your renderer, is to directly access the store, not go through the editor. Then you only need a pre-loaded store to be able to render the grid correctly.
renderer: function(value){
if(Ext.isNumber(value)){
var store =Ext.getStore("MyStore");
return store.findRecord('foodid',value).get('description');
}
return value;
},
editor: {
xtype:'combobox',
store:'MyStore'
Of course, you have to make sure that MyStore is loaded before you render the grid.
One can only guess what you are trying to do there but:
Column doesn't have a selection model of itself. Grid does.
Combobox needs a store.
getEditor may return String OR Object if an editor was set and column is editable
editable is provided by a grid plugin. In other words, specifying a column as being editable and specifying a column editor will not be enough, you also need to provide the grid with the editable plugin.
Some working example:
Ext.define('Examples.grid.fdGrid', {
extend: 'Ext.grid.Panel',
xtype: 'feedGrid',
forceNewStore: true,
itemId: 'foodGrid',
height: Ext.getBody().getViewSize().height - 200,
autoload: false,
selModel: 'cellmodel',
plugins: {
ptype: 'cellediting',
clicksToEdit: 1
},
columns: [
{
text: 'Food Distrib',
xtype: 'gridcolumn',
itemId:'gridId',
dataIndex: 'food_distributor',
flex: 1,
editable: true,
renderer: function(value){
if(Ext.isNumber(value)){
var store = this.getEditor().getStore();
return store.findRecord('foodid',value).get('description');
}
return value;
},
editor: {
xtype: 'combobox',
allowBlank: true,
displayField: "description",
valueField: "foodid",
store: {
fields:['food_distributor', 'description'],
data:[
{
'food_distributor':'a',
foodid:1,
description:'aaaaa'
},
{
'food_distributor':'a',
foodid:2,
description:'bbbbbb'
},
{
'food_distributor':'a',
foodid:3,
description:'aaaaa'
}]
},
listeners: {
expand: function () {
debugger;
var desc = this.up('grid').getSelectionModel().getSelection()[0].get('description').trim();
this.store.clearFilter();
this.store.filter({
property: 'description',
value: desc,
exactMatch: true
})
}
},
},
}
]
});
Ext.create('Examples.grid.fdGrid', {
store: {
fields:['food_distributor', 'description'],
data:[
{
'food_distributor':'a',
foodid:1,
description:'aaaaa'
},
{
'food_distributor':'a',
foodid:2,
description:'bbbbbb'
},
{
'food_distributor':'a',
foodid:3,
description:'aaaaa'
}]
},
renderTo:Ext.getBody()
})
Do you have
var cellEditing = Ext.create('Ext.grid.plugin.CellEditing', {
clicksToEdit: 1
});
Without the plugin the editor doesnt work! and the editor will be undefined when you will try to obtain it

Extjs set icon of treenode in TreeStore

I want to change icon of treenode of Tree Panel. I am able to do so when I append childNode dynamically. But how to set icon to treenode when we apply treestore through proxy directly to Treepanel view in initComponent().
TreePanelView.js
initComponent: function() {
Ext.apply(this, {
store: new Ext.data.TreeStore({
proxy: {
type: 'ajax',
url: 'json/treeview.json'
},
folderSort: true
}),
columns: [{
xtype: 'treecolumn',
text: 'Components',
flex: 2,
sortable: true,
dataIndex: 'childName'
}]
});
this.callParent();
}
You should be able to use a renderer to parse the value of the column and then set an icon based on whatever rules you may have. This simple example adds a class to the column cell that equals it's value, but you could parse the column value and set a specific class or icon in the renderer function instead.
...
columns: [{
xtype: 'treecolumn',
text: 'Components',
flex: 2,
sortable: true,
dataIndex: 'childName',
renderer: function (value, metaData) {
metaData.tdCls = value;
return value;
}
}]
...
More info: http://docs.sencha.com/extjs/4.2.3/#!/api/Ext.grid.column.Column-cfg-renderer

In ExtJs 3.3.1, how can I show a ComboBox drop down without click in EditorGrid?

I am using ExtJs 3.3.1.
Within an EditorGrid, my "editable" column has a ComboBox as its editor. How can I have the ComboBox always showing for each row? Meaning, the user would not have to click on a cell to know there is a ComboBox there. Currently, I have clicksToEdit set to 1, but I wish I could set this to 0 (I tried that).
See some of my code below to see my current configuration.
var combo = new Ext.form.ComboBox({
typeAhead: true,
triggerAction: 'all',
lazyRender: true,
mode: 'local',
store: new Ext.data.ArrayStore({
id: 0,
fields: [
'statusId',
'displayText'],
data: data
}),
valueField: 'statusId',
displayField: 'displayText'
});
var cm = new Ext.grid.ColumnModel({
columns: [{
id: 'orderId',
header: 'ID',
dataIndex: 'id',
width: 50
}, {
header: 'Status',
dataIndex: 'status',
width: 130,
editor: (data.length == 1) ? null : combo,
renderer: Ext.util.Format.comboRenderer(combo)
}, {
id: 'orderSummary',
header: 'Summary',
dataIndex: 'summary',
renderer: this.renderSummary
}]
});
var orderGrid = new Ext.grid.EditorGridPanel({
store: this.getOrderStore(),
cm: cm,
autoExpandColumn: 'orderSummary',
clicksToEdit: 1
});
Here is the solution I came up with.
In my column model, I made sure that the column I am making "editable" has an id. Each cell in that column will now have a CSS class associated with it named "x-grid-col-{id}". My column id is "status" so the class was "x-grid-col-status".
I created the CSS for class "x-grid-col-status" which sets the dropdown arrow image as the background, aligned right. It also sets the cursor to pointer, so the user knows they can click on the cell.
.x-grid3-col-status
{
background-image: url(Image/trigger-single.gif);
background-position: right;
background-repeat: no-repeat;
cursor: pointer;
}
Next, I set up a listener for my ComboBox that listens for the 'focus' event. On focus, I expand the drop down. It is important that I had to add lazyInit: false to my ComboBox config, or else an empty list will appear when you expand. lazyInit - true to not initialize the list for this combo until the field is focused (defaults to true)
The code:
Ext.util.Format.comboRenderer = function (combo) {
return function (value, metaData, record, rowIndex, colIndex, store) {
var record = combo.findRecord(combo.valueField, value);
return record ? record.get(combo.displayField) : combo.valueNotFoundText;
}
}
var combo = new Ext.form.ComboBox({
typeAhead: true,
triggerAction: 'all',
lazyInit: false,
lazyRender: true,
mode: 'local',
editable: false,
store: new Ext.data.ArrayStore({
id: 0,
fields: [
'statusId',
'displayText'
],
data: data
}),
valueField: 'statusId',
displayField: 'displayText',
listeners: {
'focus': {
fn: function (comboField) {
comboField.doQuery(comboField.allQuery, true);
comboField.expand();
}
, scope: this
}
, 'select': {
fn: function (comboField, record, index) {
comboField.fireEvent('blur');
}
, scope: this
}
}
});
var cm = new Ext.grid.ColumnModel({
defaults: {
sortable: true
},
columns: [
{
id: 'orderId',
header: 'ID',
dataIndex: 'id',
width: 50
}, {
header: 'Status',
id: 'status',
dataIndex: 'status',
width: comboColumnWidth,
editor: combo,
renderer: Ext.util.Format.comboRenderer(combo)
}, {
id: 'orderSummary',
header: 'Summary',
dataIndex: 'summary',
renderer: this.renderSummary
}
]
});
var orderGrid = new Ext.grid.EditorGridPanel({
store: this.getOrderStore(),
cm: cm,
autoExpandColumn: 'orderSummary',
title: title,
clicksToEdit: 1
});
I think you'll need to add a special css to the combo box that displays the drop down icon. This is natively not supported by Ext JS. Here's an example of how it can be done:
var companyColumn = {
header: 'Company Name',
dataIndex: 'company',
renderer: function(value, metaData, record, rowIndex, colIndex, store) {
// provide the logic depending on business rules
// name of your own choosing to manipulate the cell depending upon
// the data in the underlying Record object.
if (value == 'whatever') {
//metaData.css : String : A CSS class name to add to the TD element of the cell.
//metaData.attr : String : An html attribute definition string to apply to
// the data container element within the table
// cell (e.g. 'style="color:red;"').
metaData.css = 'name-of-css-class-you-will-define';
}
return value;
}
}
Or you could use the Ext.grid.TemplateColumn and specify the tpl config. This will automatically generate a renderer for the cells in the column and apply the tpl.

extjs apply formatting to grid column

In extjs, if I have a grid definition like this:
xtype: 'grid',
store: 'someStore',
flex: 1,
frame: true,
loadMask: true,
titleCollapse: true,
cls: 'vline-on',
ref: '../someGrid',
id: 'someGrid',
columns: [
{
xtype: 'gridcolumn',
header: 'ID',
dataIndex: 'someID',
sortable: true,
width: 100
}
Is there a way to apply some formatting to this column? For example, this field is a number and if i wish to set a decimal precision..can I do it? Or do I need to apply formatting when the store is being loaded in my java file?
My guess is the latter??
Use "renderer" option. You can define you function there. For example i want to show someID wrapped in some tag:
columns: [
{
xtype: 'gridcolumn',
header: 'ID',
dataIndex: 'someID',
sortable: true,
width: 100,
renderer: function(value) {
// your logic here
return "<b>" + value + "</b>";
}
}
If you want to show decimal precision inside a grid column you should define the dataindex in your store of "float" type:
...
, {name: 'column_data_name', type: 'float'}
...
Then inside the grid column definition you should specify a renderer, as suggested by KomarSerjio, and use it.
function floatRenderer(value) {
if (value) {
var val = value.toFixed(2);
return addSeparatorsNF(val, '.', ',', '.');
}
else return "";
}
...
, { id:'column_data_name', header: 'label', dataIndex: 'column_data_name' , renderer: floatRenderer , align: 'right' }
...
The function addSeparatorsNF has been suggested here.
For formatting your grid decimal value to restrict after point to two number or till you want simply use below code to your column :
renderer: Ext.util.Format.numberRenderer('00.00')
I tried the renderer config KomarSerjio suggested and it worked brilliantly for me when using Sencha Ext JS 6. I used it to zero fill some time data I was receiving which was missing the prefix zero to make it a 24 hour time. So I tried the following and it worked great! Thank you.
Ext.define('ViewLL.view.main.List', {
extend: 'Ext.grid.Panel',
xtype: 'mainlist',
reference: 'mainList',
flex: 1,
requires: [
'ViewLL.store.Datastore'
],
title: 'Records',
store: {
type: 'datastore'
},
columns:
{ text:'Pln On Site Time',
dataIndex:'plnOnSiteTime',
renderer: function (number) {
if (number<=2400) { number = ("000"+number).slice(-4); }
return number;
}
}
});
Prior to renderer config my grid was displaying values e.g. 926, 800, 1000.
Post adding function via renderer config my grid displayed values e.g. 0926, 0800, 1000

How to change the text color of only one row in an ExtJS grid?

I've created the following ExtJS grid.
I want to change the text color of all the cells in the second row.
However, I can only find out how to change the color of all cells in all rows including the header text, as show here:
var myData = [
[4, 'This is a whole bunch of text that is going to be word-wrapped inside this column.', 0.24, 3.0, '2010-11-17 08:31:12'],
[16, 'Computer2-this row should be red', 0.28, 2.7, '2010-11-14 08:31:12'],
[5, 'Network1', 0.02, 2.5, '2010-11-12 08:31:12'],
[1, 'Network2', 0.01, 4.1, '2010-11-11 08:31:12'],
[12, 'Other', 0.42, 5.0, '2010-11-04 08:31:12']
];
var myReader = new Ext.data.ArrayReader({}, [{
name: 'id',
type: 'int'
}, {
name: 'object',
type: 'object'
}, {
name: 'status',
type: 'float'
}, {
name: 'rank',
type: 'float'
}, {
name: 'lastChange',
type: 'date',
dateFormat: 'Y-m-d H:i:s'
}]);
var grid = new Ext.grid.GridPanel({
region: 'center',
style: 'color:red', //makes ALL text in grid red, I only want one row to be red
store: new Ext.data.Store({
data: myData,
reader: myReader
}),
columns: [{
header: 'ID',
width: 50,
sortable: true,
dataIndex: 'id',
hidden: false
}, {
header: 'Object',
width: 120,
sortable: true,
dataIndex: 'object',
renderer: columnWrap
}, {
header: 'Status',
width: 90,
sortable: true,
dataIndex: 'status'
}, {
header: 'Rank',
width: 90,
sortable: true,
dataIndex: 'rank'
}, {
header: 'Last Updated',
width: 120,
sortable: true,
renderer: Ext.util.Format.dateRenderer('Y-m-d H:i:s'),
dataIndex: 'lastChange'
}],
viewConfig: {
forceFit: true
},
title: 'Computer Information',
width: 500,
autoHeight: true,
frame: true,
listeners: {
'rowdblclick': function(grid, index, rec){
var id = grid.getSelectionModel().getSelected().json[0];
go_to_page('edit_item', 'id=' + id);
}
}
});
What do I have to do to change the text color of only the cells in the second row?
You want to override getRowClass, which is passed into the GridPanel's viewConfig. An example is provided in GridPanel's API docs:
viewConfig: {
forceFit: true,
// Return CSS class to apply to rows depending upon data values
getRowClass: function(record, index) {
var c = record.get('change');
if (c < 0) {
return 'price-fall';
} else if (c > 0) {
return 'price-rise';
}
}
},
GridView contains the method, so look there in the API for more information:
getRowClass( Record record, Number index, Object rowParams, Store store )
Override this function to apply custom
CSS classes to rows during rendering.
You can also supply custom parameters
to the row template for the current
row to customize how it is rendered
using the rowParams parameter. This
function should return the CSS class
name (or empty string '' for none)
that will be added to the row's
wrapping div. To apply multiple class
names, simply return them
space-delimited within the string
(e.g., 'my-class another-class').
You can apply the CSS to a certain row by using the index argument.
not sure if this helps, but you can try. I tried locally, it does change the color of all the characters in 2nd row.
Add listener to the grid:
listeners:{
viewready: function(g){
g.getView().getRow(1).style.color="#f30";
}
}
"When viewready, you can get the row's div by using getRow() method from GridView. After that is normal Javascript of assigning colors (or adding extra class, if you want)
Cheers
EDIT
I realize it's not working if your store is retrieving data remotely. View is ready but data is not ready. This method would fail.

Resources