Top strategy for creating a customizable table using Django

Check out my new Template which includes an HTML-table and an iframe that's rendering user-uploaded pdf's: https://i.sstatic.net/Jpg0lTm2.png

The main purpose is to enable users to link table entries with one or more pdf files. This part has been successfully accomplished.

Now, I'm seeking advice on the best approach for filtering the data in my table. Here's how the table is structured:

{% extends "website/base.html" %}
{% block content %}
{% load static %}
{% load evidence_extras %}
<script src="{% static 'evidence/matching_final.js' %}"></script>
<link rel="stylesheet" type="text/css" href="{% static 'evidence/css/style.css' %}">
<script src='https://cdnjs.cloudflare.com/ajax/libs/axios/1.6.8/axios.js'></script>

<div class="container">
    {% csrf_token %}
    <div class="row">
        <div class="col-8">
            <div class="row">
                <div class="col-12">
                    <table class="table">
                        <thead class="table-dark">
                            <td>
                                id
                            </td>
                            .......
                        </thead>
                        <tbody>
                            {% for piece in transactions %}
                            <tr onclick="handleRowClick(this, {{piece.id}})">
                                <td>
                                    {{ piece.id }}
                                </td>
                                ......
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>
            <div class="row">
                <div class="col-12">
                    <button onclick="handleMatch(this)" class="btn btn-primary" style="width: 100%;">Match</button>
                </div>
            </div>
        </div>
        <div class="col-4 overflow-scroll" style="height: 60em;">
            {% for evidence in evidences %}
            <div class="row">
                <div class="col-12">
                    <div class="card" id="evidence_{{evidence.id}}">
                        <div class="card-header">{{ evidence.name}}</div>
                        <div class="card-body">
                            <iframe src="/evidence/pdf/{{evidence.id}}" id="pdf_frame" width="100%"
                                height="400"></iframe>
                            <ul>
                                <li maxlength="20"> file: {{evidence.file}} </li>
                                <li maxlength="20"> hash: {{evidence.hash}} </li>
                            </ul>
                            <div class="row">
                                <div class="col-6">
                                    <button onclick="handleMarkClick(this, {{evidence.id}})" class="btn btn-primary"
                                        style="width: 100%;">mark</button>
                                </div>
                                <div class="col-6">
                                    <button onclick="handleExpandClick(this,{{evidence.id}})" class="btn btn-primary"
                                        style="width: 100%;">show</button>
                                </div>
                            </div>
                        </div>
                    </div>

                </div>
            </div>
            {% endfor %}
        </div>
    </div>


</div>
{% endblock %}

I'm considering constructing the table entirely with JavaScript, but I'm unsure if that aligns with Django practices. Is it possible to apply a filter directly to the frontend context data, maybe using JavaScript?
I'd like to implement a filter without reloading the data every time a filter is applied, as I've already loaded the necessary data and want to prevent unnecessary database queries. Any suggestions to move forward would be appreciated.
Thank you!

Edit:
Just a heads up, I prefer not to rely on pre-built Django libraries as I am still learning and require a fair amount of customization.

Edit2: Minimal example for reference -> stack.html:

{% extends "website/base.html" %}
{% block content %}
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'evidence/css/style.css' %}">
<div class="container">
    {% csrf_token %}
    <div class="row">
        <div class="col-8">
            <div class="row">
                <div class="col-12">
                    <table class="table">
                        <thead class="table-dark">
                            <tr>
                                <td>
                                    mame
                                </td>
                                <td>
                                    hash
                                </td>
                                <td>
                                    something
                                </td>
                            </tr>
                        </thead>
                        <tbody>
                            {% for row in stack %}
                            <tr>
                                <td>
                                    {{ row.name }}
                                </td>
                                <td>
                                    {{ row.hash }}
                                </td>
                                <td>
                                    {{ row.something }}
                                </td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}

view:

def stack(request):
    context = Stack.objects.all()
    return render(request, "evidence/stack.html", {"stack": context})

URLs:

app_name = "evidence"
urlpatterns = [
 ...,
    path('stact', stack, name='stack'),
]

Model:

class Stack(models.Model):
    name = models.CharField(max_length=150)
    hash = models.CharField(max_length=150)
    something = models.CharField(max_length=150)

Answer №1

Filtering can be done using JS alone, but for pagination, filtering should be handled by backend database queries. It is recommended to implement proper filtration and pagination on the backend. Instead of instant typing filter, consider adding a button to filter content.

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

Angular rest call is returning an undefined response object attribute

When attempting to retrieve the attribute of a response object, it is returning as "undefined". var app = angular.module('angularjs-starter', []); app.controller('MainCtrl', function($scope, $http) { $scope.addNewChoice = functio ...

Why is Property CSS of Undefined Giving Error?

var random; var squareArray = [$('#square1'),$('#square2'),$('#square3'),$('#square4')]; var randomOrder = []; var i = 0; var j = 0; function computerPlays() { random = Math.floor(Math.random() * 4); randomO ...

Displaying a popup containing a div when clicking on a link

I need assistance with creating a link that will display a div in a popup window. Here is the link I am working with: <li><a class="newAttachmentType" onclick="getFiles(true)">Move to somewhere</a></li> Also, here is the div that ...

Is it possible for Jquery to directly retrieve the form input without the need for a SET button in the script?

I have a script that functions as a basic countdown. All you need to do is enter a number, press the SET button, and click START for the countdown to begin. Although I primarily use this script in the gym, I often forget to hit the SET button after enteri ...

There is no matching overload for this call in React Native

I am working on organizing the styles for elements in order to enhance readability. Here is the code I have written: let styles={ search:{ container:{ position:"absolute", top:0, }, } } After defining the s ...

Issue with updating Leaflet map within Bootstrap 4 tabs

Here is an issue I am facing in my project. I am currently working on a Bootstrap 4 page that has multiple tabs. Each tab contains a leaflet map, dygraphs, and other content. The problem arises when I switch between tabs, the map does not reload properly ...

Is there a way to automatically populate my form with data depending on the value selected in a dropdown menu?

I'm facing an issue with prefilling a form based on the selected value from a dropdown menu that loads invoice IDs from my database. The problem arises when trying to populate the form fields using the data corresponding to the selected invoice ID. Th ...

The error message "unsupported_grant_type" was encountered while using the Django OAuth2 server

Having trouble getting django to accept my POST request for an access token. Despite having the correct parameters and authorization code, I keep receiving an error after sending the follow-up POST request. According to what I've read, the content-ty ...

The implementation of pushing inside a foreach loop is not correctly adding elements to

I have encountered an issue with running a foreach loop and adding values to an array in my code. The first foreach loop works as expected, adding values properly to the array. However, the second foreach loop seems to be malfunctioning as none of its valu ...

"Bringing the power of JavaScript to your WordPress

I have set up a wordpress page and integrated some JavaScript into it. When working in wordpress, there are two tabs on a page where you can input text - 'Visual' and 'Text'. The 'Text' tab is used for adding HTML and scripts ...

Adding elements to a global array via a callback function in JavaScript: A step-by-step guide

I am currently working on querying and adding users to a global array from my database. My goal is to store the elements in this global array so that I can access it from any part of my application. app.get("/admin/orders", (req, res) => { Q ...

The slider on my iPad and Kindle Fire in Linux mode is not functioning properly

Having an issue on my e-commerce platform, Bence Tupperware, which is built with MVC.Net. The problem seems to be related to the slider at the top of the page. After checking in Mozilla's responsive design mode, everything appears to work fine on devi ...

Mastering the art of selecting checkboxes and Font Awesome icons with JQuery

I find selecting elements using JQuery by their ID or classname quite challenging and it's a bit outside of my expertise. When a function is called, I would like to apply the following styles: .classname input[type="checkbox"] ~ i.fa.fa-circle-o { ...

Using a single name in the form, input multiple parameters and retrieve a comprehensive list of all of them

I'm struggling with a form that has dynamic fields. After submitting the form, I am getting this URL: Localhost:8000/mysite.com/jobs_form?job="Job1"&job="Job2" But I am unable to retrieve all of them in Django using reques ...

What could be causing my Vue code to behave differently than anticipated?

There are a pair of components within the div. When both components are rendered together, clicking the button switches properly. However, when only one component is rendered, the switch behaves abnormally. Below is the code snippet: Base.vue <templa ...

Tips for generating various series using dx-Chartjs

Trying to visualize ratings based on their type is proving to be difficult as I can't seem to figure out how to group the series according to types. The available chart options are: $scope.chartOptions = { dataSource: data, c ...

Tips for importing a JSON file from your local directory in Vue.js

I've been attempting to load data from a local JSON file in Vue. My goal is to simply load the data from the file and assign it to a variable. I'm not sure if I'm on the right track or if I'm missing something essential in my approach. ...

Extend a Django class to accommodate an extra keyword argument

I'm currently working on customizing an ajax uploader to allow for a hash_id of a django model (used to create the model after successfully uploading an image). I'm facing issues with passing the additional kwarg (widget2_hash_id) and would appre ...

How to manually remove an Angular directive

Is it possible to manually remove a directive? For instance, if I need the directive to perform a task once and then be cleared from memory. @Directive({ selector: '[doSomething]' }) export class DoSomethingDirective { constructor(private ...

Does D3 iterate through the entire array every time we define a new callback?

It seems that every time a callback is set, d3 loops through the entire array. Initially, I thought that functions like attr() or each() were added to a pipeline and executed all at once later on. I was trying to dynamically process my data within d3&apo ...