Append fields within form, iteratively add fields inside form, List:string append, custom forms

119 views
Skip to first unread message

Ron Chatterjee

unread,
May 27, 2016, 5:22:16 PM5/27/16
to web2py-users
Running into some issues and hoping community help out with thoughts. How do I append to list:string and using form inside a form (custom form). 

Question: How do I append skill_and_experience  as a list:string into the field added_term as 'English': '1 year', 'Math':'2 years',...etc. 

May be there is a better way to do this, but this is how I started out with. 


Model.py:

db.define_table( 'Experience',Field('Experience_level'), format = '%(Experience_level)s')
db.define_table( 'teaching',Field('teach_string'), format = '%(teach_string)s')


db.define_table( 'add_list_item',
                Field('skill_list', db.teaching, label='Enter Your Skills sets', comment = 'Enter skills'),
                Field('Experience_list', db.Experience, label='Enter Your Experience',  comment = 'Enter years'))
                

db.define_table( 'Project', 
                Field("Title", "string", requires=IS_NOT_EMPTY(),default=None),
                Field("added_term", "list:string", requires=IS_NOT_EMPTY()),)



Controller:
def create_table():
    a_list = 'My skills and experience:'
    skill_and_experience = [];
 
    form1 = SQLFORM(db.Project)
    form2 = SQLFORM(db.add_list_item, submit_button=' + Add',).process()
    form2.element('input[type=submit]').update(_class='btn btn-custom')
    
    if form2.accepted:
        a = db(db.add_list_item.id == form2.vars.id).select().first()
        skills = db(db.teaching.id == a.skill_list).select().first()
        experience = db(db.Experience.id == a.Experience_list).select().first()
        skill_and_experience = skills.teach_string+':' +''+ experience.Experience_level
        added_term.append(skill_and_experience)
        response.flash = T("Added Experience")
    
    if form1.accepted:
        response.flash = T("success!")
        
    return dict(form1 = form1, form2 = form2, a_list = a_list)


View:

{{extend 'layout.html'}}

<center>

{{=form1.custom.begin}}


<div class="input-title">
    {{=form1.custom.widget.Title}}
     <br>
    
{{=form2.custom.begin}}    

<h4>{{=a_list}}</h4>



<table>
<thead>
  <tr>
     <div class="skill_list"> 
 
  <td> {{=form2.custom.widget.skill_list}}   </td>
          <div class="exp_list"> 
    <td>{{=form2.custom.widget.Experience_list}}   </td>    
              
                <div class="add_button"> 
              <td> {{=form2.custom.submit}} </td>   
      
</tr>
</thead>
</table> 
<br>
<br>
{{=form2.custom.end}}
      
 {{=form1.custom.submit}}
    
    
        </div> 
      </div>
      </div>  
   </div> 
{{=form1.custom.end}}
</center>

<style>
.input-title{
    height: 100px;
    width: 400px;
}

#add_list_item_skill_list{
     width: 200px;
#add_list_item_Experience_list{
     width: 200px;


.btn-custom { text-align:right;color: blue;width:50px; padding:20%;position: relative;top: -4px;left: 4px;height 20px;}   
</style>



The idea is to basically add experience and training using form2 and add that to form1 and then process both. But I am having problem in appending data into list:string like I would for project. 

Project.idProject.Title2Project.added_term2
1test1test1
2test2test1, test2, test3
3test3test23, test32

Ron Chatterjee

unread,
May 30, 2016, 12:32:48 PM5/30/16
to web2py-users
Attached is the w2p file if someone is interested in looking into it. Trying to figure out how to get the form1 to work and insert values into the field.
web2py.app.custom_form.w2p

Ron Chatterjee

unread,
May 30, 2016, 12:47:24 PM5/30/16
to web2py-users
If I do this inside the action "create_table"

 db.Project.insert( added_term = ('English: 0 to 6 years', 'Math: 0 to 2 years', 'History', 'over 5 years','science: never tought'))

It works. But how to save a list like this inside the controller every time someone hits "add" button? I am wondering if I have to save the variable in a temp table and read it. Because every time I hit the "create_table" action, all the variable gets reinitialize.

Calling for Anthony with all his knowledge to help me with this! lol.

Happy memorial day everyone.

Cheers,



On Friday, May 27, 2016 at 5:22:16 PM UTC-4, Ron Chatterjee wrote:

Val K

unread,
May 31, 2016, 4:38:49 PM5/31/16
to web2py-users
Hi!
Try this

controller:

def create_table():
    a_list = 'My skills and experience:'
    form1 = SQLFORM(db.Project, hidden=dict(add_term_f1=''))
    form2 = SQLFORM(db.add_list_item, submit_button=' + Add', hidden=dict(add_term_f2=''))
    request.post_vars.added_term = request.vars.add_term_f1  and  request.vars.add_term_f1.split(',')
    form1.process()
    form1.element('input[type=submit]')['_id']='f1_submit'
    form1.element('input[type=submit]')['_style']='display:none'
    if not form1.accepted:
        form2.process()
        form2.element('input[type=submit]').update(_class='btn btn-custom')
    lst=''
    if form2.accepted:
        a = db(db.add_list_item.id == form2.vars.id).select().first()
        skills = db(db.teaching.id == a.skill_list.id).select().first()
        experience = db(db.Experience.id == a.Experience_list.id).select().first()
        skill_and_experience = skills.teach_string+':' +''+ experience.Experience_level
        lst = request.vars.add_term_f2 and request.vars.add_term_f2.split(',') or []
        lst.append(skill_and_experience)
        lst_str =   ','.join(lst)
        form2.custom.end.element('[name=add_term_f2]')['_value'] = lst_str
        form1.custom.end.element('[name=add_term_f1]')['_value'] = lst_str
        response.flash = T("Added Experience")
    if form1.accepted:
        response.flash = T("success!")
        
    return dict(form1 = form1, form2 = form2, a_list = a_list, lst=lst)



view:

{{extend 'layout.html'}}
{{from gluon.html import * }}

<center>

{{=form1.custom.begin}}


<div class="input-title">
    {{=form1.custom.widget.Title}}
     <br>
{{=form1.custom.end}}    
{{=form1.custom.submit}}
    
{{=form2.custom.begin}}    

<h4>{{=a_list}}</h4>
{{=lst}}


<table>
<thead>
  <tr>
     <div class="skill_list"> 
 
  <td> {{=form2.custom.widget.skill_list}}   </td>
          <div class="exp_list"> 
    <td>{{=form2.custom.widget.Experience_list}}   </td>    
              
                <div class="add_button"> 
              <td> {{=form2.custom.submit}} </td>   
      
</tr>
</thead>
</table> 
<br>
<br>
{{=form2.custom.end}}
      
{{=BUTTON('Submit', _class='btn', _type='button', _onclick="$('#f1_submit').click()" )}} 
    
    
        </div> 
      </div>
      </div>  
   </div> 

</center>

<style>
.input-title{
    height: 100px;
    width: 400px;
}

#add_list_item_skill_list{
     width: 200px;
#add_list_item_Experience_list{
     width: 200px;


.btn-custom { text-align:right;color: blue;width:50px; padding:20%;position: relative;top: -4px;left: 4px;height 20px;}   
</style>

Mark Graves

unread,
Jun 2, 2016, 8:32:33 PM6/2/16
to web2py-users
Hey Ron,

This looks like an architectural issue to me.

You have to maintain state somehow for the form2 vars.

You can either do this via javascript on the client side, then have only one form server side, which has a dynamic set of fields.

If i were to do it this way, I would use a javascript library similar to https://github.com/marioizquierdo/jquery.serializeJSON

I have used this in the past, and then set up the controller much more simply to handle storing and retrieving JSON.

If you need to do row by row processing, you can filter out the JSON and store it in rows server side,

Otherwise, you can store it server side, let me know if you want to chat about this offline.

-Mark
Reply all
Reply to author
Forward
0 new messages