EXTJS 6 Grid Column Renderer Function - where do you define it? - extjs

In EXTJS 4/5 you could create an inline function which you could then use as a grid column renderer, like this
function myColumnRenderer(value, metaData, record, rowIndex, colIndex, store){
//do something with the data and return
return value;
}
and in your grid's column definition you would reference the renderer like this
columns:[
{ text: 'ColA', dataIndex: 'ColA', renderer: myColumnRenderer},
{ text: 'ColB', dataIndex: 'ColB', renderer: myColumnRenderer}
]
In EXTJS 6.5, can you still do this and if so where do you define the renderer function? In the controller, or viewModel, or elsewhere? I've tried putting the function in the controller and putting this.myColumnRenderer in the renderer of the column but it never seems to get called.
Looks like this is an option, just not sure if it's the correct way to do it
columns:[
{ text: 'ColA', dataIndex: 'ColA', renderer: function(value, metaData, record, rowIndex, colIndex, store) {
return this.getController().myColumnRenderer(value, metaData, record, rowIndex, colIndex, store);
}},
{ text: 'ColB', dataIndex: 'ColB', renderer: function(value, metaData, record, rowIndex, colIndex, store) {
return this.getController().myColumnRenderer(value, metaData, record, rowIndex, colIndex, store);
}}
]

In ExtJS 6 you can also create but inline function but instead of inline function we can create ViewController for grid or any view like below example
Ext.define('MyViewController', {
extend : 'Ext.app.ViewController',
alias: 'controller.myview',
// This method is called as a "handler" for the Add button in our view
onAddClick: function() {
Ext.Msg.alert('Add', 'The Add button was clicked');
}
});
Ext.define('MyView', {
extend: 'Ext.Panel',
controller: 'myview',
items: [{
xtype: 'button',
text: 'Add',
handler: 'onAddClick', // calls MyViewController's onAddClick method
}]
});
In this FIDDLE, I have create a demo using viewController and grid component. I hope this will help/guide you to achieve your requirement.
CODE SNIPPET
Ext.application({
name: 'Fiddle',
launch: function () {
Ext.define('GridController', {
extend: 'Ext.app.ViewController',
alias: 'controller.gridcntr',
//This event will fire for grid column renderer
onColRender: function (value, metaData, record, rowIndex, colIndex, store) {
console.log(value);
return value;
}
});
var store = Ext.create('Ext.data.Store', {
fields: ['name', 'email', 'phone'],
data: [{
'name': 'Lisa',
"email": "lisa#simpsons.com",
"phone": "555-111-1224"
}, {
'name': 'Bart',
"email": "bart#simpsons.com",
"phone": "555-222-1234"
}, {
'name': 'Homer',
"email": "home#simpsons.com",
"phone": "555-222-1244"
}, {
'name': 'Marge',
"email": "marge#simpsons.com",
"phone": "555-222-1254"
}]
});
Ext.create({
xtype: 'grid',
controller: 'gridcntr',
title: 'user data',
store: store,
columns: [{
text: 'Name',
dataIndex: 'name',
width: 200,
renderer: 'onColRender'
}, {
text: 'Email',
dataIndex: 'email',
width: 250,
renderer: 'onColRender'
}, {
text: 'Phone',
dataIndex: 'phone',
width: 120,
renderer: 'onColRender'
}],
layout: 'fit',
fullscreen: true,
renderTo:Ext.getBody()
});
}
});

You can use the renderer like this ExtJs 6
columns: [
{ width:"100%", renderer: function(value,metaData,records,view) {
//your conditions
}}]

Related

How to hide and show the buttons located inside a grid in Ext JS?

I have a grid looking something like this:
Ext.create('Ext.grid.Grid', {
title: 'myGrid',
store: 'myStore',
columns: [
{ text: 'Name', dataIndex: 'name'},
{ text: 'Running', dataIndex: 'running' },
{
xtype: 'actioncolumn',
text:'play or stop',
items:[
{
iconCls: 'x-fa fa-play-circle',
handler:function(grid, rowIndex, colIndex){ play(); }
}, {
iconCls: 'x-fa fa-stop-circle',
handler:function(grid, rowIndex, colIndex){ stop(); }
}
]
}
]
});
It works fine. In the third column there are two buttons: a "play button" and a "stop button". Now they are always visible but I want the play button to be visible only when running==false, and the stop button to be visible only when running==true. How can I achieve that?
You can use the getClass config, which can be specified for the actioncolumn itself or for child items of the actioncolumn. docs
Then you can just do something like this:
Ext.create('Ext.grid.Grid', {
title: 'myGrid',
store: 'myStore',
columns: [
{text: 'Name', dataIndex: 'name'},
{text: 'Running', dataIndex: 'running'},
{
xtype: 'actioncolumn',
text: 'play or stop',
items: [
{
getClass: function (value, metadata, record) {
return record.data.running ? '' : 'x-fa fa-play-circle';
},
handler: function (grid, rowIndex, colIndex) {
play();
}
}, {
getClass: function (value, metadata, record) {
return record.data.running ? 'x-fa fa-stop-circle' : '';
},
handler: function (grid, rowIndex, colIndex) {
stop();
}
}
]
}
]
});
This worked:
Ext.create('Ext.grid.Grid', {
title: 'myGrid',
store: 'myStore',
columns: [
{text: 'Name', dataIndex: 'name'},
{text: 'Running', dataIndex: 'running'},
{
xtype: 'actioncolumn',
text: 'play or stop',
items: [
{
getClass: function (value, metadata, record) {
return record.data.running ? 'x-fa fa-stop-circle' : 'x-fa fa-play-circle';
},
handler: function (grid, rowIndex, colIndex) {
playOrStop();
}
},
]
}
]
});

ExtJS - Grid Cell Tool Tip

I am trying to create a tooltip for cells. Below code does that, but tooltip appears only onClick(in mozilla) and in IE tooltip appears on mouseOver but display value of last clicked cell.
I wanted a tooltip on grid in mouseOver with cells content as tooltip display value.
Please suggest any other way to achieve that. Thanks in advance.
var grid = Ext.getCmp('your_grid_id'); // Enter your grid id
initToolTip(grid); // call function
initToolTip: function(grid) {
var view = grid.view;
// record the current cellIndex
grid.mon(view, {
uievent: function(type, view, cell, recordIndex, cellIndex, e) {
grid.cellIndex = cellIndex;
grid.recordIndex = recordIndex;
}
});
grid.tip = Ext.create('Ext.tip.ToolTip', {
target: view.el,
delegate: '.x-grid-cell',
trackMouse: true,
renderTo: Ext.getBody(),
listeners: {
beforeshow: function updateTipBody(tip) {
if (!Ext.isEmpty(grid.cellIndex) && grid.cellIndex !== -1) {
header = grid.headerCt.getGridColumns()[grid.cellIndex];
columnText = grid.getStore().getAt(grid.recordIndex).get(header.dataIndex);
tip.update(columnText);
}
}
}
});
You could use renderer config for gridcolumn and inside of renderer you could return a html tag with you data-qtip based on your record what you need to show.
You can check here with working FIDDLE.
CODE SNIPPET
Ext.application({
name: 'Fiddle',
launch: function () {
Ext.create('Ext.data.Store', {
storeId: 'simpsonsStore',
fields: ['name', 'email', 'phone'],
data: [{
'name': 'Lisa',
"email": "lisa#simpsons.com",
"phone": "555-111-1224"
}, {
'name': 'Bart',
"email": "bart#simpsons.com",
"phone": "555-222-1234"
}, {
'name': 'Homer',
"email": "homer#simpsons.com",
"phone": "555-222-1244"
}, {
'name': 'Marge',
"email": "marge#simpsons.com",
"phone": "555-222-1254"
}]
});
Ext.create('Ext.grid.Panel', {
title: 'Simpsons',
store: 'simpsonsStore',
columns: [{
text: 'Name',
dataIndex: 'name',
renderer: function (value, metaData, record, rowIndex, colIndex, store) {
return `<span data-qtip="This is ${value}"> ${value} </span>`;
}
}, {
text: 'Email',
dataIndex: 'email',
flex: 1
}, {
text: 'Phone',
dataIndex: 'phone'
}],
height: 200,
renderTo: Ext.getBody()
});
}
});

displaying minus in grid extjs. displaying only NOT CHANGE the value to minus

i'm new comer in ext js..
i want to display one of field in grid ext js. the field type is smallint. let's say it "Dayfrom". I want to display DayFrom in Grid like (-3) days in minus without calculate the value. only display in grid.
i have try to for this. but not work
var s = Ext.String.format('<div class="{0}">{1}</div>','-','--');
storePendingApprovalDetail = Ext.create('Ext.data.Store', {
storeId: 'pendingapprovaldetail-store',
model: 'pendingapprovaldetail-model',
sorters: ['DayFrom']
});
gridPendingApprovalDetail = Ext.create('Ext.grid.Panel', {
store: 'pendingapprovaldetail-store',
columns: [{
text: 'Day From',
flex: 1,
renderer: s,
dataIndex: 'DayFrom'
}, {
text: 'Day To',
flex: 1,
dataIndex: 'DayTo'
}, {
text: 'Frequent',
flex: 1,
dataIndex: 'Frequent'
}],
dockedItems: [{
xtype: 'toolbar',
items: [actAddPendingApprovalDetail, actEditPendingApprovalDetail, actDeletePendingApprovalDetail]
}],
listeners: {
}
});
Have you looked at the renderer example at http://docs.sencha.com/extjs/4.2.2/#!/api/Ext.grid.column.Column-cfg-renderer ?
Do you actually know what Ext.String.format does? If not, please read http://docs.sencha.com/extjs/4.2.2/#!/api/Ext.String-method-format
If you had done so, and understood, your code for the renderer would be sth like this:
renderer: function(value){
if(typeof value=="number") return -value;
return value;
}
many thanks buddies... :)
i'm using this and it works
gridPendingApprovalDetail = Ext.create('Ext.grid.Panel', {
store: 'pendingapprovaldetail-store',
columns: [{
text: 'Day From',
flex: 1,
dataIndex: 'DayFrom', renderer: function (value, metaData, record, rowIndex, colIndex, store) {
var returnString = "-" + value;
return returnString;
}
}, {
text: 'Day To',
flex: 1,
dataIndex: 'DayTo'
}, {
text: 'Frequent',
flex: 1,
dataIndex: 'Frequent'
}],
dockedItems: [{
xtype: 'toolbar',
items: [actAddPendingApprovalDetail, actEditPendingApprovalDetail, actDeletePendingApprovalDetail]
}],
listeners: {
}
});

Add Ext.Button to ExtJS columnmodel

I'm creating an Ext.grid.GridPanel. I am trying to add column with xtype: button to the column model. I am not sure, if I can do that though. Below is my code and also this is a link to jsfiddle http://jsfiddle.net/bXUtQ/
I am using extjs 3.4
Ext.onReady(function () {
var myData = [[ 'Lisa', "lisa#simpsons.com", "555-111-1224"],
[ 'Bart', "bart#simpsons.com", "555-222-1234"],
[ 'Homer', "home#simpsons.com", "555-222-1244"],
[ 'Marge', "marge#simpsons.com", "555-222-1254"]];
var store = new Ext.data.ArrayStore({
fields:[ {
name: 'name'
},
{
name: 'email'
},
{
name: 'phone'
}],
data: myData
});
var grid = new Ext.grid.GridPanel({
renderTo: 'grid-container',
columns:[ {
header: 'Name',
dataIndex: 'name'
},
{
header: 'Email',
dataIndex: 'email'
},
{
header: 'Phone',
dataIndex: 'phone'
},
{
header: 'action',
xtype: 'actioncolumn',
iconCls: 'delete-icon'
text: 'Delete',
name: 'deleteBtn',
handler: function(grid, rowIndex, colIndex, item, e) {
alert('deleted');
}
},
//////////////////////////////
//I cannot add this column
{
header: 'action',
xtype: 'button',
text: 'update',
name: 'btnSubmit'
}
],
store: store,
frame: true,
height: 240,
width: 500,
title: 'Framed with Row Selection',
iconCls: 'icon-grid',
sm: new Ext.grid.RowSelectionModel({
singleSelect: true
})
});
});
You can't use a button as a column just like that. We're using the following UX to achieve just what you're asking. Unfortunately this is for ExtJS 4.1:
http://www.sencha.com/forum/showthread.php?148064-Component-Column-Components-in-Grid-Cells
You can try to Grid RowActions.
Will you try this as your actioncolumn
{
xtype: 'actioncolumn',
width: 50,
items:
[
{
icon: 'app/resources/images/cog_edit.png', // Use a URL in the icon config
tooltip: 'Edit',
handler: function (grid, rowIndex, colIndex) {
var rec = grid.getStore().getAt(rowIndex);
alert("Edit " + rec.get('name'));
}
},
{
icon: 'app/resources/images/delete.png',
tooltip: 'Delete',
handler: function (grid, rowIndex, colIndex) {
var rec = grid.getStore().getAt(rowIndex);
alert("Terminate " + rec.get('name'));
}
}
]
}
Or You can try this Plugin for actioncolumnbutton

ExtJS 4 cell "renderer" problem

After reading this article, I've managed to change rendering.
I'm calling an internal function:
renderer: this.onRenderCell
And this function is like this:
onRenderCell: function(value, metaData, record, rowIndex,
colIndex, store, view) {
metaData.css = 'ini-cell-pas-traduit';
return '«'+value+'»';
}
If you read carefully I return '«'+value+'»'; so for each value it is transformed to: '«value»'; . This is a proof that on every single line, it works perfectly. So should it be for the css. But the css is applied one time out of two!! This drives me nuts.
Here's what it gives (latest Firefox, same with latest Chrome):
Any idea where I should take a look?
Here's a big sample of my source code:
Ext.define('Lang.grid.Panel', {
extend: 'Ext.grid.Panel',
alias: 'widget.langgrid',
requires: [
'Ext.grid.plugin.CellEditing',
'Ext.form.field.Text',
'Ext.toolbar.TextItem'
],
initComponent: function(){
this.editing = Ext.create('Ext.grid.plugin.CellEditing');
Ext.apply(this, {
iconCls: 'icon-grid',
plugins: [this.editing],
dockedItems: [{
xtype: 'toolbar',
items: [{
iconCls: 'icon-add',
text: 'Add',
scope: this,
handler: this.onAddClick
}, {
iconCls: 'icon-delete',
text: 'Delete',
disabled: true,
itemId: 'delete',
scope: this,
handler: this.onDeleteClick
}]
}],
columns: [{
text: 'label',
flex:2,
sortable: true,
dataIndex: 'label'
},{
header: 'fr',
flex: 3,
sortable: true,
dataIndex: 'fr',
renderer: this.onRenderCell,
field: {
type: 'textfield'
}
},{
header: 'es',
flex: 3,
sortable: true,
dataIndex: 'es',
renderer: this.onRenderCell,
field: {
type: 'textfield'
}
},{
header: 'us',
flex: 3,
sortable: true,
dataIndex: 'us',
renderer: this.onRenderCell,
field: {
type: 'textfield'
}
}
]
});
this.callParent();
this.getSelectionModel().on('selectionchange', this.onSelectChange, this);
},
(...)
(snip useless code)
(...)
onRenderCell: function(value, metaData, record, rowIndex,
colIndex, store, view) {
metaData.css = 'ini-cell-pas-traduit';
return '<span style="color:red; font-weight:bold;">«'+
value+'»</span>';
}
});
In the metadata.css (ini-cell-pas-traduit) do this for background-color
background-color : red !important //or whichever color you've specified.
EDIT :
This is happening because the grid is configured with stripeRows : true. I dunno if this is done by default or you did it in the config but forgot to mention it here. When you use stripeRows it sets a background-color which can be overriden using the !important keyword.
Varun Achar is right about using !important, but since you are using Ext JS 4 you should also change
metaData.css = 'ini-cell-pas-traduit';
to
metaData.tdCls = 'ini-cell-pas-traduit';
The 'css' and 'attr' members of metaData have now been replaced with tdCls and tdAttr and the old ones will only work in Ext JS 4 if you also install the Ext 3 compatibility layer.

Resources