Skip to content

Redirect on click added #4

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 1 commit into
base: master
Choose a base branch
from
Open
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
122 changes: 64 additions & 58 deletions dist/jquery.bootcomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,28 @@
* @license MIT License
*
*/
(function ( $ ) {

$.fn.bootcomplete = function(options) {

(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 : {}
url: "/search.php",
method: 'get',
wrapperClass: "bc-wrapper",
menuClass: "bc-menu",
idField: true,
idFieldName: $(this).attr('name') + "_id",
minLength: 3,
dataParams: {},
formParams: {},
// something like www.site.com/products/_id_to_replace_
// where _id_to_replace_ will be replaced with "id" of selected item
linkFormatToRedirectOnClick: null
}
var settings = $.extend( {}, defaults, options );
$(this).attr('autocomplete','off')
$(this).wrap('<div class="'+settings.wrapperClass+'"></div>')

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
Expand All @@ -34,8 +36,8 @@
$('<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)

Expand All @@ -48,64 +50,68 @@
}
$(that).next('.' + settings.menuClass).hide();
}
function searchQuery(){

function searchQuery() {

var arr = [];
$.each(settings.formParams,function(k,v){
arr[k]=$(v).val()
$.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()
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.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 ) {
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>'
$.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'))
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'));
}
else {
//use created id field
$(that).prev('input[name="' + settings.idFieldName + '"]').val($(this).data('id'));

function selectResult() {
if (settings.linkFormatToRedirectOnClick === null) {
$(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'));
}
else {
//use created id field
$(that).prev('input[name="' + settings.idFieldName + '"]').val($(this).data('id'));
}
}
$(that).next('.' + settings.menuClass).hide();
} else {
window.location.href = settings.linkFormatToRedirectOnClick.replace('_id_to_replace_', $(this).data('id'));
}
$(that).next('.' + settings.menuClass).hide();
return false;
}

return this;
};
}( jQuery ));

}(jQuery));