Skip to content

Commit 5b146df

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents bd1cd21 + a4e2464 commit 5b146df

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Tristan Balon
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# django_responsive_tables2
2+
3+
This project is intended to provide added functionality to the already outstanding [django_tables2](https://github.com/jieter/django-tables2) library. Using this package, you'll be able to easily implement responsive full-text search capabilities, pagination and more.
4+
5+
## Installation
6+
7+
This can be installed using pip by following the steps below.
8+
9+
In your Python environment, install django-responsive-tables2.
10+
11+
```
12+
pip install django-responsive-tables2
13+
```
14+
15+
Add the following to your projects *INSTALLED_APPS* variable found in *settings.py* so it looks as such.
16+
17+
```
18+
INSTALLED_APPS = [
19+
'django_tables2',
20+
'django_responsive_tables2',
21+
]
22+
```
23+
24+
From there, we can begin by adding a table class in a new file named *tables.py* located in your apps directory, as we would with [django_tables2](https://django-tables2.readthedocs.io/en/latest/pages/tutorial.html).
25+
26+
```
27+
class PersonTable(tables.Table):
28+
class Meta:
29+
model = Person
30+
template_name = "django_tables2/bootstrap.html"
31+
fields = ("name", )
32+
```
33+
34+
Once we've made our table class, we can add a function in our apps *views.py* file to handle AJAX requests for table data. You will find below a function named *build_filter*, however this mainly intended for development purposes, and a more efficient full-text search algorithm should be used for production.
35+
```
36+
def build_filter(search, *args, **kwargs):
37+
"""
38+
Builds a search filter given the search parameters & search text.
39+
Use like a regular filter, except set any values you want searched set to True.
40+
"""
41+
children = [*args, *sorted(kwargs.items())]
42+
search_keywords = search.split()
43+
search_filter = Q(_connector="AND")
44+
for keyword in search_keywords:
45+
current_filter = Q(_connector="OR")
46+
for (key, value) in children:
47+
if value is True:
48+
current_filter.children.append((key, keyword))
49+
search_filter.children.append(current_filter)
50+
return search_filter
51+
52+
53+
def table_person(request):
54+
# Get the search query, if it exists.
55+
search = request.GET.get("search")
56+
# Check to see if the search query exists.
57+
if search:
58+
# Search for specified fields, etc.
59+
search_filter = build_filter(search, first_name__icontains=True, last_name__icontains=True)
60+
table = PersonTable(Person.objects.filter(search_filter))
61+
else:
62+
table = PersonTable(Person.objects.all())
63+
64+
RequestConfig(request).configure(table)
65+
return HttpResponse(table.as_html(request))
66+
```
67+
68+
From there, we must add a URL pattern so the table can fetch the tables contents, and update the table contents when something is searched, or a page is changed. In our apps *urls.py* file, we can add the following URL routing.
69+
70+
```
71+
path('table-person/', views.table_person, name="TablePerson")
72+
```
73+
74+
Now that we have our table requests, search handling & URL routing finished, we can begin to implement it into one of our views templates. There is nothing needed in the context of the view you want the table on, the only thing that is required for having the table appear on your page is to have it implemented into your views template.
75+
76+
Inside the template of the view you'd like the table to appear we must load static files and the _responsive_table_ template tag.
77+
```
78+
{% load static %}
79+
{% load responsive_table from responsive_table %}
80+
```
81+
82+
Now add the following script to the bottom of the body tag, or to the head tag.
83+
84+
```
85+
<script src="{% static 'django_responsive_tables2/tables.js' %}" type="application/javascript"></script>
86+
```
87+
88+
Then, wherever you would like the table to show on your template, insert the table as such. The first argument is the HTML ID of the table & related objects. This can be anything you'd like however if there are multiple tables on your page these must be different. The second argument is the URL pattern we created for handling the AJAX requests.
89+
90+
```
91+
{% responsive_table "myTableID" "example:TablePerson" %}
92+
```
93+
94+
All done! From there we can add as many more tables to our template as we please.

0 commit comments

Comments
 (0)