Skip to content

Commit

Permalink
Fix for pagination style and xeditable choice fields #252 (#253)
Browse files Browse the repository at this point in the history
* Bootstrap structure has been improved

* Filter xeditable choices for Foreignkey field with limit_choices_to

* Add textarea to XEDITABLE_FIELD_TYPES

* Compatibility fix for >= Django 3.1 with regard to
ModelChoiceIteratorValue for choice fields with foreign key

---------

Co-authored-by: kvdogan <[email protected]>
Co-authored-by: kvdogan <[email protected]>
  • Loading branch information
3 people authored Nov 11, 2024
1 parent 2c7f512 commit 80f8f91
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
23 changes: 22 additions & 1 deletion datatableview/templates/datatableview/bootstrap_structure.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
<link href="//cdn.datatables.net/plug-ins/be7019ee387/integration/bootstrap/3/dataTables.bootstrap.css" rel="stylesheet" />
<script src="//cdn.datatables.net/plug-ins/be7019ee387/integration/bootstrap/3/dataTables.bootstrap.js"></script>

<style>

.dataTables_wrapper .dataTables_paginate .paginate_button {
box-sizing: border-box;
display: inline-block;
min-width: 0.5em;
padding: 0.1em 0.1em;
margin: 0.5px;
text-align: center;
text-decoration: none !important;
cursor: pointer;
color: #333 !important;
border: 1px solid transparent;
border-radius: 2px;
}

</style>

<table class="display datatable table table-striped table-bordered" data-source-url="{{ url }}" id="{{ config.id }}"
data-ajax-method="{{ config.request_method }}"
data-result-counter-id="{{ config.result_counter_id }}"
Expand All @@ -13,7 +34,7 @@
<tfoot>
<tr>
{% for column in datatable %}
<th>{{ column.label }}</th>
<th data-name="{{ column.label|slugify }}">{{ column.label }}</th>
{% endfor %}
</tr>
</tfoot>
Expand Down
2 changes: 1 addition & 1 deletion datatableview/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"PositiveSmallIntegerField": "number",
"SlugField": "text",
"SmallIntegerField": "number",
"TextField": "text",
"TextField": "textarea",
"TimeField": "text",
"ForeignKey": "select",
}
Expand Down
13 changes: 12 additions & 1 deletion datatableview/views/xeditable.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,18 @@ def _get_foreignkey_choices(self, field, field_name):
# will consequently try to assume initial=None, forcing the blank option to appear.
formfield_kwargs["empty_label"] = None
formfield = field.formfield(**formfield_kwargs)
return formfield.choices

# In case of using Foreignkey limit_choices_to, django prepares model form and handles
# form validation correctly, so does django-datatableview with x-editable plugin. However
# this piece of code helps filtering limited choices to be only visible choices,else
# all the choices are visible.
if formfield.limit_choices_to:
formfield.queryset = formfield.queryset.filter(**formfield.limit_choices_to)

# return formfield.choices
# formfield choices deconstructed to get ModelChoiceIteratorValue correctly (>= Django 3.1)
# https://docs.djangoproject.com/en/3.2/ref/forms/fields/#django.forms.ModelChoiceIteratorValue.value
return [(i[0].__str__(), i[1]) for i in formfield.choices]

def _get_default_choices(self, field, field_name):
return field.choices
Expand Down

0 comments on commit 80f8f91

Please sign in to comment.