1. #1
    Sencha User
    Join Date
    Mar 2007
    Location
    Boulder, CO
    Posts
    69
    Vote Rating
    0
    ericwaldheim is on a distinguished road

      0  

    Default grid dataIndex and sorting

    grid dataIndex and sorting


    When I click to sort a column in which the dataIndex exceeds the number of columns in the grid (less one), I get the following error:

    Error: this.config[col] has no properties
    Source File: http://localhost:8080/tmg/yui-ext/basic-grid-lib.js Line: 112

    I'm using the yui-ext.32.2 release.

    (Amazing work. thanks.)

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

      0  

    Default


    Can you explain a little more, maybe a code sample? It's sounds like you are saying that when you sort on a column that has a dataIndex greater than the number of available indexes in the data model you get an error? (which is what you should get).

  3. #3
    Sencha User
    Join Date
    Mar 2007
    Location
    Boulder, CO
    Posts
    69
    Vote Rating
    0
    ericwaldheim is on a distinguished road

      0  

    Default


    The Grid has one column, the DataModel has 2 columns.
    With dataIndex (which is an excellent feature) I can display
    just the second column. But sorting it fails.

    Code:
    	var g = YAHOO.ext.grid;
    	var s = g.DefaultColumnModel.sortTypes;
    	var cols = [
    		{header:"Name", width:150, dataIndex:1,
    		 sortable:true, sortType:s.asStr}, 
    	];
    	grid = new g.Grid('lbox',
    				   new g.DefaultDataModel([[3,3],[1,1],[2,2]]),
    				   new g.DefaultColumnModel(cols)
    				  )
           grid.render();
    [img]

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

      0  

    Default


    Try it without the extra comma.

    Code:
    var cols = [
          {header:"Name", width:150, dataIndex:1,
           sortable:true, sortType:s.asStr},  <--- extra comma
       ];

  5. #5
    Sencha User
    Join Date
    Mar 2007
    Location
    Boulder, CO
    Posts
    69
    Vote Rating
    0
    ericwaldheim is on a distinguished road

      0  

    Default


    Nope, same issue. Here's what I think is happening.

    Line 758 of GridView.js calls sort with a dataIndex
    Code:
            dm.sort(cm, cm.getDataIndex(header.columnIndex), direction);
    But the data model is expecting a columnIndex:

    Code:
    YAHOO.ext.grid.DefaultDataModel.prototype.sort = function(columnModel, columnIndex, direction, suppressEvent){
    So in my example the dataIndex is 1 but the largest columnIndex is 0.

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

      0  

    Default


    That code is right. The data model is expecting a column index matching it's structure (which is what a dataIndex is). But it led me to this:

    sort() is calling out to the ColumnModel, which is where the sortTypes are defined. It is passing it's column index (the dataIndex), which doesn't match the column models column index.

    That is indeed a bug.

    Nice find, I will fix it now.

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

      0  

    Default


    Can you paste this somewhere in your code and tell me if it fixes it?
    Code:
    YAHOO.ext.grid.DefaultColumnModel.prototype.getSortType = function(col){
        col = this.getDataIndex(col);
        if(!this.config[col].sortType){
            return YAHOO.ext.grid.DefaultColumnModel.sortTypes.none;
        }
        return this.config[col].sortType;
    };
    The sortTypes being on the column model is going to be eliminated soon. They should be defined on the data model for this very reason. The only reason it hasn't been done is to not break peoples code.

  8. #8
    Sencha User
    Join Date
    Mar 2007
    Location
    Boulder, CO
    Posts
    69
    Vote Rating
    0
    ericwaldheim is on a distinguished road

      0  

    Default


    Nope, that doesn't seem to be it.

    I traced starting in YAHOO.ext.grid.HeaderController.headerClick.
    Within this func we have:
    • header.columnIndex is 0
      cm.getDataIndex(header.columnIndex) is 1
    headerClick then makes this call:
    Code:
    dm.sort(cm, cm.getDataIndex(header.columnIndex), direction);
    so we end up here with columnIndex = 1:
    Code:
    YAHOO.ext.grid.DefaultDataModel.prototype.sort = function(columnModel, columnIndex, direction, suppressEvent){
        ...
        var sortType = columnModel.getSortType(columnIndex);
    getSortType then calls getDataIndex with col=1:
    Code:
    YAHOO.ext.grid.DefaultColumnModel.prototype.getDataIndex = function(col){
        if(typeof this.config[col].dataIndex != 'number'){
            return col;
        }
        return this.config[col].dataIndex;
    };
    with col=1. Since there is no col w/ index 1 in our ColumnModel we get the exception:
    Error: this.config[col] has no properties
    Source File: DefaultColumnModel.js Line: 175
    So I'm still of the opinion that we go wrong when we pass a dataIndex of 1 and to a function that wants a columnIndex (max should be 0). But it's late, perhaps I've missed something. Thanks.[/b]

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

      0  

    Default


    So I'm still of the opinion that we go wrong when we pass a dataIndex of 1 and to a function that wants a columnIndex (max should be 0). But it's late, perhaps I've missed something.

    The data model doesn't care about the column model's column index, it only cares about it's column indexes.

    Had I thought it out a little further, I would have realize getDataIndex would fail in the same way. This will fix the problem, although I am not happy with the solution. Maybe it's time to break everyone code (and mine) and take the sortType off the ColumnModel. It is not compatible with the dataIndex feature and I predict it will cause even more problems in the future.

    The data index lookup is lazy initialized so it is only built if the user actually does a sort.
    Code:
    YAHOO.ext.grid.DefaultColumnModel.prototype.getSortType = function(col){
        if(!this.dataMap){
            // build a lookup so we don't search every time
            var map = [];
            for(var i = 0, len = this.config.length; i < len; i++){
                map[this.getDataIndex(i)] = i;
            }
            this.dataMap = map;
        }
        col = this.dataMap[col];
        if(!this.config[col].sortType){
            return YAHOO.ext.grid.DefaultColumnModel.sortTypes.none;
        }
        return this.config[col].sortType;
    };

  10. #10
    Sencha - Ext JS Dev Team Animal's Avatar
    Join Date
    Mar 2007
    Location
    Notts/Redwood City
    Posts
    30,498
    Vote Rating
    46
    Animal has a spectacular aura about Animal has a spectacular aura about

      0  

    Default


    Make the change before there's too much code to break. The sort type is definitely part of the data model. It's not a huge amount of refactoring for Grid users. I'll accomodate it no problem.

Similar Threads

  1. Sorting not working in Paging Grid
    By saasira in forum Ext 1.x: Help & Discussion
    Replies: 8
    Last Post: 28 Oct 2010, 7:33 AM
  2. Sorting for several column in the grid?
    By franklt69 in forum Ext 1.x: Help & Discussion
    Replies: 1
    Last Post: 15 Jun 2009, 7:00 PM
  3. Paging grid sorting?
    By edmundfitz in forum Ext 2.x: Help & Discussion
    Replies: 0
    Last Post: 8 Mar 2007, 7:07 AM
  4. [1.0a2 Rev 5] Grid sorting
    By Arikon in forum Ext 2.x: Help & Discussion
    Replies: 2
    Last Post: 28 Feb 2007, 9:38 AM
  5. Sorting the Grid
    By Animal in forum Ext 1.x: Help & Discussion
    Replies: 13
    Last Post: 4 Oct 2006, 3:46 AM

Thread Participants: 2