morlandi / django-ajax-datatable

A Django app which provides the integration of a Django project with the jQuery Javascript library DataTables.net
MIT License
204 stars 64 forks source link

Choice option in column filter: instead of distinc values from db table only displayed data #90

Open doctsh opened 2 years ago

doctsh commented 2 years ago

Hi I have started using this wonderful package in my application. I am stuck with one place where choices are enabled and it shows models field choices or db table distinct values.
But when data is filtered for a user if we can restrict the column dropdown options to distinct values of what is being served through initial queryset will be more opt than collecting from entire db.

I know this can't be an issue and could be improvement. Considering data protection so as other users dont see full list of distinct values is important.

Is there anyway to achieve this? or am I doing anything wrong?

morlandi commented 2 years ago

Hello, you could try settings autofilter = True which does exactly what you want:

https://github.com/morlandi/django-ajax-datatable#id12

but it might be ignored when Field has chioices ... I'm not sure about this details. I'ld give it a try

doctsh commented 2 years ago

Thank you so much for prompt reply. If I remove choices then autofilter does not work (filter becomes textbox), if I include choices then autofilter takes unique db field.... my field is foreign field so i did mention this as:

        {'name': 'topic', 'visible': True, 'searchable': True, 'orderable': True, 'choices': True,
         'foreign_field': 'topic__topic', 'autofilter': True, },
morlandi commented 2 years ago

The idea behind this is that if you start to fill a brand new table, no values are available yet, thus the only chance to help the user is to show the list of choices associated with the Field. In my project I tend to use choices with a limited number of values, so it's not a big problem; when the number of choices is significant, I rather opt to have a separate "lookup" table and a FK.

I do undestand that there might be cases where this solution is not the optimal choice.

doctsh commented 2 years ago

Yes, you are right. I moved a filter out of column filtering and filter using select from shortlisted items:

image