zitadeveloper
23 Mar 2008, 8:33 PM
Straight ahead, i have to say, that im a beginner in programming. I guess you will see this in my code. But i tried to implement a rating System,like Apple uses it in iTunes, in a grid.
And now i want to share my results with the Ext.Community.
Implemented in the Grouping Example it looks like this:
And here is the code:
The HTML Page, including the grid and alittle CSS
<html>
<head>
<title>Grid Rating Example</title>
<link rel="stylesheet" type="text/css" href="ext-2.0/resources/css/ext-all.css" />
<script type="text/javascript" src="ext-2.0/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="ext-2.0/ext-all.js"></script>
<script type="text/javascript" src="js/ext-rating.js"></script>
<script type="text/javascript" src="ext-rating-col.js"></script>
<script type="text/javascript">
<style>
.rating {
width:98%;
height:16px;
background: transparent no-repeat center 0;
cursor:pointer;
}
</style>
/*
* Ext JS Library 2.0.2
* Copyright(c) 2006-2008, Ext JS, LLC.
* licensing@extjs.com
*
* http://extjs.com/license
*/
Ext.onReady(function(){
Ext.QuickTips.init();
var xg = Ext.grid;
var rateColumn = new RateColumn();
// shared reader
var reader = new Ext.data.ArrayReader({}, [
{name: 'company'},
{name: 'price', type: 'float'},
{name: 'change', type: 'float'},
{name: 'pctChange', type: 'float'},
{name: 'lastChange', type: 'date', dateFormat: 'n/j h:ia'},
{name: 'industry'},
{name: 'rating'}
]);
var grid = new xg.GridPanel({
store: new Ext.data.GroupingStore({
reader: reader,
data: xg.dummyData,
sortInfo:{field: 'company', direction: "ASC"},
groupField:'industry'
}),
columns: [
{id:'company',header: "Company", width: 60, sortable: true, dataIndex: 'company'},
{header: "Price", width: 20, sortable: true, renderer: Ext.util.Format.usMoney, dataIndex: 'price'},
{header: "Change", width: 20, sortable: true, dataIndex: 'change', renderer: Ext.util.Format.usMoney},
{header: "Industry", width: 20, sortable: true, dataIndex: 'industry'},
{header: "Last Updated", width: 20, sortable: true, renderer: Ext.util.Format.dateRenderer('m/d/Y'), dataIndex: 'lastChange'},
rateColumn
],
view: new Ext.grid.GroupingView({
forceFit:true,
groupTextTpl: '{text} ({[values.rs.length]} {[values.rs.length > 1 ? "Items" : "Item"]})'
}),
frame:true,
width: 700,
height: 450,
collapsible: true,
animCollapse: false,
title: 'Grouping Example',
iconCls: 'icon-grid',
plugins: [rateColumn],
renderTo: document.body
});
});
// Array data for the grids
Ext.grid.dummyData = [
['3m Co',71.72,0.02,0.03,'8/1 12:00am', 'Manufacturing', 2],
['Alcoa Inc',29.01,0.42,1.47,'9/1 12:00am', 'Manufacturing', 4],
['Altria Group Inc',83.81,0.28,0.34,'10/1 12:00am', 'Manufacturing', 1],
['American Express Company',52.55,0.01,0.02,'9/1 10:00am', 'Finance',1],
['AT&T Inc.',31.61,-0.48,-1.54,'9/1 12:00am', 'Services',2],
['Boeing Co.',75.43,0.53,0.71,'9/1 12:00am', 'Manufacturing',1],
['Caterpillar Inc.',67.27,0.92,1.39,'9/1 12:00am', 'Services',4],
['Citigroup, Inc.',49.37,0.02,0.04,'9/1 12:00am', 'Finance',1],
['E.I. du Pont de Nemours and Company',40.48,0.51,1.28,'9/1 12:00am', 'Manufacturing',1],
['Exxon Mobil Corp',68.1,-0.43,-0.64,'9/1 12:00am', 'Manufacturing',1],
['General Electric Company',34.14,-0.08,-0.23,'9/1 12:00am', 'Manufacturing',2],
['General Motors Corporation',30.27,1.09,3.74,'9/1 12:00am', 'Automotive',4],
['Hewlett-Packard Co.',36.53,-0.03,-0.08,'9/1 12:00am', 'Computer',1],
['Honeywell Intl Inc',38.77,0.05,0.13,'9/1 12:00am', 'Manufacturing',1],
['Intel Corporation',19.88,0.31,1.58,'9/1 12:00am', 'Computer',2],
['International Business Machines',81.41,0.44,0.54,'9/1 12:00am', 'Computer',1],
['Johnson & Johnson',64.72,0.06,0.09,'9/1 12:00am', 'Medical',2],
['JP Morgan & Chase & Co',45.73,0.07,0.15,'9/1 12:00am', 'Finance',1],
['McDonald\'s Corporation',36.76,0.86,2.40,'9/1 12:00am', 'Food',1],
['Merck & Co., Inc.',40.96,0.41,1.01,'9/1 12:00am', 'Medical',1],
['Microsoft Corporation',25.84,0.14,0.54,'9/1 12:00am', 'Computer',1],
['The Coca-Cola Company',45.07,0.26,0.58,'9/1 12:00am', 'Food',3],
['The Home Depot, Inc.',34.64,0.35,1.02,'9/1 12:00am', 'Retail',1],
['The Procter & Gamble Company',61.91,0.01,0.02,'9/1 12:00am', 'Manufacturing',4],
['United Technologies Corporation',63.26,0.55,0.88,'9/1 12:00am', 'Computer',4],
['Verizon Communications',35.57,0.39,1.11,'9/1 12:00am', 'Services',2],
['Wal-Mart Stores, Inc.',45.45,0.73,1.63,'9/1 12:00am', 'Retail',1],
['Walt Disney Company (The) (Holding Company)',29.89,0.24,0.81,'9/1 12:00am', 'Services',4]
];
</script>
</head>
<body>
</body>
</html>The ext-rating.js
var rating = {
/////////////////////////////////////////////////////////////////////////////////////////////////
/*
Ext-Rating-System
The Ext-Rating-System is based on a method writing values to title and Style-classes.
1. The items are rendered by the custom Renderer. The value of each single Image is
included in it's own class. Just like: '.rate1','.rate2' etc.
The overall value of each item is stored in the title of the parent-DIV.
2. A save function is called, which adds a click-handler to each Image and saves the new
value to the parent-DIV
3.
*/
/////////////////////////////////////////////////////////////////////////////////////////////////
// @ Configurations @////
im1: 'images/crown.png',
im2: 'images/crown_fade.png',
no: 4,
// @ You can modify these lines, to fit your source and number of favorite icons @////
/////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////
// @ Renderer @////
renderer: function(value){
crowns = '';
for (var i = 1; i <= value; i++){
crowns += '<img class="rate'+i+'" src="'+rating.im1+'">';
}
for (var f = i; f <= rating.no; f++){
crowns += '<img class="rate'+f+'" src="'+rating.im2+'">';
}
return '<div class="rating" title="'+value+'">'+crowns+'</div>';
},
// @ @////
/////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////
// @ Save @////
save: function(){
var el = Ext.select('.rating img');
el.on('click', function(e, ele){
var o = Ext.get(ele);
var value = o.dom.className.substr(4,1);
o.parent().set({title: value});;
});
},
// @ @////
/////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////
// @ init @////
init: function(){
Ext.select('.rating').each(function(el){
rating.save();
});
}
// @ Let' go!!! @////
/////////////////////////////////////////////////////////////////////////////////////////////////
}and finaly the ext-rating-col.js
RateColumn = function(){
var grid, record;
function getRecord(t){
var index = grid.getView().findRowIndex(t);
return grid.store.getAt(index);
}
function onMouseDown(e, t){
var parentDiv = t.parentNode;
if(parentDiv.className = 'rating'){
var newRating = Ext.fly(t).dom.className.substr(4,1);
e.stopEvent();
var record = getRecord(t);
record.set('rating', newRating);
}
}
Ext.apply(this,
{
header: 'Rating',
width: 100,
sortable: true,
fixed: true,
dataIndex: 'rating',
id: 'fav-col',
renderer: rating.renderer,
init : function(xg){
grid = xg;
grid.on('render', function(){
var view = grid.getView();
view.mainBody.on('mousedown', onMouseDown);
});
}
});
};I hope someone find this useful.
I used it in a demo-air application and the data-update worked fine this way. I think you have to change it, working with others, then local databases.
http://s212738125.online.de/test/crown.png http://s212738125.online.de/test/crown_fade.png
And now i want to share my results with the Ext.Community.
Implemented in the Grouping Example it looks like this:
And here is the code:
The HTML Page, including the grid and alittle CSS
<html>
<head>
<title>Grid Rating Example</title>
<link rel="stylesheet" type="text/css" href="ext-2.0/resources/css/ext-all.css" />
<script type="text/javascript" src="ext-2.0/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="ext-2.0/ext-all.js"></script>
<script type="text/javascript" src="js/ext-rating.js"></script>
<script type="text/javascript" src="ext-rating-col.js"></script>
<script type="text/javascript">
<style>
.rating {
width:98%;
height:16px;
background: transparent no-repeat center 0;
cursor:pointer;
}
</style>
/*
* Ext JS Library 2.0.2
* Copyright(c) 2006-2008, Ext JS, LLC.
* licensing@extjs.com
*
* http://extjs.com/license
*/
Ext.onReady(function(){
Ext.QuickTips.init();
var xg = Ext.grid;
var rateColumn = new RateColumn();
// shared reader
var reader = new Ext.data.ArrayReader({}, [
{name: 'company'},
{name: 'price', type: 'float'},
{name: 'change', type: 'float'},
{name: 'pctChange', type: 'float'},
{name: 'lastChange', type: 'date', dateFormat: 'n/j h:ia'},
{name: 'industry'},
{name: 'rating'}
]);
var grid = new xg.GridPanel({
store: new Ext.data.GroupingStore({
reader: reader,
data: xg.dummyData,
sortInfo:{field: 'company', direction: "ASC"},
groupField:'industry'
}),
columns: [
{id:'company',header: "Company", width: 60, sortable: true, dataIndex: 'company'},
{header: "Price", width: 20, sortable: true, renderer: Ext.util.Format.usMoney, dataIndex: 'price'},
{header: "Change", width: 20, sortable: true, dataIndex: 'change', renderer: Ext.util.Format.usMoney},
{header: "Industry", width: 20, sortable: true, dataIndex: 'industry'},
{header: "Last Updated", width: 20, sortable: true, renderer: Ext.util.Format.dateRenderer('m/d/Y'), dataIndex: 'lastChange'},
rateColumn
],
view: new Ext.grid.GroupingView({
forceFit:true,
groupTextTpl: '{text} ({[values.rs.length]} {[values.rs.length > 1 ? "Items" : "Item"]})'
}),
frame:true,
width: 700,
height: 450,
collapsible: true,
animCollapse: false,
title: 'Grouping Example',
iconCls: 'icon-grid',
plugins: [rateColumn],
renderTo: document.body
});
});
// Array data for the grids
Ext.grid.dummyData = [
['3m Co',71.72,0.02,0.03,'8/1 12:00am', 'Manufacturing', 2],
['Alcoa Inc',29.01,0.42,1.47,'9/1 12:00am', 'Manufacturing', 4],
['Altria Group Inc',83.81,0.28,0.34,'10/1 12:00am', 'Manufacturing', 1],
['American Express Company',52.55,0.01,0.02,'9/1 10:00am', 'Finance',1],
['AT&T Inc.',31.61,-0.48,-1.54,'9/1 12:00am', 'Services',2],
['Boeing Co.',75.43,0.53,0.71,'9/1 12:00am', 'Manufacturing',1],
['Caterpillar Inc.',67.27,0.92,1.39,'9/1 12:00am', 'Services',4],
['Citigroup, Inc.',49.37,0.02,0.04,'9/1 12:00am', 'Finance',1],
['E.I. du Pont de Nemours and Company',40.48,0.51,1.28,'9/1 12:00am', 'Manufacturing',1],
['Exxon Mobil Corp',68.1,-0.43,-0.64,'9/1 12:00am', 'Manufacturing',1],
['General Electric Company',34.14,-0.08,-0.23,'9/1 12:00am', 'Manufacturing',2],
['General Motors Corporation',30.27,1.09,3.74,'9/1 12:00am', 'Automotive',4],
['Hewlett-Packard Co.',36.53,-0.03,-0.08,'9/1 12:00am', 'Computer',1],
['Honeywell Intl Inc',38.77,0.05,0.13,'9/1 12:00am', 'Manufacturing',1],
['Intel Corporation',19.88,0.31,1.58,'9/1 12:00am', 'Computer',2],
['International Business Machines',81.41,0.44,0.54,'9/1 12:00am', 'Computer',1],
['Johnson & Johnson',64.72,0.06,0.09,'9/1 12:00am', 'Medical',2],
['JP Morgan & Chase & Co',45.73,0.07,0.15,'9/1 12:00am', 'Finance',1],
['McDonald\'s Corporation',36.76,0.86,2.40,'9/1 12:00am', 'Food',1],
['Merck & Co., Inc.',40.96,0.41,1.01,'9/1 12:00am', 'Medical',1],
['Microsoft Corporation',25.84,0.14,0.54,'9/1 12:00am', 'Computer',1],
['The Coca-Cola Company',45.07,0.26,0.58,'9/1 12:00am', 'Food',3],
['The Home Depot, Inc.',34.64,0.35,1.02,'9/1 12:00am', 'Retail',1],
['The Procter & Gamble Company',61.91,0.01,0.02,'9/1 12:00am', 'Manufacturing',4],
['United Technologies Corporation',63.26,0.55,0.88,'9/1 12:00am', 'Computer',4],
['Verizon Communications',35.57,0.39,1.11,'9/1 12:00am', 'Services',2],
['Wal-Mart Stores, Inc.',45.45,0.73,1.63,'9/1 12:00am', 'Retail',1],
['Walt Disney Company (The) (Holding Company)',29.89,0.24,0.81,'9/1 12:00am', 'Services',4]
];
</script>
</head>
<body>
</body>
</html>The ext-rating.js
var rating = {
/////////////////////////////////////////////////////////////////////////////////////////////////
/*
Ext-Rating-System
The Ext-Rating-System is based on a method writing values to title and Style-classes.
1. The items are rendered by the custom Renderer. The value of each single Image is
included in it's own class. Just like: '.rate1','.rate2' etc.
The overall value of each item is stored in the title of the parent-DIV.
2. A save function is called, which adds a click-handler to each Image and saves the new
value to the parent-DIV
3.
*/
/////////////////////////////////////////////////////////////////////////////////////////////////
// @ Configurations @////
im1: 'images/crown.png',
im2: 'images/crown_fade.png',
no: 4,
// @ You can modify these lines, to fit your source and number of favorite icons @////
/////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////
// @ Renderer @////
renderer: function(value){
crowns = '';
for (var i = 1; i <= value; i++){
crowns += '<img class="rate'+i+'" src="'+rating.im1+'">';
}
for (var f = i; f <= rating.no; f++){
crowns += '<img class="rate'+f+'" src="'+rating.im2+'">';
}
return '<div class="rating" title="'+value+'">'+crowns+'</div>';
},
// @ @////
/////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////
// @ Save @////
save: function(){
var el = Ext.select('.rating img');
el.on('click', function(e, ele){
var o = Ext.get(ele);
var value = o.dom.className.substr(4,1);
o.parent().set({title: value});;
});
},
// @ @////
/////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////
// @ init @////
init: function(){
Ext.select('.rating').each(function(el){
rating.save();
});
}
// @ Let' go!!! @////
/////////////////////////////////////////////////////////////////////////////////////////////////
}and finaly the ext-rating-col.js
RateColumn = function(){
var grid, record;
function getRecord(t){
var index = grid.getView().findRowIndex(t);
return grid.store.getAt(index);
}
function onMouseDown(e, t){
var parentDiv = t.parentNode;
if(parentDiv.className = 'rating'){
var newRating = Ext.fly(t).dom.className.substr(4,1);
e.stopEvent();
var record = getRecord(t);
record.set('rating', newRating);
}
}
Ext.apply(this,
{
header: 'Rating',
width: 100,
sortable: true,
fixed: true,
dataIndex: 'rating',
id: 'fav-col',
renderer: rating.renderer,
init : function(xg){
grid = xg;
grid.on('render', function(){
var view = grid.getView();
view.mainBody.on('mousedown', onMouseDown);
});
}
});
};I hope someone find this useful.
I used it in a demo-air application and the data-update worked fine this way. I think you have to change it, working with others, then local databases.
http://s212738125.online.de/test/crown.png http://s212738125.online.de/test/crown_fade.png