noirbizarre / django-eztables

Easy integration between jQuery DataTables and Django.
http://django-eztables.readthedocs.org/en/latest/
GNU Lesser General Public License v3.0
96 stars 36 forks source link

Allow callbacks and/or querysets or some other way of providing deeper data #7

Open ScottEAdams opened 11 years ago

ScottEAdams commented 11 years ago

Fantastic work and very easy to set up datatables (have it working with tabletools and editor = very nice).

However, I very quickly find myself needing/wanting to be able to pull in data using callbacks/querysets and similar. Is there anyway to do this?

noirbizarre commented 11 years ago

Thanks!!

Can you explain hat you need: an example, a code sample, or even better a pull-request if you need to add some feature!

Right now you can hook into the DatatablesView class and override the get_queryset() method (or any other).

If it's not responding to your need, I can adjust that. Just provide an example.

kmitrovic commented 9 years ago

I got something similar here: http://stackoverflow.com/questions/26338879/use-decorated-methods-property-in-datatables-fields-array-django-eztables

bernardotorres commented 9 years ago

Hi @noirbizarre and folks. This has also been a issue with me. I wanted to display data defined as @property and also transform the data to present it in a more pleasant way, with widgets, for example (in my specific case, I had a Batch object with a printProgress @property which I wanted to present as a progress bar.

So I made a patch.

This was the final result:

bildschirmfoto 2014-12-18 um 23 48 25

This is how the DataTable was defined:

class BatchDatatablesView(DatatablesView):
    model = Batch
    fields = {
        'client':'client__name',
        'start':'start',
        'quantity':'quantity',
        'credential':'credential',
        'progress':showProgress,
    }

credential is a @property of Batch.

showProgress is a callable which takes the row (Django ORM single object) and returns a string to be displayed. In this case:

def showProgress(row):
    return """<div class="progressbar_outer">
<div class="progressbar_percentage" style="width: %d%%" />
</div>""" % row.printProgress

The present patch does not enable searching from properties or callables, but I don't think it would be a good idea anyway.

I'm opening a pull-request.