Animal
28 Jul 2010, 11:37 PM
This plugin is for 3.3 onwards of ExtJS.
It renders an editable star rating from an INTEGER field in the Record.
The optional (defaults to 5) size config specifies the total number of stars.
This image
http://i131.photobucket.com/albums/p286/TimeTrialAnimal/rating.gif
needs to be in examples/shared/icons
Then drop this into examples/<anywhere>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Rating Column Example</title>
<link rel="stylesheet" type="text/css" href="../../resources/css/ext-all.css" />
<link rel="stylesheet" type="text/css" href="../shared/examples.css" />
<link rel="stylesheet" type="text/css" href="grid-examples.css" />
<style type=text/css>
.x-action-col-cell img.ux-rating-icon {
height: 16px;
width: 16px;
background-image: url(../shared/icons/rating.gif);
}
.x-action-col-cell img.ux-rating-icon-on {
background-position: 0 -16px;
}
.x-action-col-cell img.ux-rating-icon-hover {
background-position: 0 -32px;
}
</style>
<script type="text/javascript" src="../../adapter/ext/ext-base-debug.js"></script>
<script type="text/javascript" src="../../ext-all-debug.js"></script>
<script type="text/javascript">
Ext.ux.RatingColumn = Ext.extend(Ext.grid.ActionColumn, {
size: 5, // Number of icons in the column
inconIndexRe: /ux-rating-icon-(\d+)/,
constructor: function(config) {
var items = config.items = [],
i = 1,
l = ((config.size || this.size) + 1);
for (; i < l; i++) {
items.push({});
}
Ext.ux.RatingColumn.superclass.constructor.call(this, config);
this.renderer = this.renderer.createInterceptor(this.setItemClasses);
},
// When we are initialized as a plugin, hook into the grid's render evet
init: function(grid) {
grid.on({
render: this.onHostGridRender,
single: true
});
},
// Route mousemove and mouseout events of the Grid's body through the View's event processing so we get control with value added (rowIdx)
onHostGridRender: function(g) {
g.getView().mainBody.on({
mousemove: function(e) {
g.view.processEvent('mousemove', e);
},
mouseout: function(e) {
g.view.processEvent('mouseout', e);
}
})
},
setItemClasses: function(v) {
for (var i = 0, it = this.items, l = it.length; i < l; i++) {
it[i].iconCls = 'ux-rating-icon ux-rating-icon-' + (i + 1);
if (i < v) {
it[i].iconCls += ' ux-rating-icon-on';
}
}
},
processEvent: function(evtName, e, grid, rowIndex, colIndex) {
var t = Ext.get(e.getTarget()),
match, iconIdx, icons, i, rec;
switch (evtName) {
case 'mouseout':
if (t.dom.tagName != 'img') {
this.activeCell && this.activeCell.select('img').removeClass('ux-rating-icon-hover');
delete this.activeCell;
delete this.ignoreMouseMove;
}
break;
case 'mousemove':
if (match = t.dom.className.match(this.inconIndexRe)) {
this.activeCell = t.up('');
if ((iconIdx = parseInt(match[1], 10)) != this.ignoreMouseMove) {
delete this.ignoreMouseMove;
icons = this.activeCell.query('img');
for (i = 0; i < icons.length; i++) {
Ext.fly(icons[i])[(i < iconIdx) ? 'addClass' : 'removeClass']('ux-rating-icon-hover');
}
}
}
break;
case 'click':
if (match = t.dom.className.match(this.inconIndexRe)) {
rec = grid.store.getAt(rowIndex);
if ((iconIdx = parseInt(match[1], 10)) == rec.get(this.dataIndex)) {
rec.set(this.dataIndex, 0);
} else {
rec.set(this.dataIndex, iconIdx);
}
this.ignoreMouseMove = iconIdx;
}
default:
} // End switch
// Return any event handler return statuses to honour event cancelling
return Ext.ux.RatingColumn.superclass.processEvent.apply(this, arguments);
}
});
Ext.onReady(function(){
Ext.QuickTips.init();
// NOTE: This is an example showing simple state management. During development,
// it is generally best to disable state management as dynamically-generated ids
// can change across page loads, leading to unpredictable results. The developer
// should ensure that stable state ids are set for stateful components in real apps.
Ext.state.Manager.setProvider(new Ext.state.CookieProvider());
// sample static data for the store
var myData = [
['3m Co',71.72,0.02,0.03,'9/1 12:00am', 1],
['Alcoa Inc',29.01,0.42,1.47,'9/1 12:00am', 2],
['Altria Group Inc',83.81,0.28,0.34,'9/1 12:00am', 3],
['American Express Company',52.55,0.01,0.02,'9/1 12:00am', 4],
['American International Group, Inc.',64.13,0.31,0.49,'9/1 12:00am', 5],
['AT&T Inc.',31.61,-0.48,-1.54,'9/1 12:00am', 1],
['Boeing Co.',75.43,0.53,0.71,'9/1 12:00am', 2],
['Caterpillar Inc.',67.27,0.92,1.39,'9/1 12:00am', 3],
['Citigroup, Inc.',49.37,0.02,0.04,'9/1 12:00am', 4],
['E.I. du Pont de Nemours and Company',40.48,0.51,1.28,'9/1 12:00am', 5],
['Exxon Mobil Corp',68.1,-0.43,-0.64,'9/1 12:00am', 1],
['General Electric Company',34.14,-0.08,-0.23,'9/1 12:00am', 2],
['General Motors Corporation',30.27,1.09,3.74,'9/1 12:00am', 3],
['Hewlett-Packard Co.',36.53,-0.03,-0.08,'9/1 12:00am', 4],
['Honeywell Intl Inc',38.77,0.05,0.13,'9/1 12:00am', 5],
['Intel Corporation',19.88,0.31,1.58,'9/1 12:00am', 1],
['International Business Machines',81.41,0.44,0.54,'9/1 12:00am', 2],
['Johnson & Johnson',64.72,0.06,0.09,'9/1 12:00am', 3],
['JP Morgan & Chase & Co',45.73,0.07,0.15,'9/1 12:00am', 4],
['McDonald\'s Corporation',36.76,0.86,2.40,'9/1 12:00am', 5],
['Merck & Co., Inc.',40.96,0.41,1.01,'9/1 12:00am', 1],
['Microsoft Corporation',25.84,0.14,0.54,'9/1 12:00am', 2],
['Pfizer Inc',27.96,0.4,1.45,'9/1 12:00am', 3],
['The Coca-Cola Company',45.07,0.26,0.58,'9/1 12:00am', 4],
['The Home Depot, Inc.',34.64,0.35,1.02,'9/1 12:00am', 5],
['The Procter & Gamble Company',61.91,0.01,0.02,'9/1 12:00am', 1],
['United Technologies Corporation',63.26,0.55,0.88,'9/1 12:00am', 2],
['Verizon Communications',35.57,0.39,1.11,'9/1 12:00am', 3],
['Wal-Mart Stores, Inc.',45.45,0.73,1.63,'9/1 12:00am', 4]
];
/**
* Custom function used for column renderer
* @param {Object} val
*/
function change(val){
if(val > 0){
return '<span style="color:green;">' + val + '</span>';
}else if(val < 0){
return '<span style="color:red;">' + val + '</span>';
}
return val;
}
/**
* Custom function used for column renderer
* @param {Object} val
*/
function pctChange(val){
if(val > 0){
return '<span style="color:green;">' + val + '%</span>';
}else if(val < 0){
return '<span style="color:red;">' + val + '%</span>';
}
return val;
}
// create the data store
var store = new Ext.data.ArrayStore({
fields: [
{name: 'company'},
{name: 'price', type: 'float'},
{name: 'change', type: 'float'},
{name: 'pctChange', type: 'float'},
{name: 'lastChange', type: 'date', dateFormat: 'n/j h:ia'},
{name: 'rating', type: 'int'}
]
});
// manually load local data
store.loadData(myData);
// Create the rating column so we can use it as a plugin as well as a column
// Defaults to a 5 column star set.
var ratingColumn = new Ext.ux.RatingColumn({
header: 'Rating',
dataIndex: 'rating',
width: 110
});
// create the Grid
var grid = new Ext.grid.GridPanel({
store: store,
plugins: ratingColumn,
columns: [
{id:'company',header: 'Company', width: 140, sortable: true, dataIndex: 'company'},
{header: 'Price', width: 75, sortable: true, renderer: 'usMoney', dataIndex: 'price'},
{header: 'Change', width: 75, sortable: true, renderer: change, dataIndex: 'change'},
{header: '% Change', width: 75, sortable: true, renderer: pctChange, dataIndex: 'pctChange'},
{header: 'Last Updated', width: 85, sortable: true, renderer: Ext.util.Format.dateRenderer('m/d/Y'), dataIndex: 'lastChange'},
ratingColumn
],
stripeRows: true,
autoExpandColumn: 'company',
height: 350,
width: 600,
title: 'Rating Grid'
});
// render the grid to the specified div in the page
grid.render('grid-example');
});
</script>
</head>
<body>
<h1>Rating Column Example</h1>
<div id="grid-example"></div>
</body>
</html>
Screenshot:
http://tdg-i.com/img/screencasts/2010-07-21_1445.png
It renders an editable star rating from an INTEGER field in the Record.
The optional (defaults to 5) size config specifies the total number of stars.
This image
http://i131.photobucket.com/albums/p286/TimeTrialAnimal/rating.gif
needs to be in examples/shared/icons
Then drop this into examples/<anywhere>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Rating Column Example</title>
<link rel="stylesheet" type="text/css" href="../../resources/css/ext-all.css" />
<link rel="stylesheet" type="text/css" href="../shared/examples.css" />
<link rel="stylesheet" type="text/css" href="grid-examples.css" />
<style type=text/css>
.x-action-col-cell img.ux-rating-icon {
height: 16px;
width: 16px;
background-image: url(../shared/icons/rating.gif);
}
.x-action-col-cell img.ux-rating-icon-on {
background-position: 0 -16px;
}
.x-action-col-cell img.ux-rating-icon-hover {
background-position: 0 -32px;
}
</style>
<script type="text/javascript" src="../../adapter/ext/ext-base-debug.js"></script>
<script type="text/javascript" src="../../ext-all-debug.js"></script>
<script type="text/javascript">
Ext.ux.RatingColumn = Ext.extend(Ext.grid.ActionColumn, {
size: 5, // Number of icons in the column
inconIndexRe: /ux-rating-icon-(\d+)/,
constructor: function(config) {
var items = config.items = [],
i = 1,
l = ((config.size || this.size) + 1);
for (; i < l; i++) {
items.push({});
}
Ext.ux.RatingColumn.superclass.constructor.call(this, config);
this.renderer = this.renderer.createInterceptor(this.setItemClasses);
},
// When we are initialized as a plugin, hook into the grid's render evet
init: function(grid) {
grid.on({
render: this.onHostGridRender,
single: true
});
},
// Route mousemove and mouseout events of the Grid's body through the View's event processing so we get control with value added (rowIdx)
onHostGridRender: function(g) {
g.getView().mainBody.on({
mousemove: function(e) {
g.view.processEvent('mousemove', e);
},
mouseout: function(e) {
g.view.processEvent('mouseout', e);
}
})
},
setItemClasses: function(v) {
for (var i = 0, it = this.items, l = it.length; i < l; i++) {
it[i].iconCls = 'ux-rating-icon ux-rating-icon-' + (i + 1);
if (i < v) {
it[i].iconCls += ' ux-rating-icon-on';
}
}
},
processEvent: function(evtName, e, grid, rowIndex, colIndex) {
var t = Ext.get(e.getTarget()),
match, iconIdx, icons, i, rec;
switch (evtName) {
case 'mouseout':
if (t.dom.tagName != 'img') {
this.activeCell && this.activeCell.select('img').removeClass('ux-rating-icon-hover');
delete this.activeCell;
delete this.ignoreMouseMove;
}
break;
case 'mousemove':
if (match = t.dom.className.match(this.inconIndexRe)) {
this.activeCell = t.up('');
if ((iconIdx = parseInt(match[1], 10)) != this.ignoreMouseMove) {
delete this.ignoreMouseMove;
icons = this.activeCell.query('img');
for (i = 0; i < icons.length; i++) {
Ext.fly(icons[i])[(i < iconIdx) ? 'addClass' : 'removeClass']('ux-rating-icon-hover');
}
}
}
break;
case 'click':
if (match = t.dom.className.match(this.inconIndexRe)) {
rec = grid.store.getAt(rowIndex);
if ((iconIdx = parseInt(match[1], 10)) == rec.get(this.dataIndex)) {
rec.set(this.dataIndex, 0);
} else {
rec.set(this.dataIndex, iconIdx);
}
this.ignoreMouseMove = iconIdx;
}
default:
} // End switch
// Return any event handler return statuses to honour event cancelling
return Ext.ux.RatingColumn.superclass.processEvent.apply(this, arguments);
}
});
Ext.onReady(function(){
Ext.QuickTips.init();
// NOTE: This is an example showing simple state management. During development,
// it is generally best to disable state management as dynamically-generated ids
// can change across page loads, leading to unpredictable results. The developer
// should ensure that stable state ids are set for stateful components in real apps.
Ext.state.Manager.setProvider(new Ext.state.CookieProvider());
// sample static data for the store
var myData = [
['3m Co',71.72,0.02,0.03,'9/1 12:00am', 1],
['Alcoa Inc',29.01,0.42,1.47,'9/1 12:00am', 2],
['Altria Group Inc',83.81,0.28,0.34,'9/1 12:00am', 3],
['American Express Company',52.55,0.01,0.02,'9/1 12:00am', 4],
['American International Group, Inc.',64.13,0.31,0.49,'9/1 12:00am', 5],
['AT&T Inc.',31.61,-0.48,-1.54,'9/1 12:00am', 1],
['Boeing Co.',75.43,0.53,0.71,'9/1 12:00am', 2],
['Caterpillar Inc.',67.27,0.92,1.39,'9/1 12:00am', 3],
['Citigroup, Inc.',49.37,0.02,0.04,'9/1 12:00am', 4],
['E.I. du Pont de Nemours and Company',40.48,0.51,1.28,'9/1 12:00am', 5],
['Exxon Mobil Corp',68.1,-0.43,-0.64,'9/1 12:00am', 1],
['General Electric Company',34.14,-0.08,-0.23,'9/1 12:00am', 2],
['General Motors Corporation',30.27,1.09,3.74,'9/1 12:00am', 3],
['Hewlett-Packard Co.',36.53,-0.03,-0.08,'9/1 12:00am', 4],
['Honeywell Intl Inc',38.77,0.05,0.13,'9/1 12:00am', 5],
['Intel Corporation',19.88,0.31,1.58,'9/1 12:00am', 1],
['International Business Machines',81.41,0.44,0.54,'9/1 12:00am', 2],
['Johnson & Johnson',64.72,0.06,0.09,'9/1 12:00am', 3],
['JP Morgan & Chase & Co',45.73,0.07,0.15,'9/1 12:00am', 4],
['McDonald\'s Corporation',36.76,0.86,2.40,'9/1 12:00am', 5],
['Merck & Co., Inc.',40.96,0.41,1.01,'9/1 12:00am', 1],
['Microsoft Corporation',25.84,0.14,0.54,'9/1 12:00am', 2],
['Pfizer Inc',27.96,0.4,1.45,'9/1 12:00am', 3],
['The Coca-Cola Company',45.07,0.26,0.58,'9/1 12:00am', 4],
['The Home Depot, Inc.',34.64,0.35,1.02,'9/1 12:00am', 5],
['The Procter & Gamble Company',61.91,0.01,0.02,'9/1 12:00am', 1],
['United Technologies Corporation',63.26,0.55,0.88,'9/1 12:00am', 2],
['Verizon Communications',35.57,0.39,1.11,'9/1 12:00am', 3],
['Wal-Mart Stores, Inc.',45.45,0.73,1.63,'9/1 12:00am', 4]
];
/**
* Custom function used for column renderer
* @param {Object} val
*/
function change(val){
if(val > 0){
return '<span style="color:green;">' + val + '</span>';
}else if(val < 0){
return '<span style="color:red;">' + val + '</span>';
}
return val;
}
/**
* Custom function used for column renderer
* @param {Object} val
*/
function pctChange(val){
if(val > 0){
return '<span style="color:green;">' + val + '%</span>';
}else if(val < 0){
return '<span style="color:red;">' + val + '%</span>';
}
return val;
}
// create the data store
var store = new Ext.data.ArrayStore({
fields: [
{name: 'company'},
{name: 'price', type: 'float'},
{name: 'change', type: 'float'},
{name: 'pctChange', type: 'float'},
{name: 'lastChange', type: 'date', dateFormat: 'n/j h:ia'},
{name: 'rating', type: 'int'}
]
});
// manually load local data
store.loadData(myData);
// Create the rating column so we can use it as a plugin as well as a column
// Defaults to a 5 column star set.
var ratingColumn = new Ext.ux.RatingColumn({
header: 'Rating',
dataIndex: 'rating',
width: 110
});
// create the Grid
var grid = new Ext.grid.GridPanel({
store: store,
plugins: ratingColumn,
columns: [
{id:'company',header: 'Company', width: 140, sortable: true, dataIndex: 'company'},
{header: 'Price', width: 75, sortable: true, renderer: 'usMoney', dataIndex: 'price'},
{header: 'Change', width: 75, sortable: true, renderer: change, dataIndex: 'change'},
{header: '% Change', width: 75, sortable: true, renderer: pctChange, dataIndex: 'pctChange'},
{header: 'Last Updated', width: 85, sortable: true, renderer: Ext.util.Format.dateRenderer('m/d/Y'), dataIndex: 'lastChange'},
ratingColumn
],
stripeRows: true,
autoExpandColumn: 'company',
height: 350,
width: 600,
title: 'Rating Grid'
});
// render the grid to the specified div in the page
grid.render('grid-example');
});
</script>
</head>
<body>
<h1>Rating Column Example</h1>
<div id="grid-example"></div>
</body>
</html>
Screenshot:
http://tdg-i.com/img/screencasts/2010-07-21_1445.png