python - Update table html in django -
i new django , little confused trying update html table populated database. need update table selecting different values drop-down list (year, month , provider_type).
this table.py:
import django_tables2 tables .models import proveedor, estado class proveedortable(tables.table): class meta: model = proveedor fields = ("id_provider", "name", "type", "year", "month") sequence = ("id_provider", "name", "type", "year", "month")
my views.py
django.shortcuts import render, render_to_response, requestcontext, httpresponseredirect django_tables2 import requestconfig .tables import proveedortable .forms import provform .forms import estadoform .models import proveedor django.contrib import messages def home(request): table = proveedortable(proveedor.objects.all()) requestconfig(request).configure(table) return render(request,'index.html',{'table': table})
my template index.html
{% load querystring django_tables2 %} {% load trans blocktrans i18n %} {% load bootstrap_toolkit %} {% if table.page %} <div class="table-container"> {% endif %} {% block table %} <table class="table table-striped table-condensed table-bordered"{% if table.attrs %} {{ table.attrs.as_html }}{% endif %}> {% block table.thead %} <thead> <tr> {% column in table.columns %} {% if column.orderable %} <th {{ column.attrs.th.as_html }}><a href="{% querystring table.prefixed_order_by_field=column.order_by_alias.next %}">{{ column.header }}</a></th> {% else %} <th {{ column.attrs.th.as_html }}>{{ column.header }}</th> {% endif %} {% endfor %} </tr> </thead> {% endblock table.thead %} {% block table.tbody %} <tbody> {% row in table.page.object_list|default:table.rows %} {# support pagination #} {% block table.tbody.row %} <tr class="{% cycle "odd" "even" %}"> {% column, cell in row.items %} <td {{ column.attrs.td.as_html }}>{{ cell }}</td> {% endfor %} </tr> {% endblock table.tbody.row %} {% empty %} {% if table.empty_text %} {% block table.tbody.empty_text %} <tr><td colspan="{{ table.columns|length }}">{{ table.empty_text }}</td></tr> {% endblock table.tbody.empty_text %} {% endif %} {% endfor %} </tbody> {% endblock table.tbody %} {% block table.tfoot %} <tfoot></tfoot> {% endblock table.tfoot %} </table> {% endblock table %} {% if table.page %} {% block pagination %} <ul class="pagination"> {{ table.page|pagination }} </ul> {% endblock pagination %} {% endif %}
i confused if need use choicefield or ajax function. can bring me snippet or link can have more clear process implement functionality
thanks in advance
here, <td {{ column.attrs.td.as_html }}>{{ cell }}</td>
data being used displaying data. if want ajax request here, have in cell section. example:
{% column, cell in row.items %} {% if column|stringformat:"s" == "some-string" %} <td {{ column.attrs.td.as_html }} class="ajax-request">{{ cell }}</td> <!-- or can use: <td {{ column.attrs.td.as_html }}><input class="ajax-request" value={{ cell }} type="button (or other type)"></td> choice field, need render <td {{ column.attrs.td.as_html }}><select id=".ajax-request"> {% items in cell.values %} <option value={{ items }}></option> </select></td> --> {% else %} <td {{ column.attrs.td.as_html }} class="ajax-request">{{ cell }}</td> {% endif %} {% endfor %} <script> $(document).ready(function(){ $('.ajax-request').change(function(){ var e = document.getelementbyid("select_dropdown"); var value = e.options[e.selectedindex].value; $.ajax({ url: "your-url", type: "post", // or "get" data: value, success: function(data) { alert(data.result); }}); }); </script>
Comments
Post a Comment