1

I am populating page with html loaded from controler by calling

$("#test").load("{% url 'simple_dataframe' %}");

'simple_dataframe' is pandas.to_html() method which is returning proper html for datatables jquery.

Next script is:

<script>
    $(document).ready(function () {
        $('#df_table').DataTable();
    });
</script>

Which doesnt load properly. I have copied returned html to page and run it with only DataTable jquery and it runs fine. So its not related to malformed html. I think its something to do with the order in which scripts are called.

{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="{% static '/css/bootstrap.min.css' %}" type="text/css"/>
    <link rel="stylesheet" href="{% static '/css/jquery.dataTables.css' %}" type="text/css"/>
    <script src="{% static "js/jquery-3.3.1.min.js" %}"></script>
    <script src="{% static 'js/jquery.dataTables.js' %}"></script>
</head>
<body>
<div id="test"></div>
<script>
    $(document).ready(function () {
        $("#test").load("{% url 'simple_dataframe' %}");
    });</script>

<script>
    $(document).ready(function () {
        $('#df_table').DataTable();
    });
</script>
</body>
</html>
0

1 Answer 1

1

The problem is because the load() call is asynchronous, so you're attempting to create a Datatable on HTML which doesn't yet exist. To fix this, put the DataTable() call in the callback of load(), like this:

$(function () {
  $("#test").load("{% url 'simple_dataframe' %}", function() {
    $('#df_table').DataTable();
  });
});
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.