Skip to content

Commit

Permalink
Updated search method to optionally reveal results. #65
Browse files Browse the repository at this point in the history
- New option `{revealResults: true|false}`, true by default
- Updated revealNode to accept an array of nodes.
- Updated searchable tree example
  • Loading branch information
jonmiles committed Apr 14, 2015
1 parent ef99728 commit ef86a8f
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 29 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -494,8 +494,9 @@ Returns an array of matching nodes.

```javascript
$('#tree').treeview('search', [ 'Parent', {
ignoreCase: true,
exactMatch: false
ignoreCase: true, // case insensitive
exactMatch: false, // like or equals
revealResults: false, // reveal matching nodes
}]);
```

Expand Down
2 changes: 1 addition & 1 deletion dist/bootstrap-treeview.min.js

Large diffs are not rendered by default.

11 changes: 9 additions & 2 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ <h2>Input</h2>
Exact Match
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" class="checkbox" id="chk-reveal-results" value="false">
Reveal Results
</label>
</div>
<button type="button" class="btn btn-success" id="btn-search">Search</button>
<button type="button" class="btn btn-default" id="btn-clear-search">Clear</button>
<!-- </form> -->
Expand Down Expand Up @@ -421,7 +427,7 @@ <h2></h2>
enableLinks: true,
data: defaultData
});



var $searchableTree = $('#treeview-searchable').treeview({
Expand All @@ -432,7 +438,8 @@ <h2></h2>
var pattern = $('#input-search').val();
var options = {
ignoreCase: $('#chk-ignore-case').is(':checked'),
exactMatch: $('#chk-exact-match').is(':checked')
exactMatch: $('#chk-exact-match').is(':checked'),
revealResults: $('#chk-reveal-results').is(':checked')
};
var results = $searchableTree.treeview('search', [ pattern, options ]);

Expand Down
37 changes: 29 additions & 8 deletions public/js/bootstrap-treeview.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@

_default.searchOptions = {
ignoreCase: true,
exactMatch: false
exactMatch: false,
revealResults: true
};

var Tree = function (element, options) {
Expand Down Expand Up @@ -739,17 +740,26 @@

/**
Reveals a given tree node, expanding the tree from node to root.
@param {Object|Number} identifier - A valid node or node id
@param {Object|Number|Array} identifier - A valid node, node id or array of node identifiers
@param {optional Object} options
*/
Tree.prototype.revealNode = function (identifier, options) {
options = $.extend({}, _default.options, options);

var parentNode = this.getParent(identifier);
while (parentNode) {
this.setExpandedState(parentNode, true, options);
parentNode = this.getParent(parentNode);
};
if (!(identifier instanceof Array)) {
identifier = [identifier];
}

// Iterate nodes
$.each(identifier, $.proxy(function (index, identifier) {

// Traverse the tree expanding parents from node to root
var parentNode = this.getParent(identifier);
while (parentNode) {
this.setExpandedState(parentNode, true, options);
parentNode = this.getParent(parentNode);
};
}, this));

this.render();
};
Expand Down Expand Up @@ -795,11 +805,22 @@
}

results = this.findNodes(pattern, modifier);

// Add searchResult property to all matching nodes
// This will be used to apply custom styles
// and when identifying result to be cleared
$.each(results, function (index, node) {
node.searchResult = true;
})

this.render();
// If revealResults, then render is triggered from revealNode
// otherwise we just call render.
if (options.revealResults) {
this.revealNode(results);
}
else {
this.render();
}
}

this.$element.trigger('searchComplete', $.extend(true, {}, results));
Expand Down
37 changes: 29 additions & 8 deletions src/js/bootstrap-treeview.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@

_default.searchOptions = {
ignoreCase: true,
exactMatch: false
exactMatch: false,
revealResults: true
};

var Tree = function (element, options) {
Expand Down Expand Up @@ -739,17 +740,26 @@

/**
Reveals a given tree node, expanding the tree from node to root.
@param {Object|Number} identifier - A valid node or node id
@param {Object|Number|Array} identifier - A valid node, node id or array of node identifiers
@param {optional Object} options
*/
Tree.prototype.revealNode = function (identifier, options) {
options = $.extend({}, _default.options, options);

var parentNode = this.getParent(identifier);
while (parentNode) {
this.setExpandedState(parentNode, true, options);
parentNode = this.getParent(parentNode);
};
if (!(identifier instanceof Array)) {
identifier = [identifier];
}

// Iterate nodes
$.each(identifier, $.proxy(function (index, identifier) {

// Traverse the tree expanding parents from node to root
var parentNode = this.getParent(identifier);
while (parentNode) {
this.setExpandedState(parentNode, true, options);
parentNode = this.getParent(parentNode);
};
}, this));

this.render();
};
Expand Down Expand Up @@ -795,11 +805,22 @@
}

results = this.findNodes(pattern, modifier);

// Add searchResult property to all matching nodes
// This will be used to apply custom styles
// and when identifying result to be cleared
$.each(results, function (index, node) {
node.searchResult = true;
})

this.render();
// If revealResults, then render is triggered from revealNode
// otherwise we just call render.
if (options.revealResults) {
this.revealNode(results);
}
else {
this.render();
}
}

this.$element.trigger('searchComplete', $.extend(true, {}, results));
Expand Down
37 changes: 29 additions & 8 deletions tests/lib/bootstrap-treeview.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@

_default.searchOptions = {
ignoreCase: true,
exactMatch: false
exactMatch: false,
revealResults: true
};

var Tree = function (element, options) {
Expand Down Expand Up @@ -739,17 +740,26 @@

/**
Reveals a given tree node, expanding the tree from node to root.
@param {Object|Number} identifier - A valid node or node id
@param {Object|Number|Array} identifier - A valid node, node id or array of node identifiers
@param {optional Object} options
*/
Tree.prototype.revealNode = function (identifier, options) {
options = $.extend({}, _default.options, options);

var parentNode = this.getParent(identifier);
while (parentNode) {
this.setExpandedState(parentNode, true, options);
parentNode = this.getParent(parentNode);
};
if (!(identifier instanceof Array)) {
identifier = [identifier];
}

// Iterate nodes
$.each(identifier, $.proxy(function (index, identifier) {

// Traverse the tree expanding parents from node to root
var parentNode = this.getParent(identifier);
while (parentNode) {
this.setExpandedState(parentNode, true, options);
parentNode = this.getParent(parentNode);
};
}, this));

this.render();
};
Expand Down Expand Up @@ -795,11 +805,22 @@
}

results = this.findNodes(pattern, modifier);

// Add searchResult property to all matching nodes
// This will be used to apply custom styles
// and when identifying result to be cleared
$.each(results, function (index, node) {
node.searchResult = true;
})

this.render();
// If revealResults, then render is triggered from revealNode
// otherwise we just call render.
if (options.revealResults) {
this.revealNode(results);
}
else {
this.render();
}
}

this.$element.trigger('searchComplete', $.extend(true, {}, results));
Expand Down

0 comments on commit ef86a8f

Please sign in to comment.