Giter Club home page Giter Club logo

django-tables2-ajax's Introduction

django_tables2_ajax

This project is intended to provide added functionality to the already outstanding django_tables2 library, inspired & based off of django-ajax-tables. Using this package, you'll be able to easily implement responsive full-text search capabilities, pagination and more.

See project on GitHub.

Installation

This can be installed using pip by following the steps below.

In your Python environment, install django_tables2_ajax.

pip install django-tables2-ajax

Add the following to your projects INSTALLED_APPS variable found in settings.py so it looks as such.

INSTALLED_APPS = [
    'django_tables2',
    'django_tables2_ajax',
]

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.

class PersonTable(tables.Table):
    class Meta:
        model = Person
        template_name = "django_tables2/bootstrap.html"
        fields = ("name", )

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.

def build_filter(search, *args, **kwargs):
    """
    Builds a search filter given the search parameters & search text.
    Use like a regular filter, except set any values you want searched set to True.
    """
    children = [*args, *sorted(kwargs.items())]
    search_keywords = search.split()
    search_filter = Q(_connector="AND")
    for keyword in search_keywords:
        current_filter = Q(_connector="OR")
        for (key, value) in children:
            if value is True:
                current_filter.children.append((key, keyword))
        search_filter.children.append(current_filter)
    return search_filter


def table_person(request):
    # Get the search query, if it exists.
    search = request.GET.get("search")
    # Check to see if the search query exists.
    if search:
        # Search for specified fields, etc.
        search_filter = build_filter(search, first_name__icontains=True, last_name__icontains=True)
        table = PersonTable(Person.objects.filter(search_filter))
    else:
        table = PersonTable(Person.objects.all())

    RequestConfig(request).configure(table)
    return HttpResponse(table.as_html(request))

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.

path('table-person/', views.table_person, name="TablePerson")

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.

Inside the template of the view you'd like the table to appear, add the following script to the bottom of the body tag, or to the head tag.

<script src="{% static 'django_tables2_ajax/tables.js' %}" type="application/javascript"></script>

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.

{% responsive_table "myTableID" "example:TablePerson" %}

All done! From there we can add as many more tables to our template as we please.

django-tables2-ajax's People

Contributors

legendaryfire avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.