PDA

View Full Version : Filesize Column



janssen
4 Apr 2011, 3:53 AM
I've got a GridPanel with 2 columns. One column contains the filename the other one contains filesize.
The filesize column contains a variety of filesizes - bytes, kB, MB, GB, TB - the column sorting needs to sort by size. ( 1 GB > 1 MB )
At first i tried to solve it with the creation of a new ext.data.type ( and use it when I build the store ).. only to find out that "convert" is fired before "sortType" so when I convert the bytes ( Json from back-end ) to for example MB it will not see that 1 MB > 1 Byte. - sorting would not work

By using the "renderer" in the column I made the sorting work perfectly 1 MB > 1 Byte
(the store is loaded with filename and filesize in bytes)



buildGrid: function(){
return new Ext.grid.GridPanel({
border: false,
store: this.buildStore(),
columns: [{
id: 'name',
header: 'Name',
dataIndex: 'name',
sortable: true,
width: 300
},{
id: 'filesize',
header: 'Filesize',
dataIndex: 'filesize',
sortable: true,
width: 150,
scope: this,
renderer: this.filesizeRenderer
}]
});
}, //eo function buildGrid
buildStore: function(){
return new Ext.data.JsonStore({
autoDestroy: true,
fields: ['name', {name: 'filesize', type: 'int'}]
});
}, //eo function buildStore
filesizeRenderer: function(v){
if(v > 0){
var s = ['bytes', 'kB', 'MB', 'GB', 'TB', 'PB'];
var e = Math.floor(Math.log(v)/Math.log(1024));
return (v/Math.pow(1024, Math.floor(e))).toFixed(2)+" "+s[e];
}
else{
return '0 ' + 'bytes';
}
} //eo function filesizeRenderer

If anyone has a better idea ( especially if its reusable ) i would like to know!