Modal for company create

- Ajax modals now return the URL of the item they are operating on
- passing {follow: true} to the modal caller will go to that URL on success
- footable'd company list
This commit is contained in:
Oliver 2018-04-27 21:58:22 +10:00
parent c4e305bde6
commit 36cf946a78
5 changed files with 52 additions and 23 deletions

View File

@ -54,6 +54,8 @@ class AjaxCreateView(AjaxView, CreateView):
# Return the PK of the newly-created object
data['pk'] = obj.pk
data['url'] = obj.get_absolute_url()
return self.renderJsonResponse(request, form, data)
else:
@ -84,6 +86,9 @@ class AjaxUpdateView(AjaxView, UpdateView):
if form.is_valid():
obj = form.save()
data['pk'] = obj.id
data['url'] = obj.get_absolute_url()
return self.renderJsonResponse(request, form, data)
else:

View File

@ -34,12 +34,7 @@ class EditCompanyForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(EditCompanyForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_id = 'id-edit-part-form'
self.helper.form_class = 'blueForms'
self.helper.form_method = 'post'
self.helper.add_input(Submit('submit', 'Submit'))
self.helper.form_tag = False
class Meta:
model = Company

View File

@ -6,23 +6,30 @@
<h3>Companies</h3>
<div class="container">
<div class='input-group'>
<input class="form-control" id="company-filter" type="text" placeholder="Search...">
<span class='input-group-btn'>
<button type='button' class='btn' id='clear-filter'>Clear</button>
</span>
</div>
<hr>
<ul class='list-group' id="company-list">
</ul>
</div>
<table class='table table-striped' id='company-table' data-sorting='true' data-filtering='true'>
<thead>
<tr>
<th>Name</th>
<th>Description</th>
</tr>
</thead>
<tbody>
{% for company in companies %}
<tr>
<td>
<a href="{% url 'company-detail' company.id %}">
{{ company.name }}
</a>
</td>
<td>{{ company.description }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<div class='container-fluid'>
<a href="{% url 'company-create' %}">
<button class="btn btn-success">New Company</button>
</a>
<button class="btn btn-success" id='new-company'>New Company</button>
</div>
{% include 'modals.html' %}
@ -31,8 +38,19 @@
{% block javascript %}
<script type="text/javascript" src="{% static 'script/delay.js' %}"></script>
<script type="text/javascript" src="{% static 'script/filter_company.js' %}">
<script type='text/javascript' src="{% static 'script/footable.js' %}"></script>
<script type='text/javascript' src="{% static 'script/modal_form.js' %}"></script>
<script type='text/javascript'>
$('#company-table').footable();
$('#new-company').click(function () {
launchModalForm('#modal-form',
"{% url 'company-create' %}",
{
follow: true
});
});
</script>
{% endblock %}

View File

@ -6,6 +6,8 @@ from django.http import HttpResponseRedirect
from django.views.generic import DetailView, ListView
from django.views.generic.edit import UpdateView, DeleteView, CreateView
from InvenTree.views import AjaxCreateView, AjaxUpdateView, AjaxDeleteView
from .models import Company
from .forms import EditCompanyForm
@ -43,11 +45,13 @@ class CompanyEdit(UpdateView):
context_object_name = 'company'
class CompanyCreate(CreateView):
class CompanyCreate(AjaxCreateView):
model = Company
context_object_name = 'company'
form_class = EditCompanyForm
template_name = "company/create.html"
ajax_template_name = 'modal_form.html'
ajax_form_title = "Create new Company"
class CompanyDelete(DeleteView):

View File

@ -129,12 +129,19 @@ function launchModalForm(modal, url, options = {}) {
$(modal).modal('hide');
// Form success callback
if (options.success) {
options.success();
}
// Follow the URL returned by the JSON response
else if (options.follow && response.url) {
window.location.href = response.url;
}
// Redirect to a specific URL
else if (options.redirect) {
window.location.href = options.redirect;
}
// Reload the current page
else if (options.reload) {
location.reload();
}