jQuery datepicker not closing - jquery-ui

My jQuery datepicker only closes when picking a date, but I want it to close when the user clicks away from or on the close button. However, even with the showButtonPanel option set to true, the close button does not appear but the 'Today' button does.
I think it may have something to with having a custom onSelect action instead of the default but can't figure out how to close it myself. Tried using $.datepicker('hide') and ('destroy') but no difference.
$(document).ready(function() {
$.datepicker.setDefaults({firstDay: 1, dateFormat: 'dd/mm/yy', showAnim: 'fade'});
});
$(document).delegate('.editEndDate', 'click', function() {
$('.formattedEndDate').datepicker({
defaultDate: $('.formattedEndDate').attr('id'),
onSelect: function(dateText, inst) {
var date = dateText;
var data = 'project=' + projectId + '&date=' + date + '&dateToChange=end';
$.ajax({
type: 'POST',
url: 'helpers/change-project-date.php',
data: data + '&ajax=1',
success: function(response){
getNotification(response);
$('.formattedEndDate').fadeOut(function() {
$(this).load(location.href+ ' .formattedEndDate', function() {
$(this).fadeIn('slow');
});
});
},
error: function(response){
getNotification(response);
},
complete: function(response){
$('.formattedEndDate').datepicker('hide');
}
});
}
});
return false;
});
It may be something simple but I just can't see it. Thanks in advance.

I may have found a solution to my own problem...
$('.ui-datepicker').live('mouseleave', function() {
$('.ui-datepicker').fadeOut(function() {
$('.formattedStartDate').attr('class', 'formattedStartDate');
$(this).remove();
});
});
This works for me, hopefully it'll work for others too.

Everything blew up when I tried to use live. So I had to use on. I also added in input focus hide. So if you happen to focus on a different field the calendar isn't just hanging around. I also just did hide, but you should be able to switch it for fade if that is what you are wanting.
$('.bootstrap-datetimepicker-widget').on('mouseleave', function () {
$(this).hide();
})
$('input').on('focus', function () {
$('.bootstrap-datetimepicker-widget').hide();
});

Related

Bind dynatable search to onchange

Im using http://www.dynatable.com/ and there is a search input for filtering data. The problem is it works on hitting enter/losing focus etc and I want to use it on every letter change.
You can change dynatable to accomodate your needs. At line 1215 of jquery.dynatable.js(version 0.3.1), the search is performed on enter keypress. You may change this to keyup without waiting for enter key press.
Default Code:
$search
.bind(settings.inputs.queryEvent, function() {
obj.queries.runSearch($(this).val());
})
.bind('keyup', function(e) {
if (e.which == 13) {
obj.queries.runSearch($(this).val());
e.preventDefault();
}
});
Modified code:
$search
.bind(settings.inputs.queryEvent, function() {
obj.queries.runSearch($(this).val());
})
.bind('keyup', function(e) {
obj.queries.runSearch($(this).val());
e.preventDefault();
});
The following change to the jquery.dynatable.js code will do what you want, and will also update the results when the search input box is cleared with the "x" if you are using one. The reason the clear will also work is the event being used is "input" instead of "keyup" or "change", for reasons detailed in the documentation.
Unlike the input event, the change event is not necessarily fired for
each change to an element's value.
Original code
$search
.bind(settings.inputs.queryEvent, function() {
obj.queries.runSearch($(this).val());
})
.bind('kepress', function(e) {
if (e.which == 13) {
obj.queries.runSearch($(this).val());
e.preventDefault();
}
});
Modified code
$search
.bind(settings.inputs.queryEvent, function() {
obj.queries.runSearch($(this).val());
})
.bind('input', function(e) {
obj.queries.runSearch($(this).val());
e.preventDefault();
});
Note: Based off answer from nexuscreator. Very good answer, helped alot thank you.
worked for me :)
$search
.bind(settings.inputs.queryEvent, function() {
obj.queries.runSearch($(this).val());
})
.bind('keyup', function(e) {
obj.queries.runSearch($(this).val());
e.preventDefault();
});

jQuery UI Tooltip delayed loading

When hovering over a link, I'd like to wait at least a second before showing a tooltip with dynamically loaded tooltip.
What I've created is the follow jQuery Code:
$(document).ready(function () {
$("div#galleries ul li:not(.active) a").tooltip({
items: "a",
show: { delay: 1000 },
content: 'Loading preview...',
open: function (event, ui) {
previewGallery(event, ui, $(this));
}
});
});
function previewGallery(event, ui, aLinkElement) {
event.preventDefault();
ui.tooltip.load("http://www.someurl.com/Preview.aspx #preview");
}
Which seemed to work pretty fine, you can see it here:
http://fotos.amon.cc/ (simply hover over the list of galleries)
But I didn't realize at the beginning, that the loading of preview text happens immediately when hovering over the link. So if you quickly hover over all the links, you'll set up several requests:
From the users point of view (without knowing that requests are fired) it looks already the way I want, but how to only start loading the preview, when tooltip is actually showing up?
Thanks,
Dominik
What I did in the end was to use window.setTimeout and window.clearTimeout:
var galleryToolTipTimer = null;
var previewElement = null;
$(document).ready(function () {
$("div#photos div a img").tooltip();
$("div#galleries ul li:not(.active) a")
.tooltip({ items: "a", content: 'Loading preview...', disabled: true, open: function (event, ui) { previewElement.appendTo(ui.tooltip.empty()); } })
.mouseover(function (e) {
if (galleryToolTipTimer != null) { window.clearTimeout(galleryToolTipTimer); }
var aLinkObject = $(this);
galleryToolTipTimer = window.setTimeout(function () { previewGallery(aLinkObject); }, 500);
}).mouseleave(function (e) {
window.clearTimeout(galleryToolTipTimer);
$(this).tooltip("option", { disabled: true });
});
});
function previewGallery(aLinkElement) {
previewElement = $("<div/>").load(aLinkElement.closest("div").data("galleryPreview") + "/" + aLinkElement.data("path") + " #preview", function () {
aLinkElement.tooltip("open");
});
}
Works at least the way I want.
To see it in action, simply navigate to http://fotos.amon.cc/ and hover over one of the gallery links on the left for a preview:

triggering JQuery 'autocomplete' in a secondary textarea

We currently have two HTML textareas 'tinput'(primary) and 'toutput' (secondary) where we mimic the input in the primary to be reflected in the secondary as if someone is really typing in the secondary. The idea is to trigger an 'autocomplete' (over ajax) on the secondary. We have this working but not optimally.
We have attached a JQuery UI 'Autocomplete' (JQAC) to the secondary with a minLength:3 set. You may know that, normally, after 3 characters have been entered, JQAC 'buffers' the char entries thereon after and doesn't make an ajax call for every char that has been entered. Which is ideal.
However, with our secondary mimicking we have subverted this behavior, unfortunately, where after the 3rd char entry a JQAC ajax call is being made for every char after-- which is not optimal. We know why but don't know how to get around it. We believe we've subverted this because we are calling
$('#tinput').autocomplete('search',$('#tinput').val())
in the secondary's key handle, which by JQAC's documentation forces an ajax call.
To summarize, we need the secondary, that has JQAC attached to it, to behave as if someone were really typing into it and the JQAC behaving normally.
Here is JS for what we have as our char input mimic handling(we've changed variable names for this post so please ignore typos):
$("#tinput").on('input', function (e) {
$("#toutput").val($("#tinput").text());
var newEvent = jQuery.Event("keypress");
newEvent.which = e.which; // # Some key code value
$("#toutput").trigger(newEvent);
});
$("#toutput").keypress(function(e) {
$("#toutput").autocomplete('search',$("#toutput").val());
});
$( "#tinput" ).autocomplete({
appendTo: "#modalparent",
source: function( request, response ) {
$.ajax({
url: "https://xxxxxx",
type: "POST",
dataType: "json",
data: JSON.stringify({ "ourterm": request.term}),
success: function( data ) {
response( $.map( data.data.suggestions, function( item ) {
return {
label: item,
value: item
};
}));
}
});
},
minLength: 3,
select: function( event, ui ) {
// console.log( ui.item ?
// "Selected: " + ui.item.label :
// "Nothing selected, input was " + this.value);
},
open: function() {
$( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
},
close: function() {
$( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
}
});
Any thoughts would be appreciated. Thanks!
We have found an elegant solution. It was a minor modification to our original.
change the trigger event by the primary to 'input' instead of the original 'keypress'.
remove the handler for the secondary.
here is the updated JS:
$("#tinput").on('input', function (e) {
$("#toutput").val($("#tinput").text());
var newEvent = jQuery.Event("input");
newEvent.which = e.which; // # Some key code value
$("#toutput").trigger(newEvent);
});
and delete:
//$("#toutput").keypress(function(e) {
// $("#toutput").autocomplete('search',$("#toutput").val());
// });
DONE.

JQuery: Animated resize of div after loading content which ajax

I use the following code to reload the content of the div #console every second.
var update = setInterval(
function(){
$("#console").load("/console");
$.ajaxSetup({ cache: false });
}, 1000
);
This works quite well. Additionally I would like, that the resizement of the div is animated.
I tried something like this:
$("#console").load("/console").animate({height: 'auto'}, 1000);
But it doesnt work. Any ideas?
UPDATE:
Thanks for your answers, that worked!
But there is still another problem I did not see:
I call
animate({height: 'auto'}, 1000)
but animate just can deal with numerical values but now with 'auto'.
I intended, that the div always fits to the content. Can that be realized somehow?
this should work:
var update = setInterval(
function () {
$("#console").load("/console", function (responseText, textStatus, XMLHttpRequest) {
if (textStatus == "success") {
$("#console").animate({ height: 'auto' }, 1000);
}
});
$.ajaxSetup({ cache: false });
}, 1000
);

Error: cannot call methods on dialog prior to initialization; attempted to call method 'open'

Just a little preface... I am fairly new to jQuery so if something looks wrong or redundant please feel free to offer any helpful suggestions.
Now for the issue. I have 2 modals that are initiated from 2 separate links on the page.
The first modal was fairly problem free. It is a simple form that posts back to the same page. If you are wondering what the items in the "close:" section are, they are form fields that I want to clear the values on when the dialog is closed.
Once I added the second one I have had problems. This modal calls a coldfusion page into a modal to display pictures. The problems occur after opening up the second one. I can not close the second modal from the "close" button. I get the following error.
Error: cannot call methods on dialog prior to initialization; attempted to call method 'close'
I have to close it from the "x" in the upper right hand corner of the modal. After I close it, I get an error when trying to open up the first on.
Error: cannot call methods on dialog prior to initialization; attempted to call method 'open'
Here is the code for it.
$(document).ready(function() {
$(".dig").click(function() {
//based on which we click, get the current values
var cItemName = $("#checklistItemName").attr( "title");
var c2id = $("#check2id").attr( "title");
$("#ItemName").html(cItemName);
$("#ItemID").html(c2id);
$("#objCheckItemName").val(cItemName);
$("#objCheck2ID").val(c2id);
console.log(cItemName);
console.log(c2id);
});
$( "#image-form" ).dialog({
autoOpen: false,
height: 450,
width: 650,
modal: true,
buttons: {
"Submit": function() {
$('#mForm').submit();
return true;
},
Cancel: function() {
$( this ).dialog( "close" );
}
},
close: function() {
$('#defaultSectionName')
.val('');
$('#defaultSectionDesc_hidden')
.val('');
$('#Photo')
.val('');
$('#objCheck2ID')
.val('');
$('#Check21')
.val('');
},
zIndex: 500
});
The next piece of code is where I believe the problems are occurring.
$( "#image_trigger" )
.click(function() {
$( "#image-form" ).dialog( "open" );
});
var dlg=$('#register').dialog({
title: 'Case Pictures',
resizable: true,
autoOpen:false,
modal: true,
hide: 'fade',
width:650,
height:450,
buttons: {
close: function() {
$( this ).dialog( "close" );
}
},
zIndex: 500
});
$('#reg_link').click(function(e) {
e.preventDefault();
var linkurl=('assets/includes/modalPictures.cfm' + '?'
+ 'id=' + $("#objCheck2ID").val()
);
dlg.load(linkurl, function(){
dlg.dialog('open');
});
});
jQuery UI: 1.10.1
jQuery: 1.9.1
Server Side: Coldfusion
The HTML is pretty extensive. If you need to see any part of it please let me know. Thanks for your help!
Upper case?
Close: function() {
You have to initialize your jquery dialog before calling the open function.
<script type="text/javascript">
$(document).ready(function () {
initializeDialog();
});
</script>
And change your js file to have the dialog code in initializeDialog() function.
function initializeDialog(){
$("#your-dialog-id").dialog({
autoOpen: false,
buttons: {
"Cancel": function() {
$(this).dialog("close");
}
},
modal: true,
resizable: false,
width: 600px,
height: 400px
});
}
Thanks #bpjoshi

Resources