0

Complete newbie to web2Py.... I have a web page that has a dropdown menu and I want to perform certain queries based on the value selected.

Reports.html

<h2>Reports</h2>

<p>Please select a report template or choose to write your own.</p>

<select id="ReportSelect" onchange="ReportSelectionChange()">
    <option disabled selected value="0"></option>
    <option value="1">option 1</option>
    <option value="2">option 2</option>
</select>

<script language="javascript" type="text/javascript">
function ReportSelectionChange()
{
    var menuItem = document.getElementById("ReportSelect").value;

    switch(menuItem)
    {
        case "1":
                {{=myform}}
                break;
        case "2":
        default:
                alert(menuItem);
                break;
    }
}

controller python file

def reports():
user_id = request.args(0)
tuple = db(/*My query here*/).select()

myform = FORM('/*not sure what goes here*/')

return dict(tuple=tuple, myform=myform)

I want to pass back all the results from the query to the calling page and have the results appear in a table.

Q: How can I do this? what needs to get passed back? can this even be accomplished with javascript?

2 Answers 2

1

You are mixing HTML generated by web2py with javascript in a way that will not work. Remember that javascript is executed client side and python is server side.

In this case I suggest you start with trapped links: http://www.web2py.com/books/default/chapter/29/12/components-and-plugins#Trapped-Ajax-links-and-the-A-Helper

Which will work well for your usage of either browsing or creating. Later you can revisit this page and make it better.

Sign up to request clarification or add additional context in comments.

Comments

1

Use ajax function, the ajax function will be executed in some event ( this case onclick of the select item , then will run some method in your controller, this method in controller will return the data from db and the ajax function have parameter that means the target that result will be appended .

1.Use the ajax function on option element indicating that will executed on onclick event. 2. In method in your controller make the search on database and return the result. 3. Set the target of ajax function to some element in your html ( table , li's ...)

1.

<select id="ReportSelect" name="myoption">
    <option disabled selected value="0"></option>
    <option value="1" onclick="ajax('{{=URL('controller', 'method')}}', ['myoption'], 'your_target')" />option 1</option>
    <option value="2">option 2</option>
</select>

2.

def method():
    data = request.vars.myoption
    your_result_query = db(your_query)

    elements = UL(LI(content) for i in your_result_query)
    return dict(result=elements)
  1. In your view put a element with id with same name of target of ajax_function .

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.