0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Using ag-grid table within django template

Last updated at Posted at 2025-02-15

How can I use ag-grid table within django template

Suppose you want to render a simple data, which is a list of dictionaries. Each dictionary can be thought of as a data object with consistent keys.
Pay attention to how the code accesses column names from the first object in the list using Object.keys(<js object>) method. This way you can reuse this template for multiple django views. All you need is you are passing data that is a list of dictionaries with consistent keys.

{% extends "layout.html" %}

{% block extra_css %}
<!-- ag-Grid CSS -->
<link rel="stylesheet" href="https://unpkg.com/ag-grid-community/styles/ag-grid.css">
<link rel="stylesheet" href="https://unpkg.com/ag-grid-community/styles/ag-theme-material.css">
{% endblock %}

{% block title %}{{title}}{% endblock %}

{% block content %}
<h2 class="mb-4">{{ title }}</h2>
<!-- ag-Grid container -->
<div id="myGrid" class="ag-theme-material" style="height: 700px; width: 100%;"></div>

<!-- ag-Grid JS -->
<script src="https://unpkg.com/ag-grid-community@31.0.1/dist/ag-grid-community.min.js"></script>

<script>

    // Django JSON Data (Rendered from View)
    var rowData = {{ data | safe }};

    console.log(`data is ${JSON.stringify(rowData)}`)


    const columnDefs = Object.keys(rowData[0]).map((columnName)=>({headerName:columnName, field:columnName, filter: "agTextColumnFilter"}))
    // Grid Options
    var gridOptions = {
        columnDefs: columnDefs,
        rowData: rowData,
        pagination: true
    };

    // Initialize ag-Grid
    document.addEventListener('DOMContentLoaded', function () {
        var gridDiv = document.querySelector("#myGrid");
        //new agGrid.Grid(gridDiv, gridOptions);
        const gridApi = agGrid.createGrid(gridDiv, gridOptions);
    });
</script>

{% endblock %}

layout.html looks like below

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{% block title %}My Website{% endblock %}</title>

    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom CSS -->
    {% block extra_css %}{% endblock %}
</head>
<body>

    <!-- Navigation Bar -->
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
        <div class="container">
            <a class="navbar-brand" href="{% url 'home' %}">MySite</a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav">
                    <li class="nav-item">
                        <a class="nav-link" href="{% url 'home' %}">Home</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="{% url 'about' %}">About Us</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="{% url 'contact' %}">Contact</a>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

    <!-- Main Content -->
    <div class="container mt-4">
        {% block content %}{% endblock %}
    </div>

    <!-- Bootstrap JS -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>

    {% block extra_js %}{% endblock %}
</body>
</html>
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?