dynamicly fill table using zpt and ajax as update

2019-07-25 05:19发布

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.

1条回答
我命由我不由天
2楼-- · 2019-07-25 05:42

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.

查看更多
登录 后发表回答