EXTJS4: Compare two fields in GridPanel with each other - extjs

I have a Grid-Panel with a few columns.
Now I want to set a new Class in a Column of a Row, when the values don't match with each other.
But how can i get success to a column in a different row?
Her is my code which I tried, but it says, the ID is undefined:
...{
header: 'CRS',
dataIndex: 'crs',
id:'crs',
flex:1,
renderer: function(value, meta) {
console.log("csv-->"+Ext.getCmp('csv').value);
if(value==Ext.getCmp('csv').value)
{
//change class
}
}
},
{
header: 'CSV',
dataIndex: 'csv',
id:'csv',
flex:1
},...

The code you've posted does not seem to match what you're asking for. According to your code, it appears that you're trying to compare values across columns in the same row, not a different row. So which is it?
Anyway, assuming that your code is indicative of what you want, be sure to look at the docs for the the renderer: http://docs.sencha.com/extjs/4.2.1/#!/api/Ext.grid.column.Column-cfg-renderer
One of the arguments passed to the renderer method is the "record", which will contain all the values for the record which is filling the values for the entire row. If you wanted to compare values across columns, you could do something like:
if( value==record.get( 'csv ') ) {
...do something here...
}
If you really need to compare values across rows, then the "store" also gets passed as one of the arguments to renderer, so you could compare values against specific row values that way.
Alternatively, you could do most of this in Model itself. If you are just comparing columns in the same row, you could create an additional field in your Model that stores the result of the comparison. If you did that, all that your renderer would need to do is switch on the value of that new field, rather than doing the entire comparison AND rendering.

I have it! This works for me:
{
header: 'CSV',
dataIndex: 'csv',
flex:1,
renderer: function(grid, rowIndex, rowdata) {
var csv_data=rowdata.data.csv;
var crs_data=rowdata.data.crs;
if (csv_data != crs_data) {
rowIndex.tdCls = "red-class";
}
return csv_data;
}
},

{
header: 'CSV',
dataIndex: 'csv',
flex:1,
renderer: function(value, meta, record) {
var crs = record.get('crs');
if (value && crs)
{
if (value != crs) {
meta.tdCls = "red-class";
}
}
return value;
}
},
Rewritten for ExtJS 4, you can use the value as you set it in the dataIndex, and because that's a record the recommended extjs 4 way to get the value would be the get method.
Just a heads up there are a ton more properties on renderer, you can find the full list here:
http://docs.sencha.com/extjs/4.2.2/#!/api/Ext.grid.column.Column-cfg-renderer

Related

How to 'get' another Model field on ExtJS 5.1.1

I'm trying to use renderer config inside Ext.column. I have two fields states in Model; balanceok and lastbalance. But it gives this error:
[W] XTemplate evaluation exception: getRecord is not defined
How I can display another field with get method?
Model:
Ext.define('MultiDB.model.FolioModel', {
extend: 'Ext.data.Model',
fields: [
{name: 'clientname', type: 'string'},
{name: 'balanceok', type: 'bool'},
{name: 'lastbalance', type: 'int'}
]
Grid Panel:
{
dataIndex: 'balanceok',
flex: 1,
text: 'Balance',
renderer: function (value, record, store) {
if (value == 1) {
return "All Paid";
} else {
return getRecord('lastbalance');
}
}
}
Renderer already provides a record argument. You can use getRecord when focusing on a grid cell, but it looks like you do not need this from the use case described above.
It looks like you have the renderer arguments a little off, which may be why you are having an issue checking and retrieving the value.
If you update your column renderer from:
renderer: function (value, record, store) { ... }
to:
renderer: function (value, metaData, record, rowIndex, colIndex, store, view) { ... }
You can see a full description of render method here:
http://docs.sencha.com/extjs/5.1.1/api/Ext.grid.column.Column.html#cfg-renderer
Take a look at the fiddle below for a working example that will help get you going:
https://fiddle.sencha.com/#view/editor&fiddle/251r
Not sure where you found getRecord, the correct call would be:
record.get('lastbalance');
As a side note, the check for value == 1 is a little odd since the field will be casted to a boolean because of the model.

How to disable the widget in a widgetcolumn of a grid based on the attribute of a record in Sencha ExtJS v6?

More or less the configuration looks like this:
{
xtype: 'widgetcolumn',
//text: "", //localize
dataIndex: "carStatusButton",
//disable action for the column
sortable: false,
filter: false,
menuDisabled: true,
widget: {
xtype: 'changeavailbutton',
//reference: "gridCarStatusButton", we cannot use reference because we get a duplicate reference warning
//text is being automatically generated from dataIndex of widgetcolumn
/*
//didn't work!
defaultBindProperty: "curCarStatus", //default is text
curCarStatus: "aaaaaaaa",
setCurCarStatus: function (value) {
this.curCarStatus = value;
},*/
/*
getCurCarStatus: function () {
return "aaaaaa"
},
setCurCarStatus: function (value) {
},*/
/*text: (function() {
return this.enableToggle;
})(),
bind: {
},*/
},
}
We have considered using the updater(cell, value, record, view) but it does not get called initially
We have considered using the renderer(value, metadata, record) but we can only affect the value, it does not give us any help with the widget
We considered to use a custom defaultBindProperty in the widget like this:
defaultBindProperty: "curCarStatus", //default is text
curCarStatus: "",
setCurCarStatus: function (value) {
this.curCarStatus = value;
}
The above helped to avoid creating an extra field in the model that would be necessary. In other words initially we had a field in the model, as a transient field to get the calculated value inside the dataIndex of the widgetcolumn but didn't bring any help on what we were trying to achieve.
The fact is that (from documentation) widgetcolumn binds the dataIndex to the defaultBindProperty of the widget. One problem is that there is a bind that happens in the background that we are not aware of its key value. It would look like that if it was a configuration:
bind: {
text: "{unknownProperty}"
}
If we knew how the property was called it could be helpful to use it in various properties because in our situation we need to bind more than one properties to the widget.
We are actually looking similar functionality provided by isDisabled provides to an ActionColumn to have it in a WidgetColumn.
In the widgetcolumn itself:
onWidgetAttach: function (column, widget) {
if (widget.getWidgetRecord().get('property')) {
widget.setDisabled(false)
} else {
widget.setDisabled(true)
}
}
And the grid can be updated with grid.getView().refresh() to reflect any changes

Extjs Grid - How to get width of column in renderer function

I have a gridpanel and one of my column like
columns: [
...
{ text: 'Name', dataIndex: 'name' , width: 200,
renderer: function (value) {
// how to get width of this column (200)
//alert(this.width); // this is width of gridpanel
}
}
...
],
how to get width of this column thanks.
Fairly certain this isn't the ideal way to do it, but it works. There are lots of other values passed to the renderer function besides just the value. Via dom inspection, this will get you the column width, but I don't think it's something I would trust to work as they update ExtJS:
renderer: function (value, metaData, record, rowIndex, colIndex, store, view) {
return view.ownerCt.columns[colIndex].getWidth();
}
Alternatively, you could use component query to get the grid panel view, use the row index to find the column, and then get it's width the same way:
renderer: function (value, metaData, record, rowIndex, colIndex, store, view) {
var grid = Ext.ComponentQuery.query('#gridId')[0];
return grid.columns[colIndex].getWidth();
}
I really don't know what is the better option in this case, and don't really know why they pass the gridview rather than the gridpanel as the view in the renderer function. In any case, this should get you started, good luck.

ExtJS displaying field name inside chart

Consider the following example of a stacked bar chart. I can display the count of each of the fields (comedy, action, drama, thriller) in the bars by adding the label config in the series, but how would I also display the field name?. The renderer config property for label doesn't seem to be of much use as the function only receives the count as argument.
label:{
display:'insideStart' ,
field:[null, null, 'drama', 'thriller'],
renderer:function(item){
//For this case, item will be an int value. Thus, not sure how useful it is
//as we cannot get the field name from it.
}
}
Actually, the renderer function is passed a lot more arguments than just the value. These arguments are the same as the onPlaceLabel method, with the value added to the beginning, and they are better documented there.
We've got the index of the field in the series and, as a matter of fact, we have the series also available in the item argument. With that, we can achieve what you want:
label: {
display:'insideStart'
,field:[null, null, 'drama', 'thriller']
,renderer: function(value, label, storeItem, item, i, display, animate, index) {
var series = item.series,
titles = series.title;
return titles && titles[index] || series.yField[index];
}
}
I'm trying to get the title first because, in real life, I wouldn't display a raw field name to the user. For the record, here's how the whole serie would be configured in order to do that. It doesn't appear in the doc, except for a user comment...
series: [{
type: 'bar',
axis: 'bottom',
gutter: 80,
xField: 'year',
yField: ['comedy', 'action', 'drama', 'thriller'],
title: ['Comédie', 'Action', 'Drame', 'Thriller'],
stacked: true,
label: {
display:'insideStart'
,field:[null, null, 'drama', 'thriller']
,renderer: function(value, label, storeItem, item, i, display, animate, index) {
var series = item.series,
titles = series.title;
return titles && titles[index] || item.yField;
}
}
}]

show/hide column in grid conditionally in extjs

I have grid in my application, I need to show/hide particular column conditionally. Please help me.
Code:
{
header: 'Type', dataIndex: 'mirror', flex: 1, sortable: true,
renderer: function (value, meta, record, rowIndex, colIndex, store) {
if(value == '')
return "-";
else
return value;
}
}
currently it shows - incase no records,but I need to remove column incase no record found.
Columns have a hide method which you can use. For example, take a look at this example that show you how to hide an specific column in runtime.
If you want to hide/show a the column depending on a condition, you have to evaluate that condition and use the hide & show methods.

Resources