1. #1
    Sencha User
    Join Date
    Mar 2007
    Posts
    7,854
    Vote Rating
    4
    tryanDLS is on a distinguished road

      0  

    Default Sort order backwards??

    Sort order backwards??


    Jack,

    Is it me, or is the sort on DefaultDataModel going in the opposite direction of what's specified?

  2. #2
    Sencha User jack.slocum's Avatar
    Join Date
    Mar 2007
    Location
    Tampa, FL
    Posts
    6,955
    Vote Rating
    16
    jack.slocum will become famous soon enough

      0  

    Default


    It looks like it is. I recently upgraded the sort code (nice upgrade :?) and I must have broken it. I swapped the return values to re-reverse it:

    Code:
    YAHOO.ext.grid.DefaultDataModel.prototype.sort = function(sortInfo, columnIndex, direction, suppressEvent){
        // store these so we can maintain sorting when we load new data
        this.sortInfo = sortInfo;
        this.sortColumn = columnIndex;
        this.sortDir = direction;
        
        var dsc = (direction && direction.toUpperCase() == 'DESC');
        var sortType = null;
        if(typeof sortInfo == 'function'){
            sortType = sortInfo;
        }else if(typeof sortInfo == 'object'){
            sortType = sortInfo.getSortType(columnIndex);;
        }
        var fn = function(cells, cells2){
            var v1 = sortType ? sortType(cells[columnIndex], cells) : cells[columnIndex];
            var v2 = sortType ? sortType(cells2[columnIndex], cells2) : cells2[columnIndex];
            if(v1 < v2)
    			return dsc ? 1 : -1;
    		if(v1 > v2)
    			return dsc ? -1 : 1;
    	    return 0;
        };
        this.data.sort(fn);
        if(!suppressEvent){
           this.fireRowsSorted(columnIndex, direction);
        }
    };

  3. #3
    Sencha User
    Join Date
    Mar 2007
    Posts
    7,854
    Vote Rating
    4
    tryanDLS is on a distinguished road

      0  

    Default


    At least that's an easy one

    Another ?. The comments for sort() say pass a null as the 1st arg to use the default. When I did

    Code:
            dm.setDefaultSort(cm, 1, 'ASC');
            dm.loadData(data);
            dm.sort(null, 1, 'ASC');
    I got a sortInfo has no properties err. If I pass 'cm' as 1st arg it works OK.

    One more ?. Is there a way to get the rowIndex to display as a column when it's not in the data (using JSONDataModel)?

  4. #4
    Sencha User jack.slocum's Avatar
    Join Date
    Mar 2007
    Location
    Tampa, FL
    Posts
    6,955
    Vote Rating
    16
    jack.slocum will become famous soon enough

      0  

    Default


    Do me a favor and try passing undefined as the first argument. That I am guessing will work.

  5. #5
    Sencha User jack.slocum's Avatar
    Join Date
    Mar 2007
    Location
    Tampa, FL
    Posts
    6,955
    Vote Rating
    16
    jack.slocum will become famous soon enough

      0  

    Default


    Anyway, try this:

    Code:
    YAHOO.ext.grid.DefaultDataModel.prototype.sort = function(sortInfo, columnIndex, direction, suppressEvent){
        // store these so we can maintain sorting when we load new data
        this.sortInfo = sortInfo;
        this.sortColumn = columnIndex;
        this.sortDir = direction;
        
        var dsc = (direction && direction.toUpperCase() == 'DESC');
        var sortType = null;
        if(sortInfo != null){
            if(typeof sortInfo == 'function'){
                sortType = sortInfo;
            }else if(typeof sortInfo == 'object'){
                sortType = sortInfo.getSortType(columnIndex);;
            }
        }
        var fn = function(cells, cells2){
            var v1 = sortType ? sortType(cells[columnIndex], cells) : cells[columnIndex];
            var v2 = sortType ? sortType(cells2[columnIndex], cells2) : cells2[columnIndex];
            if(v1 < v2)
    			return dsc ? +1 : -1;
    		if(v1 > v2)
    			return dsc ? -1 : +1;
    	    return 0;
        };
        this.data.sort(fn);
        if(!suppressEvent){
           this.fireRowsSorted(columnIndex, direction);
        }
    };

  6. #6
    Sencha User jack.slocum's Avatar
    Join Date
    Mar 2007
    Location
    Tampa, FL
    Posts
    6,955
    Vote Rating
    16
    jack.slocum will become famous soon enough

      0  

    Default


    As for displaying the rowIndex in the first column, what type of data model are you using?

  7. #7
    Sencha User
    Join Date
    Mar 2007
    Posts
    7,854
    Vote Rating
    4
    tryanDLS is on a distinguished road

      0  

    Default


    Passing 0 or '' to dm.sort() does work.

    For the rowIndex issue, I'm using JSON. In fact, here's a chunk of code, if you want to experiment
    Code:
    	init : function(){
    
    		var data = {'States':[
    			{'__type':'State', 'Name':'Alaska', 'Code':'AK','IsContentViewable':true},
    			{'__type':'State', 'Name':'Arkansas', 'Code':'AR','IsContentViewable':true},
    			{'__type':'State', 'Name':'California', 'Code':'CA','IsContentViewable':true},
    			{'__type':'State', 'Name':'Colorado', 'Code':'CO','IsContentViewable':true},
    			{'__type':'State', 'Name':'Connecticut', 'Code':'CT','IsContentViewable':true},
    			{'__type':'State', 'Name':'Delaware', 'Code':'DE','IsContentViewable':true}
    			]};
    
    		var dm, cm, sm;
    
    		cm = new YAHOO.ext.grid.DefaultColumnModel(
    			[{header:'', hidden:true},
    			 {header:'Name', width: 150},
    			 {header:'Code', width: 50},
    			 {header:'Content Viewable', width: 125}
    	  		]);
    
    		cm.defaultSortable = true;
    
            var schema = {
                root:'States',
                id:'use-index',
                fields: ['__type', 'Name', 'Code', 'IsContentViewable']
            };
    
    		dm = new YAHOO.ext.grid.JSONDataModel(schema);
    
            dm.onLoad.subscribe(this.onLoad.createDelegate(this));
            dm.onLoadException.subscribe(this.showError.createDelegate(this));
            dm.setDefaultSort(cm, 1, 'ASC');
            dm.loadData(data);
            dm.sort(0, 2, 'DESC');
    	
            sm = new YAHOO.ext.grid.SingleSelectionModel();
    
            this.grid = new YAHOO.ext.grid.Grid('exGrid', dm, cm, sm);
            this.grid.render();

  8. #8
    Sencha User jack.slocum's Avatar
    Join Date
    Mar 2007
    Location
    Tampa, FL
    Posts
    6,955
    Vote Rating
    16
    jack.slocum will become famous soon enough

      0  

    Default


    The problem with the rowIndex is if you manually insert or add rows it won't be updated automatically. If all you are doing is loading data the normal way, then you could do something like this:

    1. Add a bogus column as the first field in your data model. Make sure you give it a name that won't be found such as "row-index".

    2. Add a preprocessor to column 0 that looks something like this:

    var rowIndex = 0;
    dm.addPreprocessor(0, function(){ return ++rowIndex; });
    dm.on('beforeload', function(){ rowIndex = 0; });

    Obviously you could format these to be more aesthetically pleasing.

  9. #9
    Sencha User
    Join Date
    Mar 2007
    Posts
    7,854
    Vote Rating
    4
    tryanDLS is on a distinguished road

      0  

    Default


    That does exactly what I was looking for - no inserts needed. Thanks.

Similar Threads

  1. Order Entry Example
    By griffiti93 in forum Community Discussion
    Replies: 18
    Last Post: 14 May 2009, 8:43 PM
  2. Treepanel : record order
    By wehtam in forum Ext 1.x: Help & Discussion
    Replies: 0
    Last Post: 10 Jan 2007, 5:33 AM
  3. YAHOO.ext.DomHelper backwards?!
    By jay@moduscreate.com in forum Ext 1.x: Help & Discussion
    Replies: 5
    Last Post: 1 Dec 2006, 1:49 PM
  4. BasicDialog z-order issues
    By brian.moeskau in forum Ext 1.x: Help & Discussion
    Replies: 6
    Last Post: 22 Nov 2006, 10:31 AM
  5. Sort direction and sort images
    By dfenwick in forum Ext 1.x: Bugs
    Replies: 3
    Last Post: 20 Sep 2006, 11:53 PM

Thread Participants: 1