extjs apply formatting to grid column - extjs

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

Related

How to dynamic set different tpl for widgetcolumn

friends!
I have a grid, with a widget column. I get the data for the store from the server.
I get different templates for each row and try to set these templates in my widget. However, I am not able to display the data in the templates.
Here is my example fiddle https://fiddle.sencha.com/#fiddle/3j41&view/editor
I expect the second column to display data with different templates. Instead of data I see {testName}.
In the third column everything works, but it's static data
Can you help me understand what's wrong?
My result:
My store:
var store = Ext.create('Ext.data.Store', {
fields: ['name', 'tplConfig'],
data: [{
name: 'Test 1',
tplConfig: {
tplBody: '<div><b>Name:</b> {testName}</div> <div>any text</div>',
tplData: {
testName: 'Alex'
}
}
} ]
});
My grid:
{
xtype: 'grid',
title: 'Widget Column Demo',
store: store,
columns: [
.....
{
xtype: 'widgetcolumn',
text: 'Dynamic',
width: 120,
dataIndex: 'tplConfig',
widget: {
xtype: 'component',
tpl: '{tplBody}',
//data: '{tplData}' //it's not working
}
}
.......
]
}
You can use renderer:
{
xtype: 'gridcolumn',
text: 'Dynamic',
width: 120,
dataIndex: 'tplConfig',
renderer: function(tplConfig) {
var t = new Ext.Template(tplConfig.tplBody);
return t.apply(tplConfig.tplData);
}
}
or as a 1 liner:
return new Ext.Template(tplConfig.tplBody).apply(tplConfig.tplData);

How to create editable grid column with few values? | Extjs

I have a grid with columns name, value, new value. New value column is combobox because there can be a few values. Admin can approve/decline new values and correct proposed these new values.
My grid columns looks like this:
columns: [
{ text: 'Name', dataIndex: 'property_name', width: 300 },
{ text: 'Value', dataIndex: 'value', width: 350 },
{
text: 'New Value',
dataIndex: 'new_value',
width: 350,
editor: { allowBlank: true },
renderer: function(value, cell, record){
return record.get('new_value') == record.get('value') ? '' : record.get('new_value');
}
},
],
So is there a way to achive functionality a described above?
The editor configuration takes any Ext.form.Field configuration (default is textfield), so for example:
editor:{
xtype:'combobox', // <- this tells
store:'MyNewValueStore',
forceSelection:true,
queryMode:'local',
...
}

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

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: '%'
},

EXTJS how to get the component without declare id?

Ext.define('MyApp.view.MyVehicleGridPanel', {
extend: 'Ext.grid.Panel',
alias: 'widget.mygrid',
header: false,
store: UserStore,
multiSelect: false,
columns: [
{
xtype: 'gridcolumn',
dataIndex: '_id',
text: 'Vehicle ID'
},
{
xtype: 'gridcolumn',
width: 126,
dataIndex: 'Plat_No',
text: 'Plat Number'
},
{
xtype: 'gridcolumn',
width: 200,
dataIndex: 'Name',
text: 'Added By'
}
]
})
i dont have any id declare in the gridpanel, because it will used in dynamicly,
so, i m using alias to find my grid component like below code
var grid = Ext.ComponentQuery.query('mygrid');
console.log( Ext.ComponentQuery.query('mygrid') );
if (grid.getSelectionModel().hasSelection()) { //error at here
var row = grid.getSelectionModel().getSelection()[0];
console.log(row.get('Plat_No'));
};
But, firebug return error with TypeError: grid.getSelectionModel is not a function
any other way to find my gridpanel component?
Ext.ComponentQuery.query() returns an array of matched Components from within the passed root object.
So if you have only one mygrid component in your application, you can get your grid like this:
var grid = Ext.ComponentQuery.query('mygrid')[0];

Numbering Grid Rows in Ext JS Ext.grid.panel

I am trying to get numbered rows with my grid.
var grid = Ext.create('Ext.grid.Panel',
{
I looked into the documentation for Grids in Ext JS, however, I cannot find a numbering property. Any ideas for me?
Here's my columns section. The info in the columns is pulled from a server.
columns:
{
xtype: 'rownumberer',
defaults:
{
align: 'center',
flex: 0,
width: 40,
sortable: false,
menuDisabled: true
},
items:
[
Use the RowNumberer column type.
Edit:
You must add an extra column of this type. Example:
Ext.widget('grid', {
columns: {
defaults: {
// you column defaults
},
items: [{
xtype: 'rownumberer' // this is a column type
},{
dataIndex: 'foo'
,text: 'Column Foo'
},{
dataIndex: 'bar'
,text: 'Column Bar'
}]
}
// ...
})

Resources