|
| 1 | +{% extends 'base.html' %} |
| 2 | +{% load i18n %} |
| 3 | + |
| 4 | +{% block page-title %}{% trans 'Side filters' %}{% endblock page-title %} |
| 5 | + |
| 6 | +{% block extrastyle %} |
| 7 | + <style> |
| 8 | + pre { font-size: 11px; font-family: monospace; background-color: #fff; padding: 10px 0 0 10px;} |
| 9 | + input[type="number"] { width: 100px; } |
| 10 | + .filters { |
| 11 | + border: 1px solid #999; |
| 12 | + padding-top: 10px; |
| 13 | + } |
| 14 | + </style> |
| 15 | +{% endblock extrastyle %} |
| 16 | + |
| 17 | + |
| 18 | +{% block content %} |
| 19 | + |
| 20 | + <p>Example with Side filters</p> |
| 21 | + |
| 22 | + <p> |
| 23 | + Sometimes you need to provide to the user more control over the queryset |
| 24 | + used to populate the table, using interactive and specific filters. |
| 25 | + </p> |
| 26 | + |
| 27 | + <p> |
| 28 | + In those cases, you can easily add a sidebar with the required widgets, |
| 29 | + then collect and apply user selections at every table refresh. |
| 30 | + </p> |
| 31 | + |
| 32 | + <h3>How it works</h3> |
| 33 | + |
| 34 | + <p> |
| 35 | + First, add to the template all required input widgets, and schedule a table redraw |
| 36 | + whenever a filter has been changed (or when you believe is more appropriate) |
| 37 | + </p> |
| 38 | + |
| 39 | + <pre> |
| 40 | +<div class="col-sm-2 filters"> |
| 41 | + <h4>Filters</h4> |
| 42 | + <label for="from_year">From year:</label><input type="number" id="from_year"> |
| 43 | + <label for="to_year">To year:</label><input type="number" id="to_year"> |
| 44 | + ... |
| 45 | + </pre> |
| 46 | + |
| 47 | + <pre> |
| 48 | +$(document).ready(function() { |
| 49 | + |
| 50 | + $('.filters input').on('change paste keyup', function() { |
| 51 | + // redraw the table |
| 52 | + $('#datatable').DataTable().ajax.reload(null, false); |
| 53 | + }); |
| 54 | + |
| 55 | +}); |
| 56 | + </pre> |
| 57 | + |
| 58 | + <p> |
| 59 | + At table initialization, register in the "extra data" section the callbacks needed |
| 60 | + to collect user selections |
| 61 | + </p> |
| 62 | + |
| 63 | + <pre> |
| 64 | +$(document).ready(function() { |
| 65 | + |
| 66 | + AjaxDatatableViewUtils.initialize_table( |
| 67 | + ... |
| 68 | + }, { |
| 69 | + // extra_data |
| 70 | + from_year: function() { return $('input#from_year').val() }, |
| 71 | + to_year: function() { return $('input#to_year').val() }, |
| 72 | + check_year_null: function() { return $("input[name='check_year_null']:checked").val() } |
| 73 | + }, |
| 74 | + ); |
| 75 | + |
| 76 | +}); |
| 77 | + </pre> |
| 78 | + |
| 79 | + <p> |
| 80 | + Those "extra data" values will be sent to the AjaxDatatableView with |
| 81 | + every request; you can take advantage of those by filtering the queryset |
| 82 | + in the `get_initial_queryset()` override. |
| 83 | + </p> |
| 84 | + |
| 85 | + <pre> |
| 86 | +class AlbumAjaxDatatableView(AjaxDatatableView): |
| 87 | + |
| 88 | + ... |
| 89 | + |
| 90 | + def get_initial_queryset(self, request=None): |
| 91 | + |
| 92 | + def get_numeric_param(key): |
| 93 | + try: |
| 94 | + value = int(request.POST.get(key)) |
| 95 | + except: |
| 96 | + value = None |
| 97 | + return value |
| 98 | + |
| 99 | + queryset = super().get_initial_queryset(request=request) |
| 100 | + |
| 101 | + check_year_null = get_numeric_param('check_year_null') |
| 102 | + if check_year_null is not None: |
| 103 | + if check_year_null == 0: |
| 104 | + queryset = queryset.filter(year=None) |
| 105 | + elif check_year_null == 1: |
| 106 | + queryset = queryset.exclude(year=None) |
| 107 | + |
| 108 | + from_year = get_numeric_param('from_year') |
| 109 | + if from_year is not None: |
| 110 | + queryset = queryset.filter(year__gte=from_year) |
| 111 | + |
| 112 | + to_year = get_numeric_param('to_year') |
| 113 | + if to_year is not None: |
| 114 | + queryset = queryset.filter(year__lte=to_year) |
| 115 | + |
| 116 | + return queryset |
| 117 | + </pre> |
| 118 | + |
| 119 | + <div class="container"> |
| 120 | + <div class="row"> |
| 121 | + <div class="col-sm-2 filters"> |
| 122 | + <h4>Filters</h4> |
| 123 | + <label for="from_year">From year:</label><input type="number" id="from_year"> |
| 124 | + <label for="to_year">To year:</label><input type="number" id="to_year"> |
| 125 | + <br /> |
| 126 | + <br /> |
| 127 | + <label>Provided:</label><br /> |
| 128 | + <input type="radio" id="year_null" name="check_year_null" value="0"> |
| 129 | + <label for="year_null">year is NULL</label><br> |
| 130 | + <input type="radio" id="year_not_null" name="check_year_null" value="1"> |
| 131 | + <label for="year_not_null">year is not NULL</label><br> |
| 132 | + <input type="radio" id="year_any" name="check_year_null" value="-1"> |
| 133 | + <label for="year_any">any</label> |
| 134 | + </div> |
| 135 | + <div class="col-sm-10"> |
| 136 | + <div class="table-responsive"> |
| 137 | + <table id="datatable" width="100%" class="table table-striped table-bordered dt-responsive compact nowrap"> |
| 138 | + </table> |
| 139 | + </div> |
| 140 | + </div> |
| 141 | + </div> |
| 142 | + </div> |
| 143 | + |
| 144 | + |
| 145 | +{% endblock content %} |
| 146 | + |
| 147 | + |
| 148 | +{% block extrajs %} |
| 149 | + |
| 150 | + {{ block.super }} |
| 151 | + <script language="javascript"> |
| 152 | + |
| 153 | + $(document).ready(function() { |
| 154 | + |
| 155 | + AjaxDatatableViewUtils.initialize_table( |
| 156 | + $('#datatable'), |
| 157 | + "{% url 'frontend:ajax_datatable_album' %}", |
| 158 | + { |
| 159 | + // extra_options (example) |
| 160 | + processing: false, |
| 161 | + autoWidth: false, |
| 162 | + full_row_select: true, |
| 163 | + scrollX: false |
| 164 | + }, { |
| 165 | + // extra_data |
| 166 | + // ... |
| 167 | + from_year: function() { return $('input#from_year').val() }, |
| 168 | + to_year: function() { return $('input#to_year').val() }, |
| 169 | + check_year_null: function() { return $("input[name='check_year_null']:checked").val() } |
| 170 | + }, |
| 171 | + ); |
| 172 | + |
| 173 | + $('.filters input').on('change paste keyup', function() { |
| 174 | + // redraw the table |
| 175 | + $('#datatable').DataTable().ajax.reload(null, false); |
| 176 | + }); |
| 177 | + }); |
| 178 | + |
| 179 | + </script> |
| 180 | +{% endblock %} |
| 181 | + |
0 commit comments