[jQuery] how to get json or xml to be datasource of autocomplete
Hi!
I'm making an app which benefit a lot from using autocomplete.
However, I need to use an external file as source, and json or xml
would suit the backend people best (ruby on rails).
I have made an effort of trying to use xml, but ain't succeding. Maybe
some of you guys can help me out? My xml file is really
straightfoward:
<customers>
<customer name="somename" aname="somevalue">
<project name="test1" />
<project name="test2" />
<project name="test3" />
</customer>
</customers>
and my my jquery looks like this:
--------------------------------------------------------------------------------------------------------------------
function parseXML(xml) {
var results = [];
$(xml).find('customer').each(function(){
var text = $(this).attr("name");
var value = $(this).attr("aname");
results[results.length] = {"data": this, "name": $
(this).attr("name"), "aname":$(this).attr("aname")};
});
return results;
};
$(document).ready(function(){
$("#client").autocomplete("testKunde.xml", {
parse: parseXML,
width: 320,
max: 10,
highlight: false,
scroll: true,
scrollHeight: 300,
formatItem: function(item) {
return item.name;
},
formatResult: function(item) {
return item.name;
}
})
.result(function(event, item) {
$("#clientAnonymous").val(item.aname);
});
});
</script>
--------------------------------------------------------------------------------------------------------------------
I'm in the woods why this isn't working... Any helpers?
Thanks for the help!