Jquery Autocomplete - empty source - jquery-ui

I'm using JQuery Autocomplete feature.
The source come from url.
I wanted to know if I can know that the source is empty,
I mean i want to print alert message when the source the come back from the url is empty.
my code is
$(function() {
$( "#tags" ).autocomplete({
source: remoteurl
select: function (event, ui) {
alert(ui.item.value);
return true;
}
});
});
Thanks,
John.

Related

JQuery-ui Tabs - reload page with completely new content not working

I'm loading in a report and displaying it with jquery-ui in tab format. The report is returned by an ajax call in json, and a function is formatting it into HTML. Example code below:
<div id="reportdiv">
</div>
<script>
function displayreport(objectid)
{
$( "#reportdiv" ).hide();
$( "#reportdiv" ).html("");
$.ajax({
type: "GET",
headers: { 'authtoken': getToken() },
url:'/reportservice/v1/report/'+objectid.id,
success: function(data){
if(data == null)
{
alert("That report does not exist.");
}
else
{
var retHTML = dataToTabHTML(data.config);
$("#reportdiv").html(retHTML).fadeIn(500);
$(function() {
tabs = $( "#reportdiv" ).tabs();
tabs.find( ".ui-tabs-nav" ).sortable({
axis: "x",
stop: function() {
tabs.tabs( "refresh" );
}
});
});
}
}
});
}
</script>
This works fine the first time displayreport is called. However, if the user enters another value and runs displayreport again, the "tabs" format is completely lost (the tabs are displayed as links above my sections, and clicking on a link takes you to that section further down the page).
I figured completely re-setting the reportdiv html at the beginning of the function would bring me back to original state and allow it to work normally every time. Any suggestions?
After more testing, found that destroy was the way to go. If I've set up tabs already, run the destroy, otherwise, skip the destroy (http://jsfiddle.net/scmxyras/1/) :
if(tabs!=undefined)$( "#reportdiv" ).tabs("destroy");

Jquery tabs ui 1.10 tabs refresh

I m using jquery tabs .Like to know how to refresh content on tab click .
I m using document ready function with
$(#TABS).TABS()
$(document).ready(function(){
$("#tabs").tabs();
$("#tabs").bind("tabshow",function(event,ui){window.location.href=ui.tab;});});
See the "select event" here
It says:
This event is triggered when clicking a tab.
Code examples
Supply a callback function to handle the select event as an init option.
$( ".selector" ).tabs({
select: function(event, ui) { ... }
});
Bind to the select event by type: tabsselect.
$( ".selector" ).bind( "tabsselect", function(event, ui) {
...
});
in jQuery-UI 1.10, the following should help:
$("#TABS").tabs("load", $("#TABS").tabs("option", "active"));
Edit: More code details:
$(function() {
$("#TABS").tabs();
$("#TABS").on('click','li',function(event,ui) {
$("#TABS").tabs("load", $("#TABS").tabs("option", "active"));
});
});
Here is a link to jsfiddle

jQueryUI AutoComplete Remote Source misdirected

I have a jQuery AutoComplete for a text box. The code to initialize the AutoComplete is:
$("#SearchBox1").autocomplete({
autoFocus: false,
source: "Home/AutoComplete",
select: function (event, ui) {
alert(ui.item.label);
}
When the URL doesn't contain a path, the jQuery Autocomplete works, but when the URL contains something like localhost/home/GoToPage/?page=2, then AutoComplete attempts to use /home/GoToPage/Home/AutoComplete. At this point AutoComplete is completely broken, because it is using a bad path.
Are there any good ways of dealing with this problem?
One possible solution is based off of MadRabbit's comments, and Dan Wellman jQueryUI Autocomplete tutorial at http://net.tutsplus.com/tutorials/javascript-ajax/how-to-use-the-jquery-ui-autocomplete-widget/
$("#SearchBox1").autocomplete({
autoFocus: false,
source: function (request, callback) {
//pass request to server
$.getJSON("Home/AutoComplete?term=" + request.term, request, function (responseData) {
//create array for response objects
var suggestions = [];
//process response
$.each(responseData, function (i, val) {
suggestions.push(val);
});
//pass array to callback
callback(suggestions);
});
},
select: function (event, ui) {
$('#SearchBox1').val(ui.item.label);
}
});

jQuery UI tabs: ajaxOption don't work as it should be

jQuery UI tabs options have ajaxOptions.
I have next code:
$('#tabs').tabs({
cookie:{expires:1},
cache:true,
ajaxOptions:{
beforeSend: function(xhr,settings){
$(".ajax-gif").css("top",$(window).scrollTop()).show();
},
error: function(xhr,status,index,anchor){
$(anchor.hash).html("Couldn't load this tab.");
},
complete: function(xhr,textStatus){
$(".ajax-gif").hide();
}
}
});
But ajax-gif doesn't show up.
The same code in jQuery ajaxSetup (without jQuery UI) works perfect for usual ajax requests (not in ui tabs). Where did I mistake?
Thanks!
clarification
Usual ajax requests use POST form and tabs use GET form.
What version of jQuery UI tabs are you using? ajaxOptions option is only available up to version 1.8, you can see at http://api.jqueryui.com/1.8/tabs.
For current version (1.11) you would use beforeLoad property. Like this:
$('#tabs').tabs({
beforeLoad: function (event, ui) {
$(".ajax-gif").css("top",$(window).scrollTop()).show();
ui.jqXHR.complete(function(data) {
$(".ajax-gif").hide();
});
ui.jqXHR.error(function(data) {
$(anchor.hash).html("Couldn't load this tab.");
});
}
});
I found solution:
$(document).ajaxSend(function(){
$(".ajax-gif").css("top",$(window).scrollTop()).show();
});
$(document).ajaxComplete(function(){
$(".ajax-gif").hide();
});

Overriding jQuery Dialog method

I am trying to override close method of Jquery Dialog method.
Code :
jQuery.Dialog.close = function() {
alert('my close');
}
But its not working. Please help.
There is an event called beforeClose which would allow you to do what you want, I think. When it fires, you can hide the dialog, then return false, which would prevent the dialog from actually closing.
$( ".selector" ).dialog({
beforeClose: function(event, ui) {
$(this).hide();
return false;
}
});
Reference: http://jqueryui.com/demos/dialog/ under the Events tab below the example
You're setting it up wrong. Check this out to see how to do it correctly.
Ok, so that link doesn't take you where I thought it would. Here's the relevant bit from jqueryui.com.
closeType:dialogclose
This event is triggered when the dialog is closed.
Code examples
Supply a callback function to handle the close event as an init option.
$('.selector').dialog({
close: function(event, ui) { ... }
});
Bind to the close event by type: dialogclose.
$('.selector').bind('dialogclose', function(event, ui) {
...
});

Resources