PDA

View Full Version : Sorting column field according to another field



cosw69
14 Aug 2012, 7:01 AM
Hi, i have a 'File' model which has fields 'displayFileSize' and 'fileSize' like this:
Ext.define('File'), {
extend ..
fields : [
{ name : fileSize, type: int},

{ name : displayFileSize, type: string,

mapping:function()...
},
.
.
.
here fileSize is size of the file in bytes, i am mapping displayFilesize to a proper form to show the file size. Assume fileSize =1024 then displayFileSize=1 Kb. up to this point everything is fine,

However sorting displayFileSize is not working correctly as i have integers and MB,KB,GB followed by integers. I need to sort displayFileSize according to fileSize field, so how can i sort one field according to another field?

I found sortType property and some documentation about custom sort methods like this:
http://docs.sencha.com/ext-js/4-1/#!/api/Ext.data.SortTypes
(http://docs.sencha.com/ext-js/4-1/#!/api/Ext.data.SortTypes)
but i couldn't find a way to solve my problem, any suggestions?

Tim Toady
14 Aug 2012, 7:36 AM
I assume you are displaying these items in some sort of grid. The way I would handle this situation is to not have a field that specifies KB, MB, etc. I would only have a field of type integer and use a renderer on the column to convert it to KB, MB, etc. That way it would naturally sort correctly, yet the user sees it in a readable way.

http://docs.sencha.com/ext-js/4-1/#!/api/Ext.grid.column.Column-cfg-renderer

cosw69
14 Aug 2012, 11:52 PM
hi again,
Tim Toady, thank you for your kind answer. I found a function : getSortParam which sets the sorting parameter (just what i want). I needed to override this function and give 'fileSize' as sorting parameter like this:

getSortParam:function(){
return 'fileSize';
}

However i did this in view. Can i override it in my controller or somewhere other than view?

Tim Toady
15 Aug 2012, 9:15 AM
It is possible, but in this particular situation I think it is better to have it in the view. The controller is better for handling events than overriding a method in a view. It comes out a bit hacky IMO.

cosw69
15 Aug 2012, 11:36 AM
It is possible, but in this particular situation I think it is better to have it in the view. The controller is better for handling events than overriding a method in a view. It comes out a bit hacky IMO.

You are right but in my situation i m changing view often using Ext Designer (by copying the code from it to my project) and that will cause my override function to be lost which means i would have to write this method everytime i change the view. I found a way for this that i explained in another topic i created today:

http://www.sencha.com/forum/showthread.php?237297-Sorting-parameter