Skip to content

Commit

Permalink
tree: include originalQuery in getChildren options for filtering
Browse files Browse the repository at this point in the history
This gives getChildren access to the original query via
options.originalQuery, permitting it to re-apply the root query
against child levels.
  • Loading branch information
Kenneth G. Franqueiro committed Sep 28, 2013
1 parent 6d5e772 commit e345b81
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 37 deletions.
45 changes: 22 additions & 23 deletions test/data/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ define([
"dojo/_base/Deferred",
"dojo/store/Memory",
"dojo/store/Observable",
"dojo/store/util/QueryResults"
"dojo/store/util/QueryResults",
"./DeferredWrapper"
],
function(lang, arrayUtil, Deferred, Memory, Observable, QueryResults){
function(lang, arrayUtil, Deferred, Memory, Observable, QueryResults, DeferredWrapper){
// some sample data
// global var "data"
data = {
Expand Down Expand Up @@ -208,37 +209,35 @@ function(lang, arrayUtil, Deferred, Memory, Observable, QueryResults){
{ id: 'BuenosAires', name:'Buenos Aires', type:'city', parent: 'AR' }
];

// global var testCountryStore
testCountryStore = Observable(new Memory({
data: testCountryData,
getChildren: function(parent, options){
return this.query({parent: parent.id}, options);
},
mayHaveChildren: function(parent){
return parent.type != "city";
},
query: function(query, options){
var def = new Deferred();
var immediateResults = this.queryEngine(query, options)(this.data);
setTimeout(function(){
def.resolve(immediateResults);
}, 200);
var results = QueryResults(def.promise);
return results;
}
}));

// global var testSyncCountryStore
testSyncCountryStore = Observable(new Memory({
data: testCountryData,
getChildren: function(parent, options){
return this.query({parent: parent.id}, options);
// Support persisting the original query via options.originalQuery
// so that child levels will filter the same way as the root level
return this.query(
lang.mixin({}, options && options.originalQuery || null,
{ parent: parent.id }),
options);
},
mayHaveChildren: function(parent){
return parent.type != "city";
},
query: function (query, options){
query = query || {};
options = options || {};

if (!query.parent && !options.deep) {
// Default to a single-level query for root items (no parent)
query.parent = undefined;
}
return this.queryEngine(query, options)(this.data);
}
}));

// global var testCountryStore
testCountryStore = new DeferredWrapper(testSyncCountryStore);

var testTopHeavyData = arrayUtil.map(testStateStore.data, function (state) {
return {
abbreviation: state.abbreviation,
Expand Down
33 changes: 21 additions & 12 deletions test/tree.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@

var treeGrid = window.treeGrid = new StandardGrid({
store: testCountryStore,
query: {type: "continent"},
columns: [
tree({label: "Name", field:"name", sortable: false}),
editor({label: "Visited", field: "bool", sortable: false}, "checkbox"),
Expand All @@ -59,6 +58,13 @@
]
}, "treeGrid");

on(byId("filter"), "click", function(){
treeGrid.set("query", { name: /a$/ });
});
on(byId("clearFilter"), "click", function(){
treeGrid.set("query", {});
});

on(byId("save"), "click", function(){
treeGrid.save();
});
Expand Down Expand Up @@ -112,7 +118,6 @@

window.treeSelector = new StandardGrid({
store: testCountryStore,
query: {type: "continent"},
selectionMode: "none",
allowSelectAll: true,
columns: getTreeSelectorColumns()
Expand All @@ -135,7 +140,6 @@

window.treeSubRows = new StandardGrid({
store: testCountryStore,
query: {type: "continent"},
subRows: [[
tree({label:"Name", field:"name", sortable: false}),
{label:"Type", field:"type", sortable: false}
Expand All @@ -147,7 +151,6 @@

window.treeColumnSets = new (declare([Grid, Keyboard, Selection, ColumnSet]))({
store: testCountryStore,
query: {type: "continent"},
columnSets: [[
[ tree({label:"Name", field:"name", sortable: false}) ],
[ {label:"Type", field:"type", sortable: false} ]
Expand All @@ -166,14 +169,20 @@
<body class="claro">
<h2>Lazy-loading tree grid, with store functionality</h2>
<div id="treeGrid"></div>
<button id="save">Save</button>
<button id="revert">Revert</button>
<button id="add-continent">Add Continent</button>
<button id="add-country">Add Greece To Europe</button>
<button id="remove-country">Remove Greece</button>
<button id="delete">Delete Selected</button>
<button id="edit-continent">Edit Europe</button>
<button id="move-rome-na">Move Rome to North America</button>
<div>
<button id="filter">Show only entries ending in "a"</button>
<button id="clearFilter">Show all</button>
</div>
<div>
<button id="save">Save</button>
<button id="revert">Revert</button>
<button id="add-continent">Add Continent</button>
<button id="add-country">Add Greece To Europe</button>
<button id="remove-country">Remove Greece</button>
<button id="delete">Delete Selected</button>
<button id="edit-continent">Edit Europe</button>
<button id="move-rome-na">Move Rome to North America</button>
</div>

<h2>Tree grid with formatter on tree column, and a checkbox selector to test select-all on children</h2>
<div id="treeSelector"></div>
Expand Down
6 changes: 4 additions & 2 deletions tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,9 @@ function tree(column){
container,
containerStyle,
scrollHeight,
options;
options = {
originalQuery: this.query
};

if(!preloadNode){
// if the children have not been created, create a container, a preload node and do the
Expand All @@ -209,7 +211,7 @@ function tree(column){
// 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 };
options.parentId = row.id;
}
// Include level information on query for renderQuery case
if("level" in target){
Expand Down

0 comments on commit e345b81

Please sign in to comment.