What could be causing the datatable options to malfunction even after importing necessary files and scripts?

Struggling to integrate DataTable and jQuery into my HTML following the example on https://datatables.net/examples/basic_init/zero_configuration.html. However, I can't seem to get any options to work properly. Is there a mistake in my import order? My libraries are up-to-date. As a beginner in web development, any help would be greatly appreciated.

https://i.sstatic.net/FyIIz.png

My imports are as they should be:

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <title>Core Reporting</title>

  <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/jq-3.3.1/dt-1.10.20/datatables.css"/>
  <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.11.2/css/all.css">
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap">
  <link href="{{ url_for('static', filename='css/bootstrap.min.css') }}" rel="stylesheet">
  <link href="{{ url_for('static', filename='css/mdb.min.css') }}" rel="stylesheet">
  <link href="{{ url_for('static', filename='css/style.css') }}" rel="stylesheet">
</head>

In the body, there is a placeholder div for the table:

<div class="container-fluid" id="main-container" style="margin-top: 120px; max-width: 1800px; background-color: white;">
    {% block maincard %}{% endblock %}
</div>

And script imports before closing body:

<script type="text/javascript" src="https://cdn.datatables.net/v/dt/jq-3.3.1/dt-1.10.20/datatables.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.js"></script>
<script type="text/javascript" src="{{ url_for('static', filename='js/popper.min.js') }}"></script>
<script type="text/javascript" src="{{ url_for('static', filename='js/bootstrap.min.js') }}"></script>
<script type="text/javascript" src="{{ url_for('static', filename='js/mdb.min.js') }}"></script>
<script>
    $(document).ready(function() {
        $('#hypervisorTable').DataTable({
          scrollY: 300,
          paging: true
        });
    });
</script>

Extending the home template with a sample tables.html template:

{% extends 'home.html' %}

{% block maincard %}
<table id="hypervisorTable" class="display" width="100%">
    <thead>
        {% for col in data.columns %}
            <th class="th-sm"> {{ col }} </th>
        {% endfor %}
    </thead>
    <tbody>
        {% for row in data.index %}
        <tr>
            <td>{{ data.loc[row, 'Instance type'] }}</td>
            <td>{{ data.loc[row, 'Hypervisor'] }}</td>
            <td>{{ data.loc[row, 'vCPUs'] }}</td>
            <td>{{ data.loc[row, 'Cores'] }}</td>
            <td>{{ data.loc[row, 'Threads per core'] }}</td>
            <td>{{ data.loc[row, 'Memory (MiB)'] }}</td>
            <td>{{ data.loc[row, 'Storage (GB)'] }}</td>
        </tr>
        {% endfor %}
    </tbody>
</table>
{% endblock %}

Answer №1

The table header was missing some columns from the data object. In order for the DataTable to display correctly, it requires an exact match of formatted columns and rows.

To fix this issue, I looped through all the columns and values:

<table id="hypervisorTable" class="display" width="100%">
    <thead>
        {% for col in data.columns %}
        <th class="th-sm"> {{ col }} </th>
        {% endfor %}
    </thead>
    <tbody>
        {% for row in data.index %}
        <tr>
            {% for col in data.columns %}
            <td>{{ data.loc[row, col] }}</td>
            {% endfor %}
        </tr>
        {% endfor %}
    </tbody>
</table>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Adjust the width of the media player object according to the browser being used

In my asp.net web application, I have a media player object that displays differently in Chrome, Firefox (as VLC player) and IE (as Windows Media Player). This results in varying width of the embedded audio player object. How can I adjust the width of the ...

Is there a way for me to place an image in the background with CSS? When I try to do so, the upper curve looks great, but the bottom of the image appears flat

.bg-pink{ background-image:url(https://i.sstatic.net/eF8tF.png); background-size: cover; margin-bottom: 44px; background-position: top; } <body> <!-- Top content --> <div class="top ...

Expanding Beyond Boundaries: Utilizing CSS to Overflow From a Div and Fill the Entire Screen

I have a main DIV that acts as part of my responsive layout grid. It grows to the maximum width I've set, which is 1280px, and then adds margins for larger devices. Below you'll find my CSS along with a snippet of Less code. .container { mar ...

This issue arises when attempting to display a Facebook like box within an iframe using Fancybox

As a newcomer to JavaScript, I've been trying to display a fancybox iframe containing my Facebook fan page like box when my website first loads, but have been unsuccessful so far. I've included all the necessary calls to the js files in fancybox ...

Do you have any suggestions for improving my Content Aggregator Website? I'm having trouble getting the articles to display on the site

I tried following an online guide, but had trouble scraping various websites. The article headlines are not displaying for me. I can't figure out if the issue lies with my return function or my HTML file. Below is the code snippet from the views.py fi ...

Having trouble with contact form functionality, developed with AJAX, PHP, and HTML5

I am currently in the process of creating a functional contact form for my website to streamline communication with potential clients who are interested in design services. Although I have never developed an operational contact form that sends emails, I c ...

Stacking of div tags on top of each other

Looking for assistance with some CSS challenges. I am attempting to design a group of buttons using div tags, but they are all merging into one another. You can find the code at http://codepen.io/CliffTam/pen/gadEaq Could someone review the code and pro ...

Fluid div causing navigation to display improperly

I am currently working on a navigation design where I want to have an image above each list item instead of having individual images for each item. It looks great when the screen is at full size, but as soon as I minimize it, the list items start stacking ...

Increasing the height of a div in a vertical direction

Having an issue with divs not expanding vertically when there are anchors with padding inside. The explanation isn't the clearest, so I've recreated the problem here: http://jsfiddle.net/uF6KN/3/. Essentially, I want the blue anchors to align wi ...

Which architectural style is best to master for developing exceptional JavaScript software?

My familiarity with Model-View-Controller runs deep, having worked with it for years in server-side development using languages like PHP. Now, I find myself diving into JavaScript and embarking on a large project that utilizes SVG, Canvas, and other moder ...

The initial selection in the First DropDown does not activate the function of the second DropDown

I am facing an issue with my MVC application that involves using AngularJS for two DropDowns. The first DropDown successfully populates with data from the DB. However, when a user selects an option from the first DropDown, the second one should update acco ...

What is the maximum number of JSON responses that can be handled by AJAX?

Upon entering the site, I am attempting to receive a JSON as an AJAX response. However, I am curious if there is a limit to the size of the object I can retrieve - whether through a GET or POST request? $http({ method: 'POST', url: &apos ...

Activate Dropdown on hover and automatically close the menu when a link is clicked

Looking for assistance with a Dropdown menu that opens on mouse hover but needs to close when a link is clicked within the menu. Do you think JavaScript would be necessary for this function? .dropdow { position: relative; ...

Concealing subcategories within a responsive menu using jQuery

My website's responsive menu changes based on screen resolution, but I encountered an issue. On mobile viewports, the sub items in the menu are displayed by default. I want them to only appear when the parent item is clicked. Currently, clicking the p ...

When a user clicks on an anchor tag, close the current window, open a new window, and pass the

I have a scenario where I have an anchor tag that triggers the opening of a window on click. In this newly opened window, there is a table with a column containing another anchor tag. Here is what I am trying to achieve: Code for the anchor tag: functio ...

How to utilize jQuery to eliminate HTML onchange code

This block of code features an HTML onchange attribute as well as the use of jQuery's change event on an input text. I want only the jQuery change event to take precedence. I have attempted to use both unbind and off without success. Can someone prov ...

Photo attached at the bottom of each container

Here is the link to my code snippet: fiddle. I am struggling with aligning shopping bag icons at the bottom of each box on my webpage. I want all boxes to have the same height, so I used display: table-cell; in the box_left div. However, placing the shopp ...

Versatile Function for Handling Dropdown Changes

I am faced with the challenge of executing a javascript function when multiple select elements are changed. I want to create a versatile function that can be set as the onchange method for various select elements. The following code accomplishes this task ...

Flipping the switch to illuminate or darken a room

I am working on a project where I want to create a program that turns on a lightbulb when you click on it, and then turns it off when you click on it again. However, I am facing an issue where no matter which light I click on, the first one always turns ...

What is the best way to adjust the width of a navbar using JavaScript?

I am experimenting with a responsive design and facing an issue - the width of my #menu in CSS is initially set to 0, but I need to change this value based on user input using JavaScript. Now, I want to dynamically adjust the width of the menu for differe ...