3

I am doing an ajax POST call which returns a zipped list of 4 lists in response.

ajax call:

$('[name="start_manifest"]').on('click', function(event){
    event.preventDefault();
    var channel = $('[name="channel_name"]').val();
    var courier = $('[name="courier_name"]').val();
    $.ajax({
        url : "/manifest/",
        type : "POST",
        data : {action:'start_manifest',
                channel:channel,
                courier:courier},

        success : function(response) {
            var order_data = response.order_data;
            $.niftyNoty({
                type:"primary",icon:"",title:"Start Scanning Orders",message:"Current Manifest Number: " + response.manifest_number,container:"floating",timer:5000
            });
        },
        ....

My django view:

if request.POST.get('action') == 'start_manifest':
    channel = request.POST.get('channel')
    courier = request.POST.get('courier')
    manifest_data = manifest.startManifest(channel, courier)
    response_data = {}
    response_data['manifest_number'] = manifest_data[0]
    response_data['order_data'] = manifest_data[1]
    return HttpResponse(json.dumps(response_data),content_type="application/json")

Here manifest_data[1] is a zipped list like:

manifest_data[1] = zip(LIST_1, LIST_2, LIST_3, LIST_4)

Normally I can populate a table from a zipped list like:

{% for a,b,c,d in order_data %}
<tr>
    <td>{{a}}</td>
    <td>{{b}}</td>
    <td>{{c}}</td>
    <td>{{d}}</td>
</tr>
{% endfor %}

My question is:

How to populate a table from a zipped list passes as a response to an ajax call? In my case i stored the list in var order_data. Now how can i use this variable to show data in my table? Lets say table id = 'manifest_table'.

5
  • 1
    How is your question even remotely related to Python/Django ??? It's a pure html/js problem, the backend could be in PHP, Ruby or even Perl/CGI, it wouldn't change anything. Commented Jan 7, 2016 at 9:04
  • Thanks for the info. My backend is in django so i included it. Commented Jan 7, 2016 at 9:07
  • You will have to write JavaScript for that. Commented Jan 7, 2016 at 9:18
  • 1
    @ManishGupta the backend is totally irrelevant here. What you want is to make an ajax request from your page and update your page's dom accordingly - which is really nothing new FWIW -, it's a pure frontend programming question. Commented Jan 7, 2016 at 9:22
  • @brunodesthuilliers I removed the tags. Commented Jan 7, 2016 at 9:26

1 Answer 1

1

I used this code to iterare over the array and add data to table rows.

var order_data = response.order_data;
$('#manifest_table').html('');
$.each(order_data, function(i, item) {
    $('<tr id='+ order_data[i][3] +'>').html('<td>'+ inp +'</td><td style="text-center" >' + order_data[i][0] + '</td><td style="text-center" >' + order_data[i][1]  + '</td><td style="text-center" >' + order_data[i][2]  + '</td><td style="text-center" >' + order_data[i][3]  + '</td>').appendTo('#manifest_table');
});
Sign up to request clarification or add additional context in comments.

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.