I'm integrating a Google Map API that uses Geonames and Select2 to allow the user to enter the cities that he/she has visited.
Currently, I am trying to find a way for the search area to show the selections the user made in a previous session upon reloading the page (e.g., if the user already entered Paris, France in a previous session, then Paris, France should be preloaded in the search area upon reloading). The selections are stored in a database, but at the moment I'm only able to put one of the previously selected cities in the search area by repassing it through Geonames (I need to pass through Geonames to pass the lat & long). I'd like to repass as many locations as the user entered in the previous session.
The code I am using for this is below - thanks for your help:
function locationFormatResult(location) {
return location.name + ", " + location.adminName1 + ", " + location.countryName;
}//results format
function locationFormatSelection(location) {
return location.name + ", " + location.adminName1 + ", " + location.countryName;
}//selection format
$(function () {
$('#citiestext').select2({
id: function(e) { return e.name + ',' + e.adminName1 + ',' + e.countryName + ',' + e.lat + ',' + e.lng},
placeholder: 'Location',
multiple: true,
allowClear: true,
width: '350px',
height: '50px',
overflow: 'auto',
minimumInputLength: 2,
ajax: { //this is the ajax call for when the user selects a city
url: 'http://ws.geonames.org/searchJSON',
dataType: 'jsonp',
data: function (term) {
return {
featureClass: "P",
style: "medium",
isNameRequired: "true",
q: term
};
},
results: function (data) {
return {
results: data.geonames
};
}
},
initSelection : function(element, callback){
for (var i=11;i<13;i++){
$.ajax("http://ws.geonames.org/searchJSON",{//ajax for preloading
dataType: 'jsonp',
data:{
maxRows:1,
q: i}//for this example, I'm using the numbers 11 & 12 as my Geonames queries just to test the preloading functionality (both numbers do have corresponding locations in Geonames if run through a query)
}).done(function(data){callback(data.geonames);}); //PROBLEM: currently is only returning the location for "12" - I need it to return locations for 11 and 12 in the select area
}},
formatResult: locationFormatResult,
formatSelection: locationFormatSelection,
dropdownCssClass: "bigdrop",
escapeMarkup: function (m) { return m; }
});
});
jsfiddle: http://jsfiddle.net/YDJee/ (trying to get more than one entry in the select box)
The point is that the callback needs to be called with the array of objects for multiple select2.
In this case, as you need to gather each json object from an ajax call, you need to use jQuery deferreds.
Something like that:
initSelection : function(element, callback){
var result = new Array();
var f = function(i) {
return $.ajax("http://ws.geonames.org/searchJSON",{
dataType: 'jsonp',
data:{
maxRows:1,
q: i
},
success: function(data) { result.push(data);}})
};
var def = [];
for (var i=11;i<13;i++){
def.push(f(i))
}};
$.when.apply($, def).done(function() {
callback(result);
});
}
For those also using an id callback, and who find their issue is not an async problem, take a look at this (which remains a necessary fix as of this posting).
Related
I'm trying to use the newest version of Select2 to query my site's API and return a multiple-select. It's fetching the right data and it's even formatting the dropdown properly, combining two keys in the returned objects as "(A-123) John Johnson", but the dropdown's not responding to mouseover or clicks.
I'm using the select2.full.min.js and select2.min.css files. For the purposes of the project, I'm including them in the directory and accessing them through Bundles in cshtml, instead of accessing them via a CDN.
HTML:
<div>
<select class="user-list" multiple="multiple" style="width: 100%">
</select>
</div>
At the moment, it's looking like this, which is how I want it:
Not sure if this is relevant, but when I browse the generated source code while searching, the input looks fine, but the dropdown's code is greyed out as though it were hidden.
Per other suggestions I've found on here and elsewhere, I've tried a few different solutions. I eventually found out how templateResult and templateSelection are supposed to work (not particularly thanks to the documentation), and tried returning the ID as well, but I still can't seem to actually select anything, or even get a response when I hover over the options.
Here's what I wound up with, including some debugging to make sure the returned object is valid.
JS:
// Setup autocomplete/select for User filter
$(".user-list").select2({
ajax: {
url: "[api url]",
type: "GET",
dataType: "json",
data: function (params) {
return {
search: params.term, // search term
page: params.page
};
},
processResults: function (data) {
console.log(JSON.stringify(data));
return {
results: data
};
},
},
escapeMarkup: function (markup) { return markup; },
id: function (data) { return data.Id.toString(); },
minimumInputLength: 1,
templateResult: function (data, page) {
return "(" + data.User + ") " + data.Name;
},
templateSelection: function (data, page) {
return "(" + data.User + ") " + data.Name;
}
})
The ID is a GUID, and there are two other fields, which I'll call Name and User.
Data Sample:
[
{
"Id":"a1a1a1a1-a1a1-a1a1-a1a1-a1a1a1a1a1a1",
"Name":"John Johnson",
"User":"A-123"
},
{
"Id":"b2b2b2b2-b2b2-b2b2-b2b2-b2b2b2b2b2b2",
"Name":"Tom Thompson",
"User":"B-456"
}
]
I hate to add to the pile of questions that seem to be about this, but most results I've found have been for the older version with significantly different options, and they just haven't worked for me yet.
The way select2 operates is that it uses the "id" values of each data object and puts those into the original Select element as the selected option(s). This is case-sensitive.
By default, it displays the dropdown options and the selected element by whatever the "text" value is of the data object. This does not allow for custom formatting.
If (like me) you want to return different data options, you still need to return a field as "id", or re-map a field to "id" in an object returned in the processResults option under ajax. Then use the templateResult and templateSelection settings with your other returned data to display what you want.
If you return and parse everything correctly except for the id, you may wind up with an otherwise functional list, but be unable to select any options.
The requirements for the dropdown changed a bit with my project, but here's where I wound up. It works fine the multiple="multiple" attribute added to to make it a multi-select, too.
<select class="select2" style="width:100%; height: 100%;">
<option></option>
</select>
$(".select2").select2({
ajax: {
url: "[api url]",
method: "get",
data: function (params) {
return {
search: params.term
};
},
processResults: function (data) {
return {
results: data
};
},
cache: true
},
placeholder: "Enter a User ID or Name",
templateResult: function(data) {
return "(" + data.user + ") " + data.name;
},
templateSelection: function(data) {
return "(" + data.user + ") " + data.name;
}
}).ready(function () {
$(".select2-selection__placeholder").text("Enter a User ID or Name")
})
It's possible part of my initial issue was all the attempts at implementing fixes for older versions of Select2, which had a completely different set of options/settings available.
Additionally, a bit of a side-note, the "placeholder" attribute doesn't currently work with custom templating. It tries to force the placeholder text into the Result format, which shows "(undefined) undefined" with this code. To fix it requires an empty option and to replace the text on select2.ready.
I had the same problem. Solution:
added this part :
_.each(data.ResultObject, function (item) { item.id = item.K_CONTACT; });
(used underscore)
for my init
$(".js-data-example-ajax").select2({
ajax: {
url: "api/MyApi/MyApi",
method: "POST",
dataType: 'json',
delay: 250,
data: function (params) {
return {
SearchPart: params.term, // search term
page: params.page
};
},
processResults: function (data, params) {
params.page = params.page || 1;
_.each(data.ResultObject, function (item) { item.id = item.K_CONTACT; });
return {
results: data.ResultObject,
pagination: {
more: (params.page * 30) < data.total_count
}
};
},
cache: true
},
multiple: true,
escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
minimumInputLength: 3,
tags: true,
templateResult: self.Select2FormatData, // omitted for brevity, see the source of this page
templateSelection: self.Select2FormatDataSelection // omitted for brevity, see the source of this page
});
I use autocomplete for two textboxes. That works fine using this code:
$('[id$=PlaceOfDeparture]:not(.ui-autocomplete-input), [id$=PlaceOfArrival]:not(.ui-autocomplete-input)').live('focus', function () {
$(this).autocomplete({
source: function (request, response) {
$.ajax({
url: "http://dev.virtualearth.net/REST/v1/Locations",
dataType: "jsonp",
data: {
key: 'mykey',
q: request.term
},
jsonp: "jsonp",
success: function (data) {
var result = data.resourceSets[0];
if (result) {
if (result.estimatedTotal > 0) {
response($.map(result.resources, function(item) {
return {
data: item,
label: item.name + '( ' + item.address.countryRegion+ ')',
value: item.name
};
}));
}
}
}
});
},
minLength: 1,
select: function (event, ui) {
$(this).val(ui.item.value);
travel = $(this).closest('div').parent();
$(this).change();
updateMap();
},
open: function () {
$(this).removeClass("ui-corner-all").addClass("ui-corner-top");
},
close: function () {
$(this).removeClass("ui-corner-top").addClass("ui-corner-all");
}
});
});
});
That works fine. Then i want bing map to draw a line between the locations on a map.
bingMap = new Microsoft.Maps.Map(document.getElementById("map_canvas"), {
credentials: "mykey",
mapTypeId: Microsoft.Maps.MapTypeId.auto,
enableSearchLogo: false,
enableClickableLogo: false,
showDashboard: false,
center: new Microsoft.Maps.Location(55.7, 13.1833333),
zoom: 10
});
Microsoft.Maps.loadModule("Microsoft.Maps.Directions", { callback: directionsModuleLoaded });
I use this to set the waypoints to the bing map:
var startwaypoint = new Microsoft.Maps.Directions.Waypoint({ address: placeOfDeparture });
bingDirections.addWaypoint(startwaypoint);
// end
var endwaypoint = new Microsoft.Maps.Directions.Waypoint({ address: placeOfArrival });
bingDirections.addWaypoint(endwaypoint);
// Calculate directions, which displays a route on the map
bingDirections.calculateDirections();
The first two ajaxpost works just fine and gives me the "statusCode":200 which i read is supposed to be good :) But then when i do the bingDirections.calculateDirections(); it returns this:
microsoftMapsNetworkCallback({"resolvedWaypoints":[[{"failed":true,"invalidCredentials":false,"inputType":0,"latitude":0,"longitude":0,"rooftopLatitude":0,"rooftopLongitude":0,"address":null,"disambiguation":null,"locationIdentifier":null},{"failed":true,"invalidCredentials":false,"inputType":0,"latitude":0,"longitude":0,"rooftopLatitude":0,"rooftopLongitude":0,"address":null,"disambiguation":null,"locationIdentifier":null}]]}, 'd6392');
Check the response header. There might be a property called X-MS-BM-WS-INFO set to 1. This would indicate that your request was rate limited. If you make a lot of requests using a basic key (non-Enterprise/licensed account) and your account is generating transactions at a rate that will exceed the free terms of use your account is rate limited. This results in requests coming back empty. This is done to ensure that basic accounts don't affect the service for Enterprise users. Note that doing autocomplete is not within the terms of use and is likely the cause of the rate transactions being outside the free terms of use.
Also see this documentation: http://msdn.microsoft.com/en-us/library/ff701703.aspx
I have a view(MVC3) that users place orders from. The view is bound to a model that i use to display modelitems from. There are two functionalities on this view. First you enter the customers details and then you choose the items the user has ordered. This is the code i´m using to build another model to be sent back to serverside:
var modelItems = {
ModelID: [],
Amount: []
};
var serviceModel = {
Name: $.trim($('#name').val()),
Customernumber: $.trim($('#customernumber').val()),
Address1: $.trim($('#address1').val()),
Address2: $.trim($('#address2').val()),
Zipcode: $.trim($('#zipcode').val()),
City: $.trim($('#city').val()),
Country: $.trim($('#country').val()),
Phone: $.trim($('#phone').val()),
Mobile: $.trim($('#mobile').val()),
Email: $.trim($('#email').val())
};
$('div.modelSpan').each(function (i) {
var textBox = $(this).children();
var value = $(textBox).val();
if (value != '0' && value != '') {
var modelID = $(textBox).attr('name');
modelItems.ModelID.push(modelID);
modelItems.Amount.push(value);
}
});
var accessory = {
ModelItems: modelItems,
ServiceModel: serviceModel
};
$.ajax({
url: '/Site/Order', //Renamed sec reasons
type: "POST",
data: JSON.stringify(accessory),
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (ordernumber) {
window.location.href = "/Site/OrderInfo/" + businessAB + "/" + ordernumber;
},
error: function () {
alert('error');
}
});
Cool thing in MVC3 is that my accessory automatically binds to my model on serverside called AccessoriesModel. On callback success i´m setting new href to a receipt site to show user what has been created. This all works but my issue is that i would like the receipt view(OrderInfo) to be returned from my controller that receives the [httppost] and not setting new href. Is there a way to do this? This is easy when using regular form submit but because the values for my model dont come from one form it complicates things. Maybe I shouldn´t use AJAX?
You could use knockout JS with Ajax and render your pages with a mixture of JavaScript objects and regular html.
What i am trying to do is get a user to change one drop down, which then calls an ajax function which posts to the code behind (vb.net file) then clears and populates another asp dropdown list with the data returned from the function..hope i made sense
<script>
$(document).ready(function () {
$('.manuf').change(function () {
$.ajax({
type: "POST",
url: "ajax.aspx/GetModel",
data: '{' +
'ManufID:"' + $('.manuf').val() + '"' +
'}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
var data = json_parse(msg.d);
if (data.error) {
alert("Error!");
return;
}
alert(data.model);
},
error: function(msg) {
alert('Get Details Failure: ' + msg);
}
});
});
});
</script>
My bad - you can write your ajax call like this -
$.ajax({ url :"/website/myurl", type:"POST", contentType:"application/json;", dataType:"json", data : "{ 'id' : '" + $("select[id*=selCurrentManuf]").val() + "' } ",
success : function (return_data) {
var data = $.parseJSON(return_data);
// code to add the contents of data to other list
$("select[id*=selCurrentModel]").empty().append($("<option>").attr("value","0").text("Choose..."));
// for the dropdown list clear all options and add a 'choose...' as the first option
$.each(data, function (i, d) { $("<option>").attr("value", d.k).text(d.v).appendTo($("select[id*=selCurrentModel]")); });
}, error:function () {
// handle error
}
});
I assume that you know how to fetch data from the backend via ajax. Something like this -
$.ajax({ url :"/website/myurl", type:"POST", dataType:"application/json"; data:"json",
success : function (return_data) {
var data = $.parseJSON(return_data);
// code to add the contents of data to other list
}, error:function () {
// handle error
}
});
Lets say you are getting it in a variable data which is an array.
You may try something like this -
$("select[id*=selCurrentModel]").empty().append($("<option>").attr("value", "0").text("Choose..."));
// for the dropdown list clear all options and add a 'choose...' as the first option
$.each(data, function (i, d) { $("<option>").attr("value", d.k).text(d.v).appendTo($("#ddlExperience")); });
// user $.each to iterate the data object which
You may try things on these lines.... Hope this helps.
I have a geo collection that contains items like:
[state name]
[city], [state]
[country]
A text box is available for a user to begin typing, and a jQuery autocomplete box fills displays possible options.
The URL structure of the post request will depend on which was selected from the collection above, ie
www.mysite.com/allstates/someterms (if a country is selected)
www.mysite.com/city-state/someterms (if a city, state is selected)
www.mysite.com/[state name]/someterms (if a state is selected)
These are already defined in my routes.
I was initially going to add some logic on the controller to determine the appropriate URL structure, but I was thinking to simply add that as an additional field in the geo table, so it would be a property of the geo collection.
Here is my jQuery function to display the collection details when, fired on keypress in the textbox:
$(function () {
$("#txtGeoLocation").autocomplete(txtGeoLocation, {
source: function (request, response) {
$.ajax({
url: "/home/FindLocations", type: "POST",
dataType: "json",
selectFirst: true,
autoFill: true,
mustMatch: true,
data: { searchText: request.term, maxResults: 10 },
success: function (data) {
response($.map(data, function (item) {
return { label: item.GeoDisplay, value: item.GeoDisplay, id: item.GeoID }
}))
}
})
},
select: function (event, ui) {
alert(ui.item ? ("You picked '" + ui.item.label + "' with an ID of " + ui.item.id)
: "Nothing selected, input was " + this.value);
document.getElementById("hidLocation").value = ui.item.id;
}
});
});
What I would like is to have structure the URL based on an object parameter (seems the simplest). I can only seem to read the parameters on "selected", and not on button click.
How can I accomplish this?
Thanks.
To resolve this, I removed the select: portion from the Javascript, and added the selected object parameters in the MVC route sent to my controller.