views:

304

answers:

2

Hi,

I'm using the load method to replace the contents of ONE div.

$( '#ajax_tbody_result' ).html( ' ' ).load(url);

Now I'm wondering whether it is possible to call the url, get the return values, split them up and update TWO or more divs in my template.

Thus the solution would be to get some objects out of the returned django context, split it up and update the divs with the correct content. This is the returned context:

context = {
            'object_list' : contact_list_page,
            'headers': headers,
            'filter_by_classification_form': filter_by_classification_form,            
            'filter_by_address_form': filter_by_address_form,
            'filter_by_company_form': filter_by_company_form,
            'urlquerystring_previous_page' : urlquerystring_previous_page,
            'urlquerystring_next_page' : urlquerystring_next_page,
        } 

Edit: The solution:

Split up the html response of a jquery get request into its parts and update the corresponding parts in the .html. You can do it like this:

$.get(url, function(results){
      var table = $("table", results);
      var spans = $("span.step-links", results);

      //update the ajax_table_result with the return value
      $('#ajax_table_result').html(table);
      $('.step-links').html(spans);

    }, "html");

The results object is the django template rendered to .html. From this .html return I take the stuff that I need (the table and the span with the id step-links) and update the corresponding objects in my page.

+1  A: 

use

$.get(url, [{key:value}], function(data){
    //data is the entired contents of going to that url.
    //split it up here and do your stuff

    $('#div1').html(piece1);
    $('#div2').html(piece2);
});

http://docs.jquery.com/Ajax/jQuery.get

edit: looking more closely at your question, you will need to also create a django template for the view which you're calling with url that will give all the information you want to the javascript function.

Brandon H
i just stumpled upon http://docs.jquery.com/Ajax/jQuery.getJSON. Is it better to use get or getJSON?
Tom Tom
if you're dealing with JSON in the javascript, then use getJSON. but you'll have to make your view return JSON. django has some functions that return JSON formatted data, but i think that wouldn't work for that context. (It would depend on which parts of that context you needed)
Brandon H
Ok thanks. My view returns a template for an ajax call. With the part of your code: .html(piece1) . Do I reference on the django context or on a part of the returned template? My view returns it like this: return render_to_response( template, context, context_instance = RequestContext( request ) )
Tom Tom
can you update your question with the output of the url that goes to the ajax/get function? data = that output. then you'd manipulate that into piece1 and piece2 or however you need to divide it up.
Brandon H
that's right. I just do not know how I can manipulate the returned data object, which is actually a html rendered django template.
Tom Tom
You were right with your first hint. I just have to split up the html return of the .get request and update the corresponding parts. Thank you for pointing me in the right direction! I have posted the solution in my initial post.
Tom Tom
A: 

I feel like a broken record by giving this answer, but you really want to check out the jQuery Taconite Plugin. It does what you asked for and 500% more. Checkout the examples and be amazed. I don't use this word lightly, but this is a truly elegant solution.

Peter Rowell