If you benefit from web2py hope you feel encouraged to pay it forward by contributing back to society in whatever form you choose!

This is an extension of ochiba77's slice here.  The issue I had is that the data that contained the fields for the slice were available to me via a web service call, not in a database.  I modified his code as follows.  The view feels a little hacky...  but I've never done any web development, and it works, so it's good enough for a prototype.  

Here's the controller...  I had a main category of two segments...  when one of those is chosen, I needed to get the materials corresponding to the chosen segment via a web service call.  getMats is the function for that.

def shouldcost4():  
    segments = ['Castings', 'Forgings'] #category = segment
    if request.vars.segment_name:
        materials = getMats(segments[int(request.vars.segment_name)])
    else:
        materials = getMats(segments[0])
    if request.vars.material_name:  #maker == material
        ret_string = request.vars.segment_name + ' | ' + request.vars.material_name
    else:
        ret_string = 'a'   
    return dict(segments=segments, materials=materials, ret_string=ret_string)

Here's my A_Widgets.py file with the getMats function...

import uuid
import datetime
from httplib2 import Http
from urllib import urlencode
import json

import json, urllib2
def getMats(segment):
    u = urllib2.urlopen('URL_GOES_HERE?Segment='+segment)        
    j = json.load(u)['results']
    mats = [str(j[a]['MATERIALSPEC']) for a in range(len(j))]      
    return(mats)

 

And my view...

{{extend 'layout.html'}}
{{segments=segments}}
{{materials=materials}}


<form enctype="multipart/form-data" action="{{URL()}}" method="post">
    <label for="part_number">Part Number:</label>
    <input type="text" name="part_number" value={{=request.vars.part_number}}><div />
    <label for="segment_name">Segment Name:</label>
    <select name='segment_name' 
        onchange="jQuery(material_name).remove();jQuery(matlabel).remove();
        ajax('material', ['segment_name'], 'shadow_clone');">             
        {{for i in range(len(segments)):}}
            <option value="{{=i}}" 
                {{=" selected='selected'" if str(i)==request.vars.segment_name else ""}}>
            {{=segments[i]}}
            </option>
        {{pass}}
    </select> <div />
    <span id='shadow_clone'></span>
    <label id="matlabel" for="material_name">Material:</label>
    <select name='material_name' >
        {{for i in range(len(materials)):}}
            <option value="{{=i}}" 
                {{=XML(" selected='selected'") if str(i)==request.vars.material_name else ""}}>
            {{=materials[i]}}</option>
        {{pass}}
    </select><div />
    <label for="EAU">Estimated Annual Usage:</label>
    <input type="text" name="EAU" value={{=request.vars.EAU}}><div />
    
    <input type="submit">  
</form>

<div />

And here's what it looks like...

That's it.  

cn

Related slices

Comments (0)


Hosting graciously provided by:
Python Anywhere