get data from tabel and send to view

105 views
Skip to first unread message

Gert V

unread,
Mar 13, 2013, 11:42:30 AM3/13/13
to pylons-...@googlegroups.com
We use pyramid and chameleon template.

Currently we have a page where users can fill in a table.
This table has a variable lenght because rows are added dynamically.
We want to get all the content of the table and put it in a list, so we send it to our database.

The biggest problem is that we don't know how to send the data to a list and get it to our vieuw.
can some1 help me with this problem?
thanks in advance.

Vincent Catalano

unread,
Mar 13, 2013, 12:28:29 PM3/13/13
to pylons-...@googlegroups.com
Can you be a bit more specific with what you are attempting to do? Have you created a form with input fields throughout this table? Are you posting the form data to your pyramid application?

-Vincent




--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pylons-discus...@googlegroups.com.
To post to this group, send email to pylons-...@googlegroups.com.
Visit this group at http://groups.google.com/group/pylons-discuss?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Vincent Catalano
Software Engineer and Web Ninja,
(520).603.8944

Gert V

unread,
Mar 13, 2013, 2:43:45 PM3/13/13
to pylons-...@googlegroups.com
Thanks for the fast reply. sorry i wasn't clearer on my previous post. If annything is still not documentated enough please let me know.
i'll give some code snippets of my template. no data is loaded into this page. the user has to fill in a few fields and after that we want to store it to our database.

<div id="diagnoses_zelf" class="span5 well">
            <form >   
            <h5>Diagnoses</h5>
                <table id="tableDiagnose" class="table table-hover">
                        <thead class="header">
                            <tr>
                                <th>Nr.</th>
                                <th>Code</th>
                                <th>Uitleg</th>
                                <th>Zek</th>
                                <th>Aan</th>
                            </tr>
                        </thead>
                        <tr>
                            <td>A</td>
                            <td><input type='text' id="dz_code" class="input-mini" onFocus="isleegveld(this.value)" onchange="voegrijtoe_Diagnose(this.value)" placeholder="Code"></input></td>
                            <td style="max-width: 140px; word-wrap: break-word">some random text</td>
                            <td><input type='text' id="dz_zek" class="input-mini1" ></input></td>
                            <td><input type='text' id="dz_aan" class="input-mini1" ></input></td>
                        </tr>                       
                    </table>
            </form>
        </div>       

every time the user has filled in the dz_code textfield, annother textfield will appear beneeth it. (using javascript)
I'd like to get all the data of this table into a list. each row is a medical file of a patient and should be placed in a table "medical files".
To bring it to our database I understood I need to bring this list to a view in our views.py so we can send it to the database.

Vincent Catalano

unread,
Mar 13, 2013, 5:57:18 PM3/13/13
to pylons-...@googlegroups.com
I would recommend spending some more time reading the Pyramid documentation, especially how Pyramid handles form posts with View Callables: http://docs.pylonsproject.org/projects/pyramid/en/1.4-branch/narr/views.html#handling-form-submissions-in-view-callables-unicode-and-character-set-issues.

-Vincent

cropr

unread,
Mar 14, 2013, 4:07:22 AM3/14/13
to pylons-...@googlegroups.com
Gert,

What you could do is the following.  Make on the client in javascript a datastructure (probably a list), that has all the relevant data from the table.   Do a JSON.stringify() on a the data structure and send the resulting string to the webserver either using a hidden field in a form, either using an AJAX call.  On the webserver it is very easy to decode the string back to the datastructure:  in PHP use json_decode, in Node.js use JOSN.parse, in Python use json.loads ...

Ruben Decrop

Gert V

unread,
Mar 15, 2013, 10:03:23 AM3/15/13
to pylons-...@googlegroups.com
Thx for all previous reactions.
We got all our tabledata into a json. It looks like this:

    function neeminhoud()
    {
        var headers = [];
        var InputsArray = [];
        var Data = [];
       
        var oTable = document.getElementById('tableDiagnose');
        var rijen = oTable.rows.length;
        //gets table
        //loops through rows
           var oCells = oTable.rows.item(0).cells;
           //gets cells of current row
           var cellLength = oCells.length;
               for(var j = 0; j < cellLength; j++)
               {
                   //loops through each cell in current row
                      <!--get your cell info here-->
                      //var cellVal = oCells.children[j].value;
                    if(j != 2)
                    {
                        if(j > 2)
                        {
                            headers[j] =oCells.item(j).innerHTML;
                            var cellVal = oCells.item(j).innerHTML;
                            //alert(cellVal);
                        }
                        else
                        {
                            headers[j-1] =oCells.item(j).innerHTML;
                            var cellVal = oCells.item(j).innerHTML;
                            //alert(cellVal);
                        }
                    } 
               }
               for (i = 1; i < rijen; i++)
               {              
                    var inputs = oTable.rows.item(i).getElementsByTagName("input");
                    var inputslengte = inputs.length;
                    var rijData = {};
                    for(var j = 0; j < inputslengte; j++)
                    {
                        //alert(inputs.length);
                        var inputval = inputs[j].value;
                        rijData[headers[j]] = inputval;
                       
                        alert(inputval);
                    }
                    Data.push(rijData);
                    alert(Data);
               }
    }

Now we'd like to send it to our view using AJAX so we can send it to our model/database.
How can we do this, cause we can't seem to find the documentation for this.
Message has been deleted

Gert V

unread,
Mar 16, 2013, 12:09:34 PM3/16/13
to pylons-...@googlegroups.com
We managed to set up a ajax request with our data inside.
We get the succes message, but we don't know how to get the json out of our ajax request.

We added these lines in our views.py

def get_items(request):
    return {request} 

@view_config(route_name='
diagnose', xhr=True, renderer='json')
def r_ajax(request):
    items = get_items(request)
    logger = logging.getLogger("random")
    logger.warning(json.dumps(items))


our ajax post looks like this:
                  $.ajax({
                      type: "POST",
                      url: "/diagnose",
                      dataType: "json",
                      data: Data,
                      success: function (msg) {
                        alert("succes: " + msg);
                      },
                      error: function(){
                          alert(msg + 'tet');
                          }                     
                   });

how can we get the json out of our ajax request?


On Wednesday, March 13, 2013 4:42:30 PM UTC+1, Gert V wrote:

Malthe Borch

unread,
Mar 16, 2013, 4:12:45 PM3/16/13
to pylons-...@googlegroups.com
This is related to jQuery, not the Python framework that you're using.

Note that the `success` function receives more arguments than just the message.

\malthe
> --
> You received this message because you are subscribed to the Google Groups
> "pylons-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to pylons-discus...@googlegroups.com.
> To post to this group, send email to pylons-...@googlegroups.com.
> Visit this group at http://groups.google.com/group/pylons-discuss?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>



--
Au revoir, et tous mes voeux pour un avenir plein de succès et de bonheur ––

Malthe Borch
mbo...@gmail.com
Reply all
Reply to author
Forward
0 new messages