Skip to content

Added function #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/nbproject
150 changes: 65 additions & 85 deletions dist/jquery.bootcomplete.js
Original file line number Diff line number Diff line change
@@ -1,115 +1,95 @@
/*
* @name jQuery.bootcomplete
* @projectDescription Lightweight AJAX autocomplete for Bootstrap 3
* @author Rick Somers | http://getwebhelp.com/bootcomplete
* @version 1.0
* @license MIT License
*
*/
(function ( $ ) {

$.fn.bootcomplete = function(options) {

* @name jQuery.bootcomplete
* @projectDescription Lightweight AJAX autocomplete for Bootstrap 3
* @author Rick Somers | http://getwebhelp.com/bootcomplete
* @version 1.0
* @license MIT License
*
*/
(function ($) {

$.fn.bootcomplete = function (options) {
var defaults = {
url : "/search.php",
method : 'get',
wrapperClass : "bc-wrapper",
menuClass : "bc-menu",
idField : true,
idFieldName : $(this).attr('name')+"_id",
minLength : 3,
dataParams : {},
formParams : {}
}

var settings = $.extend( {}, defaults, options );

$(this).attr('autocomplete','off')
$(this).wrap('<div class="'+settings.wrapperClass+'"></div>')
url: "/search.php",
method: 'get',
wrapperClass: "bc-wrapper",
menuClass: "bc-menu",
idField: true,
idFieldName: $(this).attr('name') + "_id",
minLength: 3,
dataParams: {},
formParams: {}
};
var settings = $.extend({}, defaults, options);
$(this).attr('autocomplete', 'off');
$(this).wrap('<div class="' + settings.wrapperClass + '"></div>');
if (settings.idField) {
if ($(this).parent().parent().find('input[name="' + settings.idFieldName + '"]').length !== 0) {
//use existing id field
} else {
//there is no existing id field so create one
$('<input type="hidden" name="' + settings.idFieldName + '" value="">').insertBefore($(this))
}
$('<input type="hidden" name="' + settings.idFieldName + '" value="">').insertBefore($(this));
}
$('<div class="'+settings.menuClass+' list-group"></div>').insertAfter($(this))
$('<div class="' + settings.menuClass + ' list-group"></div>').insertAfter($(this));

$(this).on("keyup", searchQuery);
$(this).on("focusout", hideThat)




var xhr;
var that = $(this)
var that = $(this);

function hideThat() {
if ($('.list-group-item' + ':hover').length) {
return;
}
$(that).next('.' + settings.menuClass).hide();
}

function searchQuery(){

function searchQuery() {
var arr = [];
$.each(settings.formParams,function(k,v){
arr[k]=$(v).val()
})
var dyFormParams = $.extend({}, arr );
$.each(settings.formParams, function (k, v) {
arr[k] = $(v).val();
});
var dyFormParams = $.extend({}, arr);
var Data = $.extend({query: $(this).val()}, settings.dataParams, dyFormParams);
if(!Data.query){
$(this).next('.'+settings.menuClass).html('')
$(this).next('.'+settings.menuClass).hide()

if (!Data.query) {
$(this).next('.' + settings.menuClass).html('');
$(this).next('.' + settings.menuClass).hide();
}
if(Data.query.length >= settings.minLength){
if(xhr && xhr.readyState != 4){

if (Data.query.length >= settings.minLength) {

if (xhr && xhr.readyState !== 4) {
xhr.abort();
}

xhr = $.ajax({
type: settings.method,
url: settings.url,
data: Data,
dataType: "json",
success: function( json ) {
var results = ''
$.each( json, function(i, j) {
results += '<a href="#" class="list-group-item" data-id="'+j.id+'" data-label="'+j.label+'">'+j.label+'</a>'
success: function (json) {
var results = '';
$.each(json, function (i, j) {
results += '<a href="#" class="list-group-item" data-id="' + j.id + '" data-label="' + j.label + '">' + j.label + '</a>';
});

$(that).next('.'+settings.menuClass).html(results)
$(that).next('.'+settings.menuClass).children().on("click", selectResult)
$(that).next('.'+settings.menuClass).show()

$(that).next('.' + settings.menuClass).html(results);
$(that).next('.' + settings.menuClass).children().on("click", selectResult);
$(that).next('.' + settings.menuClass).show();
}
})
});
}
}
function selectResult(){
$(that).val($(this).data('label'))

function selectResult() {
$(that).val($(this).data('label'));
if (settings.idField) {
if ($(that).parent().parent().find('input[name="' + settings.idFieldName + '"]').length !== 0) {
//use existed id field
$(that).parent().parent().find('input[name="' + settings.idFieldName + '"]').val($(this).data('id'));
//ensure we trigger the onchange so we can do stuff
$(that).parent().parent().find('input[name="' + settings.idFieldName + '"]').trigger('change');
}
else {
//use created id field
$(that).prev('input[name="' + settings.idFieldName + '"]').val($(this).data('id'));
//ensure we trigger the onchange so we can do stuff
$(that).prev('input[name="' + settings.idFieldName + '"]').trigger('change');
}
$(that).prev('input[name="' + settings.idFieldName + '"]').val($(this).data('id'));
}
$(that).next('.' + settings.menuClass).hide();
return false;
}

$(this).keyup(function (e) {
if (e.keyCode === 27) {
$(that).next('.' + settings.menuClass).hide();
xhr.abort();
}
});

return this;
};
}( jQuery ));

}(jQuery));