Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    #16
    I have organized this better into a complete self-contained class with static data bound to a datasource.

    Here is the grid/datasource code. Attached is the json data file. When you run it you can see the ordering sent to the console & the normalizer is returning things in the correct orders, but the sorting does not reflect what the normalizer is returning.

    Code:
    isc.PositionGrid = isc.defineClass("PositionGrid", isc.TreeGrid);
    	
    isc.DataSource.create({
    	ID:"positionAccountDS",
    	dataFormat:"json",
    	dataURL:"./positionData.js",
    	fields:[
    		{ hidden:"true", name:"key", primaryKey:true, type:"text" },
    		{ hidden:"true", name:"isOpen", type:"boolean" },
    		{ hidden:"true", name:"containedIn", type:"text", foreignKey:"positionAccountDS.key" },
    		{ title:"Name", name:"sec_name", type:"text", required:"true" },
    		{ title:"Cur %", name:"amt_percent", type:"float" }
    	]
    });
    		
    var gridFields = [
    	{ name:"sec_name", width: 300, treeField:true },
    	{ name:"amt_percent", width: 100 }
    ];
    		
    for (var i = 0; i < gridFields.length; i++) {
    	if (isc.isA.Object(gridFields[i]) && (gridFields[i] != null)) {
    		gridFields[i].sortNormalizer = function(record, fieldName, grid) {
    			return grid.sortNormalizer(record, fieldName);
    		};
    	}
    }
    			
    isc.PositionGrid.addProperties({
    	width: 500,
    	height: 300,
    	canEdit: false,
    	canSort: true,
    	loadDataOnDemand: false,
    	groupStartOpen:"all",
    	position: "relative",
    	alternateRecordStyles: true,
    	showAllRecords: true,
    	autoFetchData: false,
    	useSortIndexesInNormalizer: true,
    	sortIndexes: []
    });
    		
    isc.PositionGrid.addMethods({
    	sortNormalizer: function(record, fieldName) {	
    		console.log("sortNormalizer(" + record['name'] + ", " + fieldName + ") = " + this.sortIndexes[record.key]);
    		return this.useSortIndexesInNormalizer ? this.sortIndexes[record.key] : record[fieldName];
    	},
    			
    	setSort: function(sortSpecifiers) {
    		this.recomputeSortIndexes(sortSpecifiers);		
    		
    		return this.Super("setSort", arguments);
    	},
    	
    	recomputeSortIndexes: function(sortSpecifiers) {
    		if ((this.data) && (this.data != null) && isc.isA.Tree(this.data)) {			
    			// 1st re-initialize the sort indexes
    			this.sortIndexes = [];
    			this.sortIndexesByName = [];
    				
    			// Now process the tree
    			var currentIndexObj = { currentIndex: 0 };
    			this.useSortIndexesInNormalizer = false;
    			this.computeSortIndexes(this.data.getRoot(), currentIndexObj, sortSpecifiers);
    			this.useSortIndexesInNormalizer = true;
    			this.sortIndexes.setLength(currentIndexObj.currentIndex);
    					
    			console.log(this.sortIndexesByName.valueOf());
    		}
    	},
    	
    	computeSortIndexes: function(node, currentIndexObj, sortSpecifiers) {
    		if (node && (node != null)) {			
    			if (this.data.getLevel(node) <= 2) {
    				// 	This is a level above what we want sorted, so just add it as-is (if its not the tree's root),
    				// then go onto any children, if any
    				
    				if (!this.data.isRoot(node)) {
    					// This isn't the root of the tree. The actual root node of the tree we don't show. It's just a dummy row
    					this.sortIndexes[node.key] = currentIndexObj.currentIndex++;
    					this.sortIndexesByName.push(node.name);
    				}
    					
    				if (!this.data.isLeaf(node)) {
    					// This node is not a leaf, so get the children & call ourselves for each child
    					var children = this.data.getChildren(node);
    						
    					if ((children != null) && isc.isA.Array(children)) {
    						for (var i = 0; i < children.length; i++) {		
    							this.computeSortIndexes(children[i], currentIndexObj, sortSpecifiers);
    						}
    					}
    				}
    			}
    			else if ((typeof this.sortIndexes[node.key] == "undefined") || (this.sortIndexes[node.key] == null)) {
    				// We're at a level where we want to sort & the node has not already been added to the sortIndexes
    				// So get our parent and then perform the sort on the paren't direct children
    				// Then map the results to the sort indexes
    				var parent = this.data.getParent(node);
    					
    				if (parent != null) {
    					var parentsChildren = this.data.getChildren(parent);
    						
    					if ((parentsChildren != null) && isc.isA.Array(parentsChildren)) {
    						var arrayToSort = [];
    						arrayToSort.addList(parentsChildren); // Don't want to modify the actual array
    						arrayToSort.setSort(sortSpecifiers);
    							
    						// Now that its sorted, we'll walk it and add them to the sort indexes
    						for (var i = 0; i < arrayToSort.length; i++) {
    							this.sortIndexes[arrayToSort[i].key] = currentIndexObj.currentIndex++;
    							this.sortIndexesByName.push(arrayToSort[i].name);
    								
    							if (!this.data.isLeaf(arrayToSort[i])) {
    								var children = this.data.getChildren(arrayToSort[i]);
    									
    								if ((children != null) && isc.isA.Array(children)) {
    									for (var j = 0; j < children.length; j++) {
    										this.computeSortIndexes(children[j], currentIndexObj, sortSpecifiers);
    									}
    								}
    							}
    						}					
    					}
    				}
    			}
    		}
    	}
    });
    	
    isc.PositionGrid.create({
    	ID: "positions",
    	dataSource: "positionAccountDS",
    	fields: gridFields	
    });
    		
    positions.fetchData(null, 'positions.data.openAll()');
    Attached Files

    Comment


      #17
      I actually solved the problem. It seems that in my sortNormalizer method, if the sort is descending then the value returned is sorted descending which doesn't produce the correct result. If this.sortDirection == false then I can return -this.sortIndexes[record.key] and the sorting seems to be fine.

      Thank you for your help with this.

      Comment


        #18
        Thanks for updating us, one less thing to look into :)

        Comment

        Working...
        X