简体   繁体   English

使用另一个数据表和Django的选择将数据加载到jquery数据表

[英]loading data to jquery datatables using the selection of another datatable and Django

I have a few tables on a page in a Django application and I am using Datatables to view, search and sort the data. 我在Django应用程序的页面上有几个表,并且我正在使用数据表来查看,搜索和排序数据。

If a row of one datatable, the customers table, is selected by the user I send the ID of this row using AJAX to a view in my views.py file. 如果用户选择了一个数据表的行(客户表),则使用AJAX将这一行的ID发送到我的views.py文件中的视图。 What I want to happen now is that a second table, the visits table, is updated with the relevant visits for this customer. 我现在想做的是,用该客户的相关访问更新了第二个表,即访问表。 I know that the data is sent correctly and the Django query I create is correct as I can print out the visits object to the Django console. 我知道数据已正确发送,并且我创建的Django查询是正确的,因为我可以将visits对象输出到Django控制台。 But I am struggling with what to do with the data now. 但是我现在正在努力处理数据。

Usually I would render a template using this data as a context, and in fact this is how I generated the customer table. 通常,我会使用这些数据作为上下文来呈现模板,实际上,这就是我生成客户表的方式。 But I think I need to send it back to the client side and let the Datatable do the rendering? 但是我想我需要将其发送回客户端,然后让Datatable进行渲染? Both tables are on the same page so I want to just render the updated visits table. 两个表都在同一页面上,所以我只想呈现更新的visits表。

Here is a snippet of my Javascript file 这是我的Javascript文件的片段

var cust_table = $('#all_customers').DataTable({"lengthMenu": [3],
                                               "bLengthChange": false,
                                               "paging": true,
                                                select: 'single'});
$('#all_visits').DataTable({"lengthMenu": [3],
                            "bLengthChange": false})

cust_table.on('select', function (e, dt, type, indexes) {
      var rowData = pt_table.rows(indexes).data().toArray()[0][0]; 
      var URL = 'select_customer/';
      var id = { 'id': rowData };
      $.get(URL, id,  function (response) {
          if (response === 'success') {alert("success");}
          else {alert("error");
          }
    });

and this is the view that processes the request 这是处理请求的视图

def select_customer(request):

"""
Use the customer id selected by the user to fill the visits table.
"""
try:
    if request.method == "GET":
        print "request has come through"
        if "id" in request.GET:
            visits = (CustomerId.objects.get(id=request.GET["id"])
                      .customer.visit_set.all()
            context = {"visits": visits}
            render(request, 'flair_app/patients/visit_table.html', context)
except:
    return HttpResponse('fail')

As I said, I can print out the visits object on the Django console so I know they are there. 就像我说的,我可以在Django控制台上打印出visits对象,因此我知道它们在那里。 But trying to render the visit_table template, which just contains the table doesn't change the page. 但是,尝试呈现只包含表格的visit_table模板不会更改页面。

I am quite new to web development so apologies if this is an easy question; 我是Web开发的新手,所以很抱歉这是一个简单的问题。 but how do I display the data on the page? 但是如何在页面上显示数据?

You are getting back an HTML string from the server. 您正在从服务器取回HTML字符串。 But on the client, you only receive it in your response variable, but then you don't do anythong with it. 但是在客户端上,您只在response变量中收到它,但随后却什么也没做。

You need to inject it into the HTML of the page if you want to to render. 如果要呈现,则需要将其注入页面的HTML中。

Assuming that flair_app/patients/visit_table.html is only the HTML for the #all_visits table, you could simply replace the current HTML in that element with the HTML that was returned by the server. 假设flair_app/patients/visit_table.html只是#all_visits表的HTML,您可以简单地用服务器返回的HTML替换该元素中的当前HTML。 Something like this 像这样

$.get(URL, id)
.done(function(responseData) {
    $('#all_visits').html(responseData);
})
.fail(function(error){
    // handle error
});

After that, you will probably need to rebind you DataTable objects again 之后,您可能需要再次重新绑定DataTable对象

$('#all_visits').DataTable({"lengthMenu": [3], "bLengthChange": false})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM