Skip to content
Browse files

Adding support for updating odd-even striping of parent rows to alter…

…nate with children rows when tree branches are expanded or collapsed (not complete).
  • Loading branch information...
1 parent a30a30c commit 1f8275b55b45d3eb1479014093525753fb116186 @kriszyp committed Oct 3, 2012
Showing with 41 additions and 14 deletions.
  1. +24 −10 List.js
  2. +4 −1 test/JsonRest.html
  3. +13 −3 tree.js
View
34 List.js
@@ -397,10 +397,10 @@ function(arrayUtil, kernel, declare, listen, has, miscUtil, TouchScroll, hasClas
this.scrollTo({ x: 0, y: 0 });
},
- newRow: function(object, before, to, options){
+ newRow: function(object, before, to, options, absoluteTo){
if(before.parentNode){
var i = options.start + to;
- var row = this.insertRow(object, before.parentNode, before, i, options);
+ var row = this.insertRow(object, before.parentNode, before, i, options, options.start + absoluteTo);
put(row, ".ui-state-highlight");
setTimeout(function(){
put(row, "!ui-state-highlight");
@@ -411,17 +411,28 @@ function(arrayUtil, kernel, declare, listen, has, miscUtil, TouchScroll, hasClas
adjustRowIndices: function(firstRow){
if(this.maintainOddEven){
// this traverses through rows to maintain odd/even classes on the rows when indexes shift;
- var next = firstRow;
- var rowIndex = next.rowIndex;
+ var last, next = firstRow;
+ var rowIndex = rowIndex = next.rowIndex;
+ var trackRowIndex = true;
+ var absoluteRowIndex = next.absoluteRowIndex || rowIndex;
do{
+ if(last && last.nextSibling != next){
+ trackRowIndex = false; // quit tracking the row index if we are going into tree children or such
+ }
if(next.rowIndex > -1){
// skip non-numeric, non-rows
+ next.absoluteRowIndex = absoluteRowIndex;
if((next.className + ' ').indexOf("dgrid-row ") > -1){
- put(next, '.' + (rowIndex % 2 == 1 ? oddClass : evenClass) + '!' + (rowIndex % 2 == 0 ? oddClass : evenClass));
+ put(next, '.' + (absoluteRowIndex % 2 == 1 ? oddClass : evenClass) + '!' + (absoluteRowIndex % 2 == 0 ? oddClass : evenClass));
+ absoluteRowIndex++;
+ }
+ if(trackRowIndex){
+ next.rowIndex = rowIndex++;
}
- next.rowIndex = rowIndex++;
+ last = next;
}
- }while((next = next.nextSibling) && next.rowIndex != rowIndex);
+ }while((next = move({element: last}, 1, "dgrid-row", true)) && // if there is no next sibling, try to use move so we can traverse in and out of tree children, but reset the rowIndex when doing that
+ next != last && next.absoluteRowIndex != absoluteRowIndex); // make sure we really moved
}
},
renderArray: function(results, beforeNode, options){
@@ -480,6 +491,7 @@ function(arrayUtil, kernel, declare, listen, has, miscUtil, TouchScroll, hasClas
}, true)) - 1;
}
var rowsFragment = document.createDocumentFragment();
+ var startingIndex = options.startingIndex || 0;
// now render the results
if(results.map){
rows = results.map(mapEach, console.error);
@@ -494,7 +506,8 @@ function(arrayUtil, kernel, declare, listen, has, miscUtil, TouchScroll, hasClas
}
var lastRow;
function mapEach(object){
- lastRow = self.insertRow(object, rowsFragment, null, start++, options);
+ lastRow = self.insertRow(object, rowsFragment, null, start, options, start + startingIndex);
+ start++;
lastRow.observerIndex = observerIndex;
return lastRow;
}
@@ -515,7 +528,7 @@ function(arrayUtil, kernel, declare, listen, has, miscUtil, TouchScroll, hasClas
},
_autoId: 0,
- insertRow: function(object, parent, beforeNode, i, options){
+ insertRow: function(object, parent, beforeNode, i, options, absoluteIndex){
// summary:
// Creates a single row in the grid.
@@ -535,7 +548,7 @@ function(arrayUtil, kernel, declare, listen, has, miscUtil, TouchScroll, hasClas
this.removeRow(row);
}
row = this.renderRow(object, options);
- row.className = (row.className || "") + " ui-state-default dgrid-row " + (i % 2 == 1 ? oddClass : evenClass);
+ row.className = (row.className || "") + " ui-state-default dgrid-row " + (absoluteIndex % 2 == 1 ? oddClass : evenClass);
// get the row id for easy retrieval
this._rowIdToObject[row.id = id] = object;
}
@@ -545,6 +558,7 @@ function(arrayUtil, kernel, declare, listen, has, miscUtil, TouchScroll, hasClas
this.adjustRowIndices(previousRow);
}
row.rowIndex = i;
+ row.absoluteRowIndex = absoluteIndex;
return row;
},
renderRow: function(value, options){
View
5 test/JsonRest.html
@@ -18,13 +18,16 @@
.ui-widget{
margin: 10px;
}
+ .dgrid-row-odd .dgrid-cell {
+ background-color: #ddd;
+ }
</style>
<script type="text/javascript" src="../../dojo/dojo.js"
data-dojo-config="async: true"></script>
<script type="text/javascript">
require(["dgrid/List", "dgrid/OnDemandGrid","dgrid/Selection", "dgrid/editor", "dgrid/Keyboard", "dgrid/tree", "dojo/_base/declare", "dojo/store/JsonRest", "dojo/store/Observable", "dojo/store/Cache", "dojo/store/Memory", "dojo/domReady!"],
function(List, Grid, Selection, editor, Keyboard, tree, declare, JsonRest, Observable, Cache, Memory){
- var testStore = Observable(Cache(JsonRest({
+ testStore = Observable(Cache(JsonRest({
target:"./data/rest.php?",
idProperty: "id",
query: function(query, options){
View
16 tree.js
@@ -156,12 +156,13 @@ return function(column){
return grid.store.getChildren(row.data, options);
};
query.level = target.level;
- if(column.allowDuplicates){
+ options = column.allowDuplicates ?
// If allowDuplicates is specified, include parentId in options
// in order to facilitate unique IDs for each occurrence of the
// same item under multiple different parents.
- options = { parentId: row.id };
- }
+ { parentId: row.id } :
+ {};
+ options.startingIndex = rowElement.absoluteRowIndex + 1;
Deferred.when(
grid.renderQuery ?
grid._trackError(function(){
@@ -244,9 +245,18 @@ return function(column){
// Update _expanded map.
if(expanded){
this._expanded[row.id] = true;
+ // adjust the row indices of the children so we can maintain striping
+ var firstChild = this.down(row, 1).element;
+ if(firstChild){
+ firstChild.absoluteRowIndex = row.element.absoluteRowIndex + 1;
+ this.adjustRowIndices(firstChild);
+ }
}else{
delete this._expanded[row.id];
}
+ this.adjustRowIndices( // adjust the row indices that come after children
+ this.up({element: container.nextSibling}, 1, true).element // we do this to get the element before the element preceding the container
+ );
}
}; // end function grid.expand

0 comments on commit 1f8275b

Please sign in to comment.
Something went wrong with that request. Please try again.