Giter Club home page Giter Club logo

Comments (11)

mwhudson avatar mwhudson commented on May 18, 2024

I have some code that renders tables and config to use datatables (from datatables.net) if you're interested... hopefully it will be in a form that will be generally useful in a week or two.

from django-tables2.

christhekeele avatar christhekeele commented on May 18, 2024

This functionality is the main reason I authored pull request #65. My hope was to pass it endless-pagination's paginator and tweak the code a little. Initially I tried just importing and referencing parts of endless. Ultimately I had to hack apart endless and splice it into tables2, ending in possibly the ugliest code I've ever refused to push to github. It works with Digg-style pagination, but I haven't tried Twitter or infinite. I intend to clean up and refine it before I show it to another soul.

However, @mwhudson, the reason I picked up django-tables2 is explicitly because I wanted to avoid using datatables.net jquery plugin. It just doesn't offer our front-end guy the control we need over the appearance. My intention is to eventually build a django-datatables app that gives all the functionality of the datatables.net plugin, while using django's functionality and rendering a more style-able table. I'm all for pushing this functionality to tables2 if @bradleyayers wants this repo to go in that direction, otherwise, I'll probably make my own repo based off of my pull.

In order to replace datatables.net, we'd have to support 4 main features: filtering, searching, ordering, and paginating. The dataset has to be transformed roughly in that order:

  • Filter out unwanted records (say, where active isn't True)
  • Pull out records that match the search parameters
  • Order the data set by column, either as specified or as default
  • Break dataset into pages and render table

Django-tables2 already supports column ordering and non-AJAX pagination. Filtering and searching are comparatively easy, because you can pass any queryset or dataset to tables2. I want to ultimately support extra parameters in the Meta of the tables.py schema to help with filtering and searching, but just modifying the queryset instead of using the 'model' parameter will do it for me right now.

AJAX pagination is more difficult since the table actually has to be built off the data set for column ordering to work (at least, the way django-tables2 does it). So for me to get AJAX pagination working, I pretty much had to hack all the code from endless-pagination into django-tables2 and call it after the table gets built. I know there's a better way, but having never gotten into the nitty-gritty of django pagination, this approach was pretty educational. Any better ideas on how to approach this problem?

from django-tables2.

mwhudson avatar mwhudson commented on May 18, 2024

For what it's worth, the code we came up with is at

http://bazaar.launchpad.net/~linaro-validation/lava-server/trunk/view/head:/lava/utils/data_tables/tables.py

and

http://bazaar.launchpad.net/~linaro-validation/lava-server/trunk/view/head:/lava/utils/data_tables/backends.py#L162

and you can see it in action in several places in this file:

http://bazaar.launchpad.net/~linaro-validation/lava-scheduler/trunk/view/head:/lava_scheduler_app/views.py

I guess compared to your code, we probably leave a bit more to the js? I didn't even think of using Django's pagination stuff partly because I'm not very familiar with it I guess, but also in my view, once the initial table is rendered, it's all down to the ajax, and all you need is another view that can serve up the data required for a request for page N or to filter by some conditions or to order by some column (we don't have a non-ajax fallback). Our implementation of this doesn't really involve tables per se, although it does use the django-tables2 code to render each cell in the same way as it would be rendered directly.

In some sense there isn't much to do for ajax tables. You need to:

  1. Define the protocol by which the page requests data. You could just re-use what datatables uses here, though it's kinda ugly.
  2. Write/find some js for the frontend that requests data via this protocol and puts it into a table
  3. Write a view at the back end that serves up this data.

I'm not sure how django's pagination stuff helps with any part of this, really? Most of the things it does are probably things the js should be doing.

from django-tables2.

bradleyayers avatar bradleyayers commented on May 18, 2024

Well I think it makes sense that if you're using AJAX based pagination, the pagination code would be in JS. The biggest challenge I see at the moment is providing nice endpoints that expose table data (optionally rendered HTML) in a way that's easy to plug JS datagrid libraries into.

I don't want to hack this library into something that's only useful for datatables, but I don't mind blessing datatables in a form similar to Fabric or Django's contrib structure. Ideally we'd have a single API usable from JS that all JS frameworks could use, but I'm skeptical that that's feasible.

from django-tables2.

christhekeele avatar christhekeele commented on May 18, 2024

All good points. I've come up with about three versions now that are all entirely hacks, some more in js and some not, some working more that others. Nothing that stays faithful to the purpose and format of django-tables2, and nothing I really want to use myself.

from django-tables2.

christhekeele avatar christhekeele commented on May 18, 2024

My hope was to expose the Paginator that gets used, use another paginator from some plugin that extends django's and supports pagination (like those of endless_paginator), and import some functionality over in the right places from that plugin. Doing it in a generalized fashion would've made it easily compatible with a variety of plugins. But there aren't really any good points of entry someone of my skill level could find.

from django-tables2.

mwhudson avatar mwhudson commented on May 18, 2024

Thinking about things a little, there isn't that much that's datatables specific in my code

  1. there's the way the request arguments are interpreted to filter and sort the data
  2. there is the code to generate the js to call $.datatable on the table
  3. I use the same language as datatables for configuring the table (e.g. the initial sort)

1 & 2 can clearly be hidden inside the implementation, but 3 would be a bit more problematic.

from django-tables2.

jproffitt avatar jproffitt commented on May 18, 2024

What is the status on this? I'd be glad to help work on this. Should we use datatables? What about about the api part?

from django-tables2.

jproffitt avatar jproffitt commented on May 18, 2024

What about a mixing for a view that would handle the ajax requests. Something like this:

class AjaxTableMixin(SingleTableMixin):
    pagination = {"per_page": 20}

    def dispatch(self, request, *args, **kwargs):
        if request.META.get('HTTP_ACCEPT') == 'application/json':
            return HttpResponse(self.get_table().as_json(), content_type='application/json')
        else:
            return super(AjaxTableMixin, self).dispatch(request, *args, **kwargs)

There would be an additional method on the table class, as_json(). Something like this:

def as_json(self):
    list = []
    for row in self.page.object_list:
        row_dict = {}
        for column, cell in row.items():
            row_dict[column.name] = cell
        list.append(row_dict)
    import json
    return json.dumps(list)

Then do magic in javascript to replace the rows. The only thing I'm not sure about is what to do about custom table templates. Especially when it comes to the pagination part of the template.

from django-tables2.

jieter avatar jieter commented on May 18, 2024

If someone wants to provide a nice implementation for AJAX tables, I might consider it. However, I think that it's quite complex to have both server- and client-side rendering of the table in a single code base.

So for now, I say no. Please feel free to open a new issue/PR suggesting how to implement this concisely.

from django-tables2.

addisonklinke avatar addisonklinke commented on May 18, 2024

@jproffitt Is your mixin solution ready to go with the current Django Tables2 release? It sounds like there are still some missing JS pieces to get it working

from django-tables2.

Related Issues (20)

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.