Passing arg from view to controller.

110 views
Skip to first unread message

Annet

unread,
Feb 20, 2012, 2:09:33 AM2/20/12
to web2py-users
In this post: http://groups.google.com/group/web2py/browse_thread/thread/9886ecbead18a13a/a9ab71c234f0afbb#a9ab71c234f0afbb,
my problem was almost solved, I am left with an args
problem.

I am using the jQuery UI autocomplete. In the controller I have this
function:

def locality_args_autocomplete():

rows=db((db.NodeKeyword.word==request.args(0))&(db.NodeKeyword.nodeID==db.Address.nodeID)&
\
(db.Address.locality.like(request.vars.term+'%')))\
.select(db.Address.locality,distinct=True,orderby=db.Address.locality).as_list()
result=[r['locality']for r in rows]
return response.json(result)

... as the source of this jQuery function in the view:

$(function() {
$("#no_table_locality_args").autocomplete({
source:
"{{=URL('hubaddressbook','locality_args_autocomplete',args='javascript:
$(select#no_table_word).val();')}}",
minLength: 2
});

This doesn't work. #no_table_word references the id of a multiselect
field, which I would like to pass to the function creating the result
list for the autocomplete. Is there a way to get this to work?

Kind regards,

Annet.

Anthony

unread,
Feb 20, 2012, 7:35:26 AM2/20/12
to web...@googlegroups.com
$(function() {
  $("#no_table_locality_args").autocomplete({
    source:
"{{=URL('hubaddressbook','locality_args_autocomplete',args='javascript:
$(select#no_table_word).val();')}}",

You have to put your javascript code in the javascript portion of the above, not the Python portion (Python doesn't know what to do with the javascript -- the URL function ends up simply URL encoding it and adding it to the URL). Instead, probably something like:

$(function() { 
  $("#no_table_locality_args").autocomplete({ 
    source: "{{=URL('hubaddressbook', 'locality_args_autocomplete')}}" + "/" + $(select#no_table_word).val(),


Anthony
Message has been deleted
Message has been deleted

Annet

unread,
Feb 21, 2012, 3:04:23 AM2/21/12
to web2py-users
Hi Anthony,

Thanks for your reply.

> $(function() {
> $("#no_table_locality_args").autocomplete({
> source: "{{=URL('hubaddressbook', 'locality_args_autocomplete')}}" +
> "/" + $(select#no_table_word).val(),

I tried the above, which made the view's JavaScript invalid, I had to
add '' to make it valid JavaScript:

$('select#no_table_word').val()

Testing this, results in the following url:

.../init/hubaddressbook/locality_args_autocomplete/?term=Am


Adding option:selected: $('select#no_table_word
option:selected').val() as in http://api.jquery.com/val/ doesn't make
any difference.


When I hard code the .val() part with "Coach":

$(function() {
$("#no_table_locality_args").autocomplete({
source: "{{=URL('hubaddressbook', 'locality_args_autocomplete')}}"
+ "/" + "Coach",
minLength: 2
});

It all works. The url:

.../init/hubaddressbook/locality_args_autocomplete/Coach?term=Am


Removing the the jQuery UI MultiSelect from the select doesn't make
any difference.



Kind regards,

Annet

Alan Etkin

unread,
Feb 21, 2012, 6:33:56 AM2/21/12
to web2py-users
> I tried the above, which made the view's JavaScript invalid, I had to
> add '' to make it valid JavaScript:

> $('select#no_table_word').val()

> Testing this, results in the following url:

> .../init/hubaddressbook/locality_args_autocomplete/?term=Am

So jQuery doesn't find data in 'select#no_table_word' (it seems to be
returning an empty string). I assume that "Coach" can be any term to
be sent for the automplete feature. Check if the selector is named
correctly. You could add extra javascript to debug (for example send
the .val() output to a popup alert() window). Does that javascript
section start when a user types data in? (check wether the function is
called after the event)

Annet

unread,
Feb 22, 2012, 2:10:44 AM2/22/12
to web2py-users
Hi Alan,

Thanks for your reply>

I tried this:

<script type="text/javascript">
$(document).ready(function(){
$("#no_table_word").multiselect({
multiple: false,
header: "Close single select",
noneSelectedText: "Select a value",
selectedList: 1
});

$('#no_table_word').change(function(){
var singleValue = $("#no_table_word").val();
alert(singleValue);
});

});

$(function() {
$("#no_table_locality_args").autocomplete({
source: "{{=URL('hubaddressbook', 'locality_args_autocomplete')}}"
+ "/" + $("#no_table_word").val(),
minLength: 2
});
});
</script>


When I select an option from the multiselect an alert box shows the
selected value. However, when I start typing a locality in the
autocomplete field, in Firefox's Net/XHR tab, the URL still doesn't
contain the value from the select:

.../init/hubaddressbook/locality_args_autocomplete/?term=Am

I hope I provided you with sufficient information to help me solve the
problem.


Best regards,

Annet.

Anthony

unread,
Feb 22, 2012, 10:05:33 AM2/22/12
to web...@googlegroups.com
I think the problem is that the "source" attribute is set only once, on document ready, before any values are selected in the multiselect. Instead, maybe you can update the source attribute when the multiselect changes:

var base = "{{=URL('hubaddressbook', 'locality_args_autocomplete')}}"
$('#no_table_word').change(function() {
    var url = base + "/" + this.val();
    $("#no_table_locality_args").autocomplete('option', 'source', url);
});

Since #no_table_word is a multiselect, what happens when multiple items are selected -- what is the value of val() in that case (is it a legal web2py arg)?

Anthony

Anthony

unread,
Feb 22, 2012, 10:06:11 AM2/22/12
to web...@googlegroups.com
I think the problem is that the "source" attribute is set only once, on document ready, before any values are selected in the multiselect. Instead, maybe you can update the source attribute when the multiselect changes:

var base = "{{=URL('hubaddressbook', 'locality_args_autocomplete')}}"
$('#no_table_word').change(function() {
    var url = base + "/" + this.val();
    $("#no_table_locality_args").autocomplete('option', 'source', url);
});

Since #no_table_word is a multiselect, what happens when multiple items are selected -- what is the value of val() in that case (is it a legal web2py arg)?

Anthony

On Wednesday, February 22, 2012 2:10:44 AM UTC-5, Annet wrote:

Annet

unread,
Feb 23, 2012, 5:01:12 AM2/23/12
to web2py-users
Hi Anthony,

Thanks for you reply. I'll give your solution a try, and let you know
if it solved my problem.


Best regards,


Annet
Reply all
Reply to author
Forward
0 new messages