[web2py] jqgrid onSelectRow trigger web2py_component

955 views
Skip to first unread message

Jason Lotz

unread,
May 17, 2010, 12:50:54 AM5/17/10
to web2py-users
I was using an iframe to show the details of a selected row from
jqgrid. Which worked. However I realized that it is a pain to get
iframe to recognize parent size so resizing produces an awkward
looking form. I came across web2py LOAD helper and thought it to be a
good fix to my frame resizing issue.

I'm trying to change a LOAD(form) based on the row_id from a selected
row in jqgrid. Using the jqgrid event onSelectRow I'm trying to
trigger web2py_component to reload the form.

I came up with a test link to update the form, which worked.

<a href="#"
onclick="web2py_component('{{=URL(...)}}','my_form');">Load Page 3</a>
{{=LOAD('default','a_form/1',ajax=True,target='my_form')}}


If possible I would like to get jqgrid to trigger the same
web2py_component feature.

My latest failed attempt was something like

jQuery(document.ready(function(){
onSelectRow: function()
{ jQuery("#my_form").trigger('web2py_component',['/Test/default/
a_form/'+(postdata),'my_form']);}
;})

Is it possible to trigger web2py_component function or LOAD helper
without an action input (button or link)?


Jay

mdipierro

unread,
May 17, 2010, 1:35:25 AM5/17/10
to web2py-users
This is the definition of web2py_component

function web2py_component(action,target)
{
jQuery(document).ready(function()
{ web2py_ajax_page('get',action,null,target); });
}

I think you just want to call web2py_ajax_page instead of
web2py_component.

Jason Lotz

unread,
May 17, 2010, 2:22:28 AM5/17/10
to web2py-users
Thanks mdipierro!!

I actually saw that in the header of the generated page but didn't put
it together. For what ever reason, trying to call web2py_component
from another function wouldn't work, but calling web2py_ajax_page
directly worked!

Working code:

jQuery("#grid_id").jqGrid({
onSelectRow: function(postdata) {
web2py_ajax_page('get','/Test/default/a_form/'+
(postdata),null,'my_form');
}
});



Jay

AsmanCom

unread,
May 20, 2010, 7:47:52 AM5/20/10
to web2py-users
Could you help me setting this up too?

This is the view:

{{extend 'layout.html'}}
<h1>This is the default/test.html template</h1>
{{=jqgrid_table}}
jQuery("#grid_id").jqGrid({
onSelectRow: function(postdata) {
web2py_ajax_page('get','/myapp/default/form/'+
(postdata),null,'my_form');
}

});

This is the Controller:

def jqgrid_table_test():
return
dict(jqgrid_table=plugin_editable_jqgrid(db.jqgrid_table,grid_name='jqgrid_table',db_name='db'))

def form():
id = request.args[0]
form=FORM(TABLE(TR("Your
name:",INPUT(_type="text",_name="name",requires=IS_NOT_EMPTY())),
TR("Your
email:",INPUT(_type="text",_name="email",requires=IS_EMAIL())),
TR("Admin",INPUT(_type="checkbox",_name="admin")),

TR("Sure?",SELECT('yes','no',_name="sure",requires=IS_IN_SET(['yes','no']))),
TR("Profile",TEXTAREA(_name="profile",value="write
something here")),
TR("",INPUT(_type="submit",_value="SUBMIT"))))
if form.accepts(request.vars,session):
response.flash="form accepted"
elif form.errors:
response.flash="form is invalid"
else:
response.flash="please fill the form"
return dict(form=form,vars=form.vars)


################################################

The JQGrid works, but underneath the Grid i only see the following
text:
jQuery("#grid_id").jqGrid({ onSelectRow: function(postdata)
{ web2py_ajax_page('get','/core/default/form/'+
(postdata),null,'my_form'); } });


It would be very nice if you could help me gettin this to work.
Thank you in advance.

Dieter Asman

Jason Lotz

unread,
May 20, 2010, 8:54:36 AM5/20/10
to web2py-users
You could try passing SCRIPT from the controller.

## View
{{extend 'layout.html'}}
<h1>This is the default/test.html template</h1>
{{=plugin_editable_jqgrid(db.jqgrid_table,grid_name='jqgrid_
table',db_name='db')}}
{{=script}}


## Controller
def jqgrid_table_test():
script = """jQuery(document).ready(function(){
jQuery("#grid_id").jqGrid({
onSelectRow: function(postdata)
{web2py_ajax_page('get','/myapp/default/form/'+
(postdata),null,'my_form'); }});"""
return SCRIPT(script)


I haven't tested this so let me know if it works out for you.

I am only using a single grid for my app so I actually included the
onSelectRow event to /models/jqgrid_plugin. The function
plugin_jqgrid() is where the default jqgrid script is retrieved when
the {{=plugin_jqgrid(db.table,columns=[...])}} is called. If you do it
this way you will want to add it to the .jqGrid({ }) parameters not in
the 'navGrid', 'toolbar' or other extended option parameters.


Jay


On May 20, 8:47 pm, AsmanCom <d.as...@web.de> wrote:
> Could you help me setting this up too?
>
> This is the view:
>
> {{extend 'layout.html'}}
> <h1>This is the default/test.html template</h1>
> {{=jqgrid_table}}
> jQuery("#grid_id").jqGrid({
>     onSelectRow: function(postdata) {
>             web2py_ajax_page('get','/myapp/default/form/'+
> (postdata),null,'my_form');
>     }
>
> });
>
> This is the Controller:
>
> def jqgrid_table_test():
>     return
> dict(jqgrid_table=plugin_editable_jqgrid(db.jqgrid_table,grid_name='jqgrid_ table',db_name='db'))

AsmanCom

unread,
May 20, 2010, 9:45:46 AM5/20/10
to web2py-users
Thanks for your effort, but it gives an empty page..
I will use two or more Grids at least one of them must keep the
"editable" behaveure.
But in this case it should work like your approach, controlling the
form.

May i missed something..?
Could you tell me how to set up the form?

should the view look something like that?:

{{=jqgrid_table}}
<script>
jQuery("#grid_id").jqGrid({
onSelectRow: function(postdata) {
web2py_ajax_page('get','/myapp/default/form/'+
(postdata),null,'my_form');
}

});
</script>
<div id='my_form'></div>
{{=form}}

Thank you in advance.

Dieter Asman

Jason Lotz

unread,
May 20, 2010, 9:52:53 AM5/20/10
to web...@googlegroups.com
Try

{{=jqgrid_table}}
<script type="text/javascript">

jQuery(document).ready(function(){
jQuery("#grid_id").jqGrid({
    onSelectRow: function(postdata) { web2py_ajax_page('get','/myapp/default/form/'+(postdata),null,'my_form');}});
</script>

AsmanCom

unread,
May 20, 2010, 10:40:51 AM5/20/10
to web2py-users
I tried like that:

View:

{{=jqgrid_table}}
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery("#grid_id").jqGrid({
onSelectRow: function(postdata) {web2py_ajax_page('get','/myapp/
default/form/'+(postdata),null,'my_form');}});
</script>

Controller:

def jqgrid_table_test():
return
dict(jqgrid_table=plugin_editable_jqgrid(db.jqgrid_table,grid_name='jqgrid_table',db_name='db'))

def form():
#id = request.args[0]
form = SQLFORM(db.jqgrid_table)
if form.accepts(request.vars, session):
response.flash = 'form accepted'
elif form.errors:
response.flash = 'form has errors'
else:
response.flash = 'please fill out the form'
return dict(form=form)

##########################################

Now the Grid is showed, but no form.

do you have any further suggestions?

Thank you in advance.

Dieter Asman

Jason Lotz

unread,
May 20, 2010, 10:49:42 AM5/20/10
to web...@googlegroups.com
What do you mean the form doesn't show?

Do you have a DIV for the form to load in your view?

{{=LOAD('default','form',args=1,ajax=True,target='my_form')}}

Does this not reload as you select a row from from the grid?

AsmanCom

unread,
May 20, 2010, 10:53:06 AM5/20/10
to web2py-users
can this be caused by the function, which set in /static/
plugin_editable_jqgrid/plugin_editable_jqgrid.js ?:

onSelectRow: function(id){
if(id && lastSel!='' && id!==lastSel){
jQuery(grid_name).restoreRow(lastSel);
}
lastSel=id;
jQuery(grid_name).editRow(id, true, '', '', '', '',
function(rowid, resultText){reload(grid_name,rowid, resultText);});
},
colNames:col_names,
colModel:col_models,
pager: pager,
rowNum:10,
rowList:[10,100,1000],
sortorder: 'desc',
multiselect: true,
multiboxonly:true,
viewrecords: true,
editurl:edit_url,
caption: caption
});

Thank you in advance.

Dieter Asman

On 20 Mai, 15:52, Jason Lotz <jayl...@gmail.com> wrote:

Jason Lotz

unread,
May 20, 2010, 10:55:39 AM5/20/10
to web...@googlegroups.com
Are you using the web2py jqgrid_plugin?

http://web2py.com/plugins/default/jqgrid

AsmanCom

unread,
May 20, 2010, 11:09:08 AM5/20/10
to web2py-users
Many thanks, it go´s in the right direction now.

I am using the http://app.ebansoftware.net/editable_jqgrid/default/show_example
, but it is quite similar.

Now the is loaded is loaded with the default template and then a
second layer covers it with the form and the default Template(loaded
twice)

The code so far:

View:

{{extend 'layout.html'}}
<h1>This is the default/test.html template</h1>
{{=jqgrid_table}}
<script type="text/javascript">
jQuery(document).ready(function()
{jQuery("#grid_id").jqGrid({onSelectRow: function(postdata)
{web2py_ajax_page('get','/myapp/default/form/'+
(postdata),null,'my_form');}});
</script>
<div id="my_form"></div>
{{=LOAD('default','form',args=1,ajax=True,target='my_form')}}

Controller:

def jqgrid_table_test():
return
dict(jqgrid_table=plugin_editable_jqgrid(db.jqgrid_table,grid_name='jqgrid_table',db_name='db'))

def form():
#id = request.args[0]
form = SQLFORM(db.jqgrid_table)
if form.accepts(request.vars, session):
response.flash = 'form accepted'
elif form.errors:
response.flash = 'form has errors'
else:
response.flash = 'please fill out the form'
return dict(form=form)

On 20 Mai, 16:55, Jason Lotz <jayl...@gmail.com> wrote:
> Are you using the web2py jqgrid_plugin?
>
> http://web2py.com/plugins/default/jqgrid
>
> On 05/20/2010 11:53 PM, AsmanCom wrote:
>
> > can this be caused by the function, which set in /static/
> > plugin_editable_jqgrid/plugin_editable_jqgrid.js ?:
>
> >     onSelectRow: function(id){
> >       if(id&&  lastSel!=''&&  id!==lastSel){

AsmanCom

unread,
May 20, 2010, 11:16:30 AM5/20/10
to web2py-users
I meant:

The Grid is loaded with the default template and then a second layer
covers it with the form and the default Template(loaded twice)

;-)

On 20 Mai, 16:55, Jason Lotz <jayl...@gmail.com> wrote:
> Are you using the web2py jqgrid_plugin?
>
> http://web2py.com/plugins/default/jqgrid
>
> On 05/20/2010 11:53 PM, AsmanCom wrote:
>
> > can this be caused by the function, which set in /static/
> > plugin_editable_jqgrid/plugin_editable_jqgrid.js ?:
>
> >     onSelectRow: function(id){
> >       if(id&&  lastSel!=''&&  id!==lastSel){

Jason Lotz

unread,
May 20, 2010, 11:23:32 AM5/20/10
to web...@googlegroups.com
web2py LOAD function will create generate <div id="target">Loading...</div> and fills your component. I'm sorry I can't test it right now but it seems that by adding the <div></div> in your view and including the LOAD you are creating two form. Maybe try remove your div and only keep the  LOAD.
{{extend 'layout.html'}}
<h1>This is the default/test.html template</h1>
{{=jqgrid_table}}
<script type="text/javascript">
jQuery(document).ready(function()
{jQuery("#grid_id").jqGrid({onSelectRow: function(postdata)
{web2py_ajax_page('get','/myapp/default/form/'+
(postdata),null,'my_form');}});
</script>
{{=LOAD('default','form',args=1,ajax=True,target='my_form')}}

check out LOAD function doc
http://www.web2py.com/AlterEgo/default/show/252

AsmanCom

unread,
May 20, 2010, 11:50:03 AM5/20/10
to web2py-users
You helped me a lot, man!

i´ve tried like you suggested, i only keeped the LOAD, but it´s the
same...
Page is loading the Grid, underneath the Grid rises "loading" and then
a new layer pops up with with the form in the default Template (Its
showed twice).

I´ve checked:
www.web2py.com/AlterEgo/default/show/252

But it doesent´t helped very much.

It seems I am stucked, right before finish... ;-(

Any help is appreciated, would so cool if I could get this done today.

Dieter Asman

On 20 Mai, 17:23, Jason Lotz <jayl...@gmail.com> wrote:
> web2py LOAD function will create generate <div
> id="target">Loading...</div> and fills your component. I'm sorry I can't
> test it right now but it seems that by adding the <div></div> in your
> view and including the LOAD you are creating two form. Maybe try remove
> your div and only keep the  LOAD.
>
> {{extend 'layout.html'}}
> <h1>This is the default/test.html template</h1>
> {{=jqgrid_table}}
> <script type="text/javascript">
> jQuery(document).ready(function()
> {jQuery("#grid_id").jqGrid({onSelectRow: function(postdata)
> {web2py_ajax_page('get','/myapp/default/form/'+
> (postdata),null,'my_form');}});
> </script>
> {{=LOAD('default','form',args=1,ajax=True,target='my_form')}}
>
> check out LOAD function dochttp://www.web2py.com/AlterEgo/default/show/252
>
> On 05/21/2010 12:09 AM, AsmanCom wrote:
>
> > Many thanks, it go�s in the right direction now.
>
> > I am using thehttp://app.ebansoftware.net/editable_jqgrid/default/show_example
> ...
>
> Erfahren Sie mehr »

Jason Lotz

unread,
May 20, 2010, 12:43:37 PM5/20/10
to web...@googlegroups.com
Can you explain a liitle more? You are actually seeing two forms in
the browser?

If I understand you correctly upon first loading the page you get the
grid and a form with default id. When you select a row in the grid the
default form is not reloaded with the new id but a second form
appears. You see a grid and two forms. Is that right?

Double check your id's. It could be that adding the extra jQuery
("#grid_id").jqGrid({ onSelectRow: .... }) in the view is conflicting
with a function in the plugin. I would try making a quick test using
the web2py jqgrid_plugin (which is a very nicely integrated plugin )
and see if you get the same issue. If not then it is a conflict with
your plugin script.

It's passing 1:45am here so I will take a look at this editable_plugin
tomorrow morning and see if I can find something, if you haven't
solved it by then.
>>> dict(jqgrid_table=plugin_editable_jqgrid
>>>>>> web2py_ajax_page('get','/myapp/default/form/'+
>>>>>> (postdata),null,'my_form');}});
>>>>>> </script>
>>

Jason Lotz

unread,
May 21, 2010, 12:18:29 AM5/21/10
to web2py-users
Alright now I see a little better what is going. Last night I wasn't
able to check what the jqgrid_editable_plugin really was. To my
surprise it is a very nice web2py plugin. My appologise for pushing
the jqgrid_plugin from web2py/plugins. Although a nice plugin this is
a very feature rich ready plugin.

Anyways, I slightly revised the plugin so that this will work for you.
I will mail it to your private mail.

Once you have replaced the new plugin files you should be able to use
it by implementing the following code.

## controller
def index():
return
dict(jqgrid_table=plugin_editable_jqgrid(db.test,grid_name='grid_test',db_name='db',grid_load=URL(r=request,c='default',f='form'),target='my_form')))

def form():
id = request.args[0]
form=FORM(TABLE(TR("Your
name:",INPUT(_type="text",_name="name",requires=IS_NOT_EMPTY())),
TR("Your
email:",INPUT(_type="text",_name="email",requires=IS_EMAIL())),

TR("Admin",INPUT(_type="checkbox",_name="admin")),
TR("Sure?",SELECT('yes','no',_name="sure",requires=IS_IN_SET(['yes','no'])) ),

TR("Profile",TEXTAREA(_name="profile",value="write
something here")),
TR("",INPUT(_type="submit",_value="SUBMIT"))))
if form.accepts(request.vars,session):
response.flash="form accepted"
elif form.errors:
response.flash="form is invalid"
else:
response.flash="please fill the form"
return dict(form=form,vars=form.vars)


## view
{{extend 'layout.html'}}
<h1>This is the default/test.html template</h1>
{{=jqgrid_table}}
{{=LOAD('default','details',args=1,ajax=True,target='my_form')}}


// Notice the two extra options (grid_load='URL(...)',target='id') in
the controller default/index.
grid_load = url to the form controller function
target = This should match the LOAD(target='id')

Hope this works for you.. :)


On May 21, 1:43 am, Jason Lotz <jayl...@gmail.com> wrote:
> Can you explain a liitle more? You are actually seeing two forms in  
> the browser?
>
> If I understand you correctly upon first loading the page you get the  
> grid and a form with default id. When you select a row in the grid the  
> default form is not reloaded with the new id but a second form  
> appears. You see a grid and two forms. Is that right?
>
> Double check your id's. It could be that adding the extra jQuery
> ("#grid_id").jqGrid({ onSelectRow: .... }) in the view is conflicting  
> with a function in the plugin. I would try making a quick test using  
> the web2py jqgrid_plugin (which is a very nicely integrated plugin )  
> and see if you get the same issue. If not then it is a conflict with  
> your plugin script.
>
> It's passing 1:45am here so I will take a look at this editable_plugin  
> tomorrow morning and see if I can find something, if you haven't  
> solved it by then.
>
> ...
>
> read more »
Reply all
Reply to author
Forward
0 new messages