Skip to content

Commit

Permalink
Events searchComplete and searchCleared added #46
Browse files Browse the repository at this point in the history
  • Loading branch information
jonmiles committed Mar 19, 2015
1 parent 3d67625 commit 8c57086
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 21 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,8 @@ Clear the tree view of any previous search results e.g. remove their highlighted
$('#tree').treeview('clearSearch');
```

Triggers `searchCleared` event

### getNode(nodeId)

Returns a single node object that matches the given node id.
Expand Down Expand Up @@ -330,6 +332,8 @@ $('#tree').treeview('search', [ 'Parent', {
}]);
```

Triggers `searchComplete` event

## Events

You can bind to any event defined below by either using an options callback handler, or the standard jQuery .on method.
Expand Down Expand Up @@ -364,6 +368,10 @@ $('#tree').on('nodeSelected', function(event, data) {
`nodeUnselected (event, node)` - A node is unselected.
`searchComplete (event, results)` - After a search completes
`searchCleared (event, results)` - After search results are cleared
## Copyright and Licensing
Expand Down
2 changes: 1 addition & 1 deletion dist/bootstrap-treeview.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 30 additions & 6 deletions public/js/bootstrap-treeview.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@
onNodeCollapsed: undefined,
onNodeExpanded: undefined,
onNodeSelected: undefined,
onNodeUnselected: undefined
onNodeUnselected: undefined,
onSearchComplete: undefined,
onSearchCleared: undefined
};

var Tree = function (element, options) {
Expand Down Expand Up @@ -136,6 +138,14 @@
if (typeof (this.options.onNodeUnselected) === 'function') {
this.$element.off('nodeUnselected');
}

if (typeof (this.options.onSearhComplete) === 'function') {
this.$element.off('searchComplete');
}

if (typeof (this.options.onSearchCleared) === 'function') {
this.$element.off('searchCleared');
}
};

Tree.prototype.subscribeEvents = function () {
Expand All @@ -159,6 +169,14 @@
if (typeof (this.options.onNodeUnselected) === 'function') {
this.$element.on('nodeUnselected', this.options.onNodeUnselected);
}

if (typeof (this.options.onSearchComplete) === 'function') {
this.$element.on('searchComplete', this.options.onSearchComplete);
}

if (typeof (this.options.onSearchCleared) === 'function') {
this.$element.on('searchCleared', this.options.onSearchCleared);
}
};

/*
Expand Down Expand Up @@ -508,7 +526,7 @@

this.clearSearch();

var nodes = [];
var results = [];
if (pattern && pattern.length > 0) {

if (options.exactMatch) {
Expand All @@ -520,25 +538,31 @@
modifier += 'i';
}

nodes = this.findNodes(pattern, modifier);
$.each(nodes, function (index, node) {
results = this.findNodes(pattern, modifier);
$.each(results, function (index, node) {
node.searchResult = true;
})

this.render();
}

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

return results;
};

/**
Clears previous search results
*/
Tree.prototype.clearSearch = function () {
$.each(this.findNodes('true', 'g', 'searchResult'), function (index, node) {

var results = $.each(this.findNodes('true', 'g', 'searchResult'), function (index, node) {
node.searchResult = false;
});

this.render();

this.$element.trigger('searchCleared', $.extend(true, {}, results));
};

/**
Expand Down
Loading

0 comments on commit 8c57086

Please sign in to comment.