overriding _rendermenu in jquery autocomplete throws js error - jquery-ui

I tried to override _rendermenu in jquery autocomplete. The list is getting generated but every time I hover over the results i get the following js error
Uncaught TypeError: Cannot read property 'value' of undefined in jquery-ui.js
the code used is
$(function () {
var availableTags = [
"ActionScript",
"AppleScript",
"Asp",
"BASIC",
"C",
"C++",
"Clojure",
"COBOL",
"ColdFusion",
"Erlang",
"Fortran",
"Groovy",
"Haskell",
"Java",
"JavaScript",
"Lisp",
"Perl",
"PHP",
"Python",
"Ruby",
"Scala",
"Scheme",
"AA",
"BB",
"CC",
"DD",
"EE",
"FF",
"GG",
"HH",
"II",
"JJ",
"KK"
];
var atComplete=$( "#autoCompleteText" ).autocomplete({
delay:0,
source:availableTags,
autoFocus: true,
minLength: 0,
appendTo: "#result"
}).focus(function () {
$(this).autocomplete("search");
}).data('ui-autocomplete');
atComplete._renderMenu = function( ul, items ) {
var that = this;
$.each( items, function( index, item ) {
that._renderItem( ul, item );
});
};
atComplete._renderItem = function(ul, item) {
console.log("item in render item:",item);
return $("<li>").data("item.autocomplete", item).append("<a>" + item.label + "</a>").appendTo(ul);
};
});

If you look at the API docs for _renderMenu you'll see the reason you're getting an error:
Creation of the individual <li> elements should be delegated to _renderItemData(), which in turn delegates to the _renderItem() extension point.
You're using renderItem() directly. This means that you're not actually binding item data to the .data('ui-autocomplete-item') cache, which the widget attempts to read when drawing the menu -- but since it's undefined, the page is throwing an error.
To fix it all you need to do is change the call to _renderItem to call _renderItemData instead:
atComplete._renderMenu = function( ul, items ) {
var that = this;
$.each( items, function( index, item ) {
that._renderItemData( ul, item );
});
};

Related

Javascript jquery mobile error [duplicate]

I am trying to populate a JQM ListView with a local JSON information. However, no list items are created. Any help would be appreciated. Here is my code:
JSON File Structure:
[
{
"name" : "test"
"calories" : "1000"
"fat" : "100"
"protein" : "100"
"carbohydrates" : "800"
},
{
"name" : "test2"
"calories" : "10000"
"fat" : "343"
"protein" : "3434"
"carbohydrates" : "4343"
}
]
HTML:
<div data-role="page" data-title="Search" id="searchPage">
<ul data-role="listview" data-inset="true" id="searchFood">
</ul>
</div>
JS:
(Updated)
$(document).on("pageinit", "#searchPage", function(){
$.getJSON("../JS/food.json", function(data){
var output = '';
$.each(data, function(index, value){
output += '<li>' +data.name+ '</li>';
});
$('#searchFood').html(output).listview("refresh");
});
});
First of all, the return JSON array is wrong, values (properties) should be separated by commas.
var data = [{
"name": "test",
"calories": "1000",
"fat": "100",
"protein": "100",
"carbohydrates": "800",
}, {
"name": "test2",
"calories": "10000",
"fat": "343",
"protein": "3434",
"carbohydrates": "4343",
}];
Second mistake, you should read value object returned by $.each() function not data array.
$.each(data, function (index, value) {
output += '<li>' + value.name + '</li>';
});
jQueryMobile only enhances the page once when it is loaded. When new data is added dynamically to the page, jQueryMobile must be made aware of the data for the data to be enhanced.
After extracting data from JSON array, append them then refresh listview to restyle newly added elements.
$('#searchFood').html(output).listview("refresh");
Demo

jQuery UI Autocomplete With Json Not Showing Suggestions

I'm experiencing some troubles with an autocomplete component from jQueryUi. The list with autocomplete suggestions doesn't appear.
I've tested the following code (from jQuery UI) and, despite servlet is sending a JSON object and "data" variable is storying it, component is still not showing suggestions list.
Also I tried the component with a simple list as source (like here), and it worked fine.
Have you any idea on what would be happening?
<script>
$(function() {
var cache = {};
$( "#bear" ).autocomplete({
minLength: 2,
source: function( request, response ) {
var term = request.term;
if ( term in cache ) {
response( cache[ term ] );
return;
}
$.getJSON( "/animals/MaintainMamals?operation=14", request, function( data, status, xhr ) {
cache[ term ] = data;
response( data );
});
}
});
});
</script>
<form>
<div class="ui-widget">
<label for="bear">Bear name (type a piece of name): </label>
<input id="bear" name="bear" class="text ui-widget-content ui-corner-all"/>
</div>
</form>
Json object used in testing (I tried the stuff with a simple jSon built with just a String refering to "name" property, with the same [bad] results):
[
{
"id": 1234567,
"name": "Yogi Bear",
"activity": {
"handler": {
"interfaces": [
{}
],
"constructed": true,
"persistentClass": {},
"getIdentifierMethod": {
"clazz": {},
"slot": 2,
"name": "getCod",
"returnType": {},
"parameterTypes": [],
"exceptionTypes": [],
"modifiers": 1,
"root": {
"clazz": {},
"slot": 2,
"name": "getId",
"returnType": {},
"parameterTypes": [],
"exceptionTypes": [],
"modifiers": 1,
"override": false
},
"override": false
},
"setIdentifierMethod": {
"clazz": {},
"slot": 3,
"name": "setId",
"returnType": {},
"parameterTypes": [
{}
],
"exceptionTypes": [],
"modifiers": 1,
"root": {
"clazz": {},
"slot": 3,
"name": "setId",
"returnType": {},
"parameterTypes": [
{}
],
"exceptionTypes": [],
"modifiers": 1,
"override": false
},
"override": false
},
"overridesEquals": false,
"entityName": "com.zoo.Activity",
"id": 7105,
"initialized": false,
"readOnly": false,
"unwrap": false
}
}
}
]
The autocomplete widget expects each item in your array to have a label property, a value property, or both. Since your data does not have either, you can either:
Tweak your server-side data source to return items that meet that criteria, or
Transform the data from your server-side code to match the criteria after you make the request.
I can only provide an answer to #2 since I don't see your server-side code, so here's how you would do that:
$(function() {
var cache = {};
$( "#bear" ).autocomplete({
minLength: 2,
source: function( request, response ) {
var term = request.term;
if (term in cache) {
response(cache[ term ]);
return;
}
$.getJSON("/animals/MaintainMamals?operation=14", request, function (data, status, xhr) {
/* Add a `label` property to each item */
$.each(data, function (_, item) {
item.label = item.name;
});
cache[term] = data;
response(data);
});
}
});
});
Here's an example that fakes an AJAX request--other than that it should be similar to your situation.

Auto-focus feature for tag-it

In the plugin tag-it from https://github.com/aehlke/tag-it (demo - http://aehlke.github.com/tag-it/examples.html), how is it possible to add the autofocus (i.e If set to true the first item will automatically be focused when the menu is shown) feature in the jquery - tag-it.js?
EDIT: The feature should also enable the suggestion to be inputted or made appear in the input box when 'Enter key' is hit.
I was able to solve the problem of entering the auto-focused suggestion by making some of the following in the tag-it.js file:
Defined a variable for receiving the value of focused suggestion on line 113 just after var that = this;:
var that = this;
var focuse;
On or after line 279 and function - this.tagInput.keydown(function(event) {}), the following has to be added:
.on( "autocompletefocus", function( event, ui ) {
focuse = ui.item.value;
})
Then finally within the function this.tagInput.keydown(function(event) {}), replace that.createTag(that._cleanedInput()); with:
if (focuse !== '' && event.which === $.ui.keyCode.ENTER)
{
that.createTag(focuse);
focuse = '';
}
else
{
that.createTag(that._cleanedInput());
}
To enable to autofocus, add the autocomplete (autocomplete: {autoFocus: true}) option in the file that calls the tagit plugin as:
$("#tags").tagit({
availableTags : availableTags,
autocomplete: {autoFocus: true}
});
Here is an example : http://jsfiddle.net/UQTY2/8/
I hope that is what you are expecting
$(document).ready(function(){
var availableTags = [
"ActionScript",
"AppleScript",
"Asp",
"BASIC",
"C",
"C++",
"Clojure",
"COBOL",
"ColdFusion",
"Erlang",
"Fortran",
"Groovy",
"Haskell",
"Java",
"JavaScript",
"Lisp",
"Perl",
"PHP",
"Python",
"Ruby",
"Scala",
"Scheme"
];
$("#tags").tagit({
availableTags : availableTags,
showAutocompleteOnFocus : true,
autocomplete: {delay: 0, minLength: 0, autoFocus: true},
});
});

Jqgrid + JQuery Autocomplete multiple input

I have the grid set up and working nicely. I wanted to add multiple input autocomplete functionality to the form view in JqGrid. The multiple autocomplete works but the extractLast function seems not to be working and I can add duplicate inputs.
Heres the code:
var availableTags = [
"ActionScript",
"AppleScript",
"Asp",
"BASIC",
"C",
"C++",
"Clojure",
"COBOL",
"ColdFusion",
"Erlang",
"Fortran",
"Groovy",
"Haskell",
"Java",
"JavaScript",
"Lisp",
"Perl",
"PHP",
"Python",
"Ruby",
"Scala",
"Scheme"
];
function split( val ) {
return val.split( /,\s*/ );
}
function extractLast( term ) {
return split( term ).pop();
}
function autocomplete_element(value, options) {
// creating input element
var $ac = $('<input type="text"/>');
// setting value to the one passed from jqGrid
$ac.val(value);
// creating autocomplete
$ac.autocomplete(
{source: function( request, response ) {
// delegate back to autocomplete, but extract the last term
response( $.ui.autocomplete.filter(
availableTags, extractLast( request.term ) ) );
},
focus: function() {
// prevent value inserted on focus
return false;
},
select: function( event, ui ) {
var terms = split( this.value );
// remove the current input
terms.pop();
// add the selected item
terms.push( ui.item.value );
// add placeholder to get the comma-and-space at the end
terms.push( "" );
this.value = terms.join( ", " );
return false;
}
});
// returning element back to jqGrid
return $ac;
}
function autocomplete_value(elem, op, value) {
if (op == "set") {
$(elem).val(value);
}
return $(elem).val();
}
Grid colmodel:
{
...
editable: true,
edittype: "custom",
editoptions: {
custom_element: autocomplete_element,
custom_value: autocomplete_value
}
},
Which I found from JQuery UI
and German Rumm's blog
Any suggestions?
UPDATED!
It looks like example on jQuery UI site allows selecting the same element multiple times as well. The problem is in source function - it always checks the last term against all available terms when creating a suggest list.
Modify select callback to display only those terms that are not already present in a field.
source: function(request, response) {
var terms = request.terms.split(/,\s*/);
var last_term = terms.pop();
var tags = $.grep(availableTags, function(el) {
return $.inArray(el, terms) == -1);
});
response($.ui.autocomplete.filter(tags, last_term))
}

Jquery Autocomplete

iam using jquery autocomplete in asp.net project. it's not working. do you have any idea. the code is given below.
<script type="text/javascript">
$(function () {
$('#clientabbrev').val("");
$("#clientstate").autocomplete({
source: "clientstates.aspx",
select: function (event, ui) {
$('#clientstateid').val(ui.item.clientid);
$('#clientstateabbrev').val(ui.item.clientabbrev);
}
});
$("#clientstate_abbrev").autocomplete({
source: "clientstatesabbrev.aspx",
minLength: 2
});
});
</script>
problem is states.aspx returning the data but it is not showing in the jquery autocomplete control.
Your server needs to return a JSON serialized array of objects with properties id, label, and value. E.g. :
[ { "id": "1", "label": "Mike Smith", "value": "Mike Smith" }, { "id": "2", "label": "Bruce Wayne", "value": "Bruce Wayne" }]
Can you confirm with firebug or Fiddler that your server is returning the correct response?
If you're having trouble serializing your data in C#, you can try using JavaScriptSerializer like this:
var result = from u in users
select new {
id = u.Id,
value = u.Name,
label = u.Name
};
JavaScriptSerialier serializer = new JavaScriptSerializer();
var json = serializer.Serialize(result);
// now return json in your response

Resources