How to dynamicly add SQLFORM.grid using JQuery

82 views
Skip to first unread message

Tu Duong

unread,
Jan 15, 2018, 10:16:38 AM1/15/18
to web2py-users
Hi,

I want to display the table's content when user clicks the button, here is my code and it does not work. I can display the SQLFORM, but for some reason, it does not work with SQLFORM.grid

--default.py
def index():
   
return dict( list_program       = SQLFORM.grid(db.Program) )




--index.html
<script>
jQuery
(document).ready(function(){
   jQuery
('#btn_list_program').click(function(){   jQuery('#selected_form').html( '{{=list_program}}' ) });
</script>


<div class="container-fluid">
   
<div class="row">
       
<div class="col col-sm-2">
             
<div class="btn-group-vertical" role="group" style="width:100%">
                   
<button id="btn_list_program"   type="button" class="btn btn-outline-primary">ListProgram</button>
             
</div>
       
</div>
       
<div class="col offset-sm-1" id="form_wrapper">
           
<div id="selected_form"></div>
       
</div>
   
</div>
</div>

Anthony

unread,
Jan 15, 2018, 11:27:55 AM1/15/18
to web2py-users
You say it works with a SQLFORM but not a grid -- what happens when you try with a grid?

Tu Duong

unread,
Jan 15, 2018, 12:25:20 PM1/15/18
to web...@googlegroups.com
It shows part of the HTML code on the browser. Seems like the code structure behind the grid is different than the form

Anthony

unread,
Jan 15, 2018, 9:01:55 PM1/15/18
to web2py-users
Why don't you just put the grid right where you want it in the HTML but hidden, and then when the button is clicked, just use jQuery to un-hide the div?
Message has been deleted

Tu Duong

unread,
Jan 16, 2018, 1:01:54 AM1/16/18
to web2py-users
I have more than 1 grid on the same page (about 10). I could do it, but it is not a best solution because I need to keep track of which gird is visible, hide that grid, and show the new selected one. I hope there is a better way to solve this... wondering why it does not work. It works beautiful with form

Anthony

unread,
Jan 16, 2018, 10:35:52 AM1/16/18
to web2py-users
On Tuesday, January 16, 2018 at 1:01:54 AM UTC-5, Tu Duong wrote:
I have more than 1 grid on the same page (about 10). I could do it, but it is not a best solution because I need to keep track of which gird is visible, hide that grid, and show the new selected one. I hope there is a better way to solve this... wondering why it does not work. It works beautiful with form

The grid HTML includes some single quotes, so you cannot wrap it in single quotes in your Javascript code, as you have done. Depending on which browsers you need to support, you can instead put the grid HTML inside a Javascript template literal. Alternatively, put each grid in a hidden div, and simply copy a given grid to the visible div whenever you want to show it:

jQuery('#visible_div').html(jQuery('#hidden_div_1').html());

Also, rather than building all the grids when the page is first requested, you might consider loading each grid asynchronously via Ajax only when needed. For example, clicking a button could call $.web2py.component() to retrieve a given grid as a web2py Ajax component.

Anthony

Tu Duong

unread,
Jan 16, 2018, 12:08:59 PM1/16/18
to web...@googlegroups.com
Thanks

1. "you can instead put the grid HTML inside a Javascript template literal"
You mean like this? 

jQuery('#btn_list_program').click(function(){   jQuery('#selected_form').html( `{{=list_program}}` ) });

Unfortunately, this does not work

2. Yes, the jQuery('#hidden_div_1').html()  approach works. 

3. "Also, rather than building all the grids when the page is first requested, you might consider loading each grid asynchronously via Ajax only when needed. For example, clicking a button could call $.web2py.component() to retrieve a given grid as a web2py Ajax component."

I don't know Ajax, could you please give me an example based on my code? Thanks

Dave S

unread,
Jan 16, 2018, 3:13:58 PM1/16/18
to web2py-users


On Tuesday, January 16, 2018 at 9:08:59 AM UTC-8, Tu Duong wrote:
[...]

3. "Also, rather than building all the grids when the page is first requested, you might consider loading each grid asynchronously via Ajax only when needed. For example, clicking a button could call $.web2py.component() to retrieve a given grid as a web2py Ajax component."

I don't know Ajax, could you please give me an example based on my code? Thanks

The place to start is the web2py book, because there are some useful helpers ... Anthony  mentioned component() which is discussed in this chapter:

<URL:http://web2py.com/books/default/chapter/29/12/components-and-plugins#Components--LOAD-and-Ajax>

/dps

Anthony

unread,
Jan 16, 2018, 3:26:55 PM1/16/18
to web2py-users
On Tuesday, January 16, 2018 at 12:08:59 PM UTC-5, Tu Duong wrote:
Thanks

1. "you can instead put the grid HTML inside a Javascript template literal"
You mean like this? 

jQuery('#btn_list_program').click(function(){   jQuery('#selected_form').html( `{{=list_program}}` ) });

Unfortunately, this does not work

Yeah, I forgot the grid also includes a script tag, so this isn't the best approach.
 
Anthony
Reply all
Reply to author
Forward
0 new messages