izimobil/django-rest-framework-datatables

Searching and Ordering are not working when using a "dummy" column for row details

kienerj opened this issue · 4 comments

I basically copy&pasted the example and replaced it with my own Model class. The datatable is displayed with correct data but searching and sorting doesn't work. I can see server-side the correct parameters are being sent (which field to sort or which search value was entered). However the data always returns the default order and all data.

There is no error message or anything, simply the sorting and filtering seems to be completely ignored. I've also installed django-filter and have it in the settings:

'DEFAULT_FILTER_BACKENDS': (
            'rest_framework_datatables.filters.DatatablesFilterBackend',
            'django_filters.rest_framework.DjangoFilterBackend',
 )

and when using format=json (instead of html), the filtering with DjangoFilterBackend works. Eg. it is the datatables part that somehow not applying the filtering.

I'm aware this isn't exactly very detailed. But I've tried a lot of different options and config but nothing get this to work. Any advice would be welcome.

EDIT:

With debugging I found that get_fields methods always immediately breaks as the first columns data property is empty. In my case the first column is a "dummy" column that contains an icon to expand row details like in this example. Removing that column and it works.

So the actually issue is how to include such a column?

I can fix the issue for me if I change the get_fields method check if data is empty to:

            if data == "": # null or empty string on datatables (JS) side
                field = {'searchable': False, 'orderable': False}
                fields.append(field)
                i += 1
                continue
            elif data is None or not data:
                break

This will have the convention that such a dummy column must have data set to "" (empty string) or null (as long as the column is configured on datatables it gets sent with the request and with current implementation of getter it will then be an empty string even if data is set to null).

I'm not 100% sure if this will not break something I haven't tried yet.

The dummy field needs to be added to the fields list or else it messes up indexing further down the road. But not being searchable or orderable should mean it should not affect the QuerySet at all. So I think it's fine.

Django filters integration is being implemented #72. Feel free to test/contribute to @TauPan pull request.

Django filters integration is being implemented #72. Feel free to test/contribute to @TauPan pull request.

Making my first Django app so it's safe to say I'm not yet ready to contribute anything meaningful.

@kienerj Sorry, I was in a hurry and my response was not very good, nor related to your issue.
The good news is that your solution works perfect, I've reproduced the issue and commited a fix:
49120c3 .
Thanks !