views:

104

answers:

2

Hello:

I wanna make certain fields in my page to show me like a list of possible finishing sentences just like Google does.

I think this is done via jQuery, but I'm not sure.

The textfield is named commodity.

Let's say I write General and in the list, array, or DB I have General Commodity I should show me General Commodity under the textfield.

Thanks in advance!

-------UPDATE-------

 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.5/jquery-ui.min.js"&gt;
var data = "General Commodity,General Store,General Custer,General Mills".split(",");

    $("#Ccommodity").autocomplete({
            source: data
        });
</script>

When I go to the Ccommodity input textfield I write G and nothing happens, maybe I have some syntax error there.

Thanks for the help

+3  A: 

You will probably want to look into the jQueryUI Autocomplete widget. It has several ways of providing lists of suggestions, and there will almost certainly be something in there that will work for you.

Here's a very basic demo: http://jsfiddle.net/QU9st/

Here's the source for that:

$(function() {
    var data = "General Commodity,General Store,General Custer,General Mills".split(",");
    $("#commodity").autocomplete({
            source: data
        });
});

That demo simply uses a static string array as the datasource, but you can also use remote datasources, in formats like JSON or XML.

To get it, just drop this somewhere in your <head> tag:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.5/jquery-ui.min.js"&gt;&lt;/script&gt;

Of course, you will also need to include jQuery:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;
Ender
That is exactly what I'm looking for. Do I need to install the jquery-ui plugin for Grails?
fgualda87
Yes, you need to specifically include jQueryUI on your page. You can download it from their site or grab a link to it from Google's code repository here: http://code.google.com/apis/libraries/devguide.html
Ender
How do I install jQuery UI in Grails?
fgualda87
You don't really "install" it. It's a javascript library, just like jQuery. You just have to include it on your page with a script tag.
Ender
And put it where? Because I know there is a jQuery UI Plugin for Grails, but I'm not sure if it is the same http://www.grails.org/plugin/jquery-ui
fgualda87
Like, I don't know if I really need to install the plugin, or just download the js library and put it somewhere.
fgualda87
Ah, I see. Admittedly, I don't know anything about Grails, but yes, I think that the plug-in you linked will suit your needs. You shouldn't really need the plug-in though. Probably the easiest thing to do is just link to the copy of it that Google hosts. I'll edit my answer to show how that's done.
Ender
For some reason is not working... Maybe I'm doing something wrong... I'll update the question with some code so you can see
fgualda87
Have you also included jQuery? See my updated answer.
Ender
Yes, I have other jQuery functions working. Specially on (document).ready
fgualda87
But again, I don't need that, by installing the plugin and putting the tag <g:javascript library="jquery" plugin="jquery" /> It loads the jQuery plugin, which is the same as putting it the HTML way. But that's why I'm having problems with the jQuery UI plugin, because UI is a plugin of jQuery.
fgualda87
It looks like you haven't wrapped the call to autocomplete in $(function() { }); (which is the shorthand for (document).ready). Perhaps that is the problem?
Ender
The thing is that Grails has the prototype library by default, and by using the $(function{} ..... ) it reads the prototype $, not the jQuery one. I'll check and let you know.
fgualda87
No, it loads perfectly, like no errors on the page, but nothing happens when I write in the txtfield :/
fgualda87
$ is just an alias for jQuery, so you can do jQuery(function() { }); as well.
Ender
I know, I read that somewhere too. But no. I changed it and nothing is happening :S
fgualda87
I'm all out of ideas then :( Sorry!
Ender
I think I know what the problems is. Because I'm using grails and it creates a list of objects, and all the elements in the list are textfields so I had two with the same name, the creating one, and the first one on the list. Lemme see if its that. Thanks!
fgualda87
OK :D!!!! I got it to work, but it doesn't work on the first one. I think is a conflict with the IDs.
fgualda87
+1  A: 

jQuery has an Autocomplete widget That can get it to do what you want it to do. here's an example: jQuery:

var data = "Core Selectors Attributes Traversing Manipulation CSS Events Effects Ajax".split(" ");
$("#commodity").autocomplete(data);

html:

<input type="text" id="commodity" name="commodity">
GSto
Do I need to install jQuery-UI plugin for Grails, or it comes already with the jQuery plugin?
fgualda87
This is seperate from jQueryUI, and doesn't require any further installation besides the plug in itself. that's why I included a different answer from @Ender
GSto
For some reason is throwing an error :/
fgualda87
The plug-in is deprecated. The jQueryUI is the successor of this plug-in.
Ender