1

I'm creating a webproject in pyramid where I'd like to update a table every few secondes. I already decided to use ajax, but I'm stuck on something.

On the client side I'm using the following code:

    function update()
    {
    var variable = 'variable ';
    $.ajax({
        type: "POST",
        url: "/diagnose_voorstel_get_data/${DosierID}",
        dataType: "text",
        data: variable ,
        success: function (msg) {
        alert(JSON.stringify(msg));           
        },
        error: function(){
            alert(msg + 'error');
          }                      
        });
    }

Pyramid side:

@view_config(route_name='diagnose_voorstel_get_data', xhr=True, renderer='string')    
def diagnose_voorstel_get_data(request):
    dosierid = request.matchdict['dosierid']
    dosieridsplit = dosierid.split     
    Diagnoses = DBSession.query(Diagnose).filter(and_(Diagnose.code_arg == str(dosieridsplit[0]), Diagnose.year_registr == str(dosieridsplit[1]), Diagnose.period_registr == str(dosieridsplit[2]), Diagnose.staynum == str(dosieridsplit[3]), Diagnose.order_spec == str(dosieridsplit[4])))       
    return {'Diagnoses ' : Diagnoses }

Now I want to put this data inside a table with zpt using the tal:repeat statement. I know how to use put this data in the table when the page loads, but I don't know how to combine this with ajax.

Can anny1 help me with this problem ? thanks in adance.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
GertV
  • 831
  • 2
  • 10
  • 23
  • 1
    AJAX is a browser-side technology. You'd build the table with JavaScript, **not** with ZPT. ZPT runs on the server side and is already done by the time the browser loads the result. – Martijn Pieters May 14 '13 at 16:53
  • so there is no possibility to reload the page with new data using ajax calls ? Yeah I tought so, but I was just checking with the pro's. Thx for the answer. – GertV May 14 '13 at 19:06
  • You can reload the page with AJAX too, and use ZPT to build that response server-side. But there is nothing special about this compared to other responses. – Martijn Pieters May 14 '13 at 19:12
  • I figured that would be the only way to do it besides using javascript etc. Thanks for your time Martijn, and as always you were a great help :) – GertV May 14 '13 at 19:15

1 Answers1

1

You can do just about anything with AJAX, what do you mean "there's no possibility"? Things become much cleaner once you clearly see what runs where and in what order - as Martijn Pieters points out, there's no ZPT in the browser and there's no AJAX on the server, so the title of the question does not make much sense.

Some of the options are:

  • clent sends an AJAX request, server does its server-side stuff, in the AJAX call success handler the client reloads the whole page using something like window.location.search='ts=' + some_timestamp_to_invalidate_cache. The whole page will reload with the new data - although it works almost exactly like a normal form submit, not much sense using AJAX like this at all.

  • client sends an AJAX request, server returns an HTML fragment rendered with ZPT which client then appends to some element on your page in the AJAX success handler:

    function update()
    {
        var variable = 'variable ';
        $.post("/diagnose_voorstel_get_data/${DosierID}")
           .done(function (data) {'
               $('#mytable tbody').append(data);
          });
    }
    
  • client sends an AJAX request, server returns a JSON object which you then render on the client using one of the client-side templating engines. This probably only make sense if you render your whole application on the client and the server provides all data as JSON.

Sergey
  • 11,892
  • 2
  • 41
  • 52
  • that's exactly what we concluded in the above comments. but thanks for pointing it out in a more decent way than a comment. – GertV May 14 '13 at 19:53
  • I wanted to use zpt if possible, yet I tought it wouldn't be realy possible so i was just checking. I am going to use the ajax call for a json and update my table this way. – GertV May 14 '13 at 19:58
  • @GertV: using zpt to generate the additional rows and append them to the table using an ajax call is very much possible if the server returns a HTML fragment rendered with zpt, as I've shown in the second option in the answer. – Sergey May 14 '13 at 20:48
  • yes, I understood that. But in our case that would pritty much mean refreshing the whole page because the page only holds 1 table with inputs. As I add items there might be a change on another page that has to be added to the table. Meaning I'd have to add some other logic to keep the current inputs as they are and insert them again after the update. I think I'm going for the json because it's easyer to keep track of the changes made inside the page and on the server. Thx very much for your help. – GertV May 14 '13 at 21:43