Skip to content
This repository has been archived by the owner on Feb 12, 2022. It is now read-only.

Add search on keypress, clear input when value unchanged #1598

Merged
merged 3 commits into from
Nov 5, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2019,7 +2019,7 @@ <h2 class="header">Year</h2>
<section id="search">
<h2>Search</h2>
<div class="thin-box">
<div class="search input-group" data-initialize="search" role="search" id="mySearch">
<div class="search input-group" data-initialize="search" data-searchOnKeyPress="true" role="search" id="mySearch">
<input type="search" class="form-control" placeholder="Search" />
<span class="input-group-btn">
<button class="btn btn-default" type="button">
Expand Down
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -610,10 +610,6 @@ define(function (require) {
});
}

// if(options.search){
// resp.items = [];
// }

// call and simulate latency
setTimeout(function () {
callback(resp);
Expand All @@ -622,6 +618,7 @@ define(function (require) {

// initialize repater
$('#myRepeater').repeater({
searchOnKeyPress: true,
dataSource: function (options, callback) {
if (options.view === 'list') {
list(options, callback);
Expand Down Expand Up @@ -857,6 +854,9 @@ define(function (require) {
$('#mySearch').on('searched.fu.search', function (event, text) {
log('Searched: ' + text);
});
$('#mySearch').on('cleared.fu.search', function (event, text) {
log('cleared search');
});


/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Expand Down
7 changes: 5 additions & 2 deletions js/repeater.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@
this.$filters.selectlist();
this.$pageSize.selectlist();
this.$primaryPaging.find('.combobox').combobox();
this.$search.search();
this.$search.search({
searchOnKeyPress: this.options.searchOnKeyPress
});

this.$filters.on('changed.fu.selectlist', function (e, value) {
self.$element.trigger('filtered.fu.repeater', value);
Expand Down Expand Up @@ -793,7 +795,8 @@
defaultView: -1, //should be a string value. -1 means it will grab the active view from the view controls
dropPagingCap: 10,
staticHeight: -1, //normally true or false. -1 means it will look for data-staticheight on the element
views: null //can be set to an object to configure multiple views of the same type
views: null, //can be set to an object to configure multiple views of the same type,
searchOnKeyPress: false
};

$.fn.repeater.viewTypes = {};
Expand Down
65 changes: 31 additions & 34 deletions js/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,16 @@
this.$element = $(element);
this.options = $.extend({}, $.fn.search.defaults, options);

if (this.$element.attr('data-searchOnKeyPress') === 'true'){
this.options.searchOnKeyPress = true;
}

this.$button = this.$element.find('button');
this.$input = this.$element.find('input');
this.$icon = this.$element.find('.glyphicon');

this.$button.on('click.fu.search', $.proxy(this.buttonclicked, this));
this.$input.on('keydown.fu.search', $.proxy(this.keypress, this));
this.$input.on('keyup.fu.search', $.proxy(this.keypressed, this));
this.$input.on('keyup.fu.search', $.proxy(this.keypress, this));

this.activeSearch = '';
};
Expand Down Expand Up @@ -87,52 +90,45 @@

action: function () {
var val = this.$input.val();
var inputEmptyOrUnchanged = (val === '' || val === this.activeSearch);

if (this.activeSearch && inputEmptyOrUnchanged) {
this.clear();
} else if (val) {
if (val && val.length > 0) {
this.search(val);
}
else {
this.clear();
}
},

buttonclicked: function (e) {
e.preventDefault();
if ($(e.currentTarget).is('.disabled, :disabled')) return;
this.action();
},

keypress: function (e) {
if (e.which === 13) {
e.preventDefault();
if(this.$element.hasClass('searched')) {
this.clear();
}
else {
this.action();
}
},

keypressed: function (e) {
var remove = 'glyphicon-remove';
var search = 'glyphicon-search';
var val;
keypress: function (e) {
var ENTER_KEY_CODE = 13;
var TAB_KEY_CODE = 9;
var ESC_KEY_CODE = 27;

if (e.which === 13) {
if (e.which === ENTER_KEY_CODE) {
e.preventDefault();
this.action();
} else if (e.which === 9) {
}
else if(e.which === TAB_KEY_CODE) {
e.preventDefault();
} else {
val = this.$input.val();

if (val !== this.activeSearch || !val) {
this.$icon.removeClass(remove).addClass(search);
if (val) {
this.$element.removeClass('searched');
} else if (this.options.clearOnEmpty) {
this.clear();
}

} else {
this.$icon.removeClass(search).addClass(remove);
}

}
else if(e.which === ESC_KEY_CODE) {
e.preventDefault();
this.clear();
}
else if(this.options.searchOnKeyPress) {
// search on other keypress
this.action();
}
},

Expand Down Expand Up @@ -174,7 +170,8 @@
};

$.fn.search.defaults = {
clearOnEmpty: false
clearOnEmpty: false,
searchOnKeyPress: false
};

$.fn.search.Constructor = Search;
Expand Down
21 changes: 0 additions & 21 deletions test/search-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,27 +70,6 @@ define(function(require){
equal(clearedEventFired, true, 'cleared event was fired');
});

test("should process sequential searches", function () {
var $search = $(html);
var searchText = '';

$search.search().on('searched.fu.search', function (e, text) { searchText = text; });

$search.find('input').val('search text');
$search.find('button').click();

equal($search.find('.glyphicon').attr('class'), 'glyphicon glyphicon-remove', 'search icon has changed');
equal(searchText, 'search text', 'search text was provided in event');

$search.find('input').val('search text 2').keyup();
equal($search.find('.glyphicon').attr('class'), 'glyphicon glyphicon-search', 'search icon has returned');

$search.find('button').click();

equal($search.find('.glyphicon').attr('class'), 'glyphicon glyphicon-remove', 'search icon has changed');
equal(searchText, 'search text 2', 'search text was provided in event');
});

test("should correctly respond to disable and enable methods", function () {
var $search = $(html);

Expand Down