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 Original file line Diff line number Diff line change
Expand Up @@ -2019,7 +2019,7 @@ <h2 class="header">Year</h2>
<section id="search"> <section id="search">
<h2>Search</h2> <h2>Search</h2>
<div class="thin-box"> <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" /> <input type="search" class="form-control" placeholder="Search" />
<span class="input-group-btn"> <span class="input-group-btn">
<button class="btn btn-default" type="button"> <button class="btn btn-default" type="button">
Expand Down
8 changes: 4 additions & 4 deletions index.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -610,10 +610,6 @@ define(function (require) {
}); });
} }


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

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


// initialize repater // initialize repater
$('#myRepeater').repeater({ $('#myRepeater').repeater({
searchOnKeyPress: true,
dataSource: function (options, callback) { dataSource: function (options, callback) {
if (options.view === 'list') { if (options.view === 'list') {
list(options, callback); list(options, callback);
Expand Down Expand Up @@ -857,6 +854,9 @@ define(function (require) {
$('#mySearch').on('searched.fu.search', function (event, text) { $('#mySearch').on('searched.fu.search', function (event, text) {
log('Searched: ' + 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 Original file line Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@
this.$filters.selectlist(); this.$filters.selectlist();
this.$pageSize.selectlist(); this.$pageSize.selectlist();
this.$primaryPaging.find('.combobox').combobox(); this.$primaryPaging.find('.combobox').combobox();
this.$search.search(); this.$search.search({
searchOnKeyPress: this.options.searchOnKeyPress
});


this.$filters.on('changed.fu.selectlist', function (e, value) { this.$filters.on('changed.fu.selectlist', function (e, value) {
self.$element.trigger('filtered.fu.repeater', 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 defaultView: -1, //should be a string value. -1 means it will grab the active view from the view controls
dropPagingCap: 10, dropPagingCap: 10,
staticHeight: -1, //normally true or false. -1 means it will look for data-staticheight on the element 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 = {}; $.fn.repeater.viewTypes = {};
Expand Down
65 changes: 31 additions & 34 deletions js/search.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -35,13 +35,16 @@
this.$element = $(element); this.$element = $(element);
this.options = $.extend({}, $.fn.search.defaults, options); 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.$button = this.$element.find('button');
this.$input = this.$element.find('input'); this.$input = this.$element.find('input');
this.$icon = this.$element.find('.glyphicon'); this.$icon = this.$element.find('.glyphicon');


this.$button.on('click.fu.search', $.proxy(this.buttonclicked, this)); 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.keypress, this));
this.$input.on('keyup.fu.search', $.proxy(this.keypressed, this));


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


action: function () { action: function () {
var val = this.$input.val(); var val = this.$input.val();
var inputEmptyOrUnchanged = (val === '' || val === this.activeSearch); if (val && val.length > 0) {

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


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


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


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


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

e.preventDefault();
if (val !== this.activeSearch || !val) { this.clear();
this.$icon.removeClass(remove).addClass(search); }
if (val) { else if(this.options.searchOnKeyPress) {
this.$element.removeClass('searched'); // search on other keypress
} else if (this.options.clearOnEmpty) { this.action();
this.clear();
}

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

} }
}, },


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


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


$.fn.search.Constructor = Search; $.fn.search.Constructor = Search;
Expand Down
21 changes: 0 additions & 21 deletions test/search-test.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -70,27 +70,6 @@ define(function(require){
equal(clearedEventFired, true, 'cleared event was fired'); 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 () { test("should correctly respond to disable and enable methods", function () {
var $search = $(html); var $search = $(html);


Expand Down