Fix issues for AjaxUdpateView

- Works properly now!
- Can edit BOM item inline
- Can add BOM item inline too
This commit is contained in:
Oliver 2018-04-26 09:26:43 +10:00
parent 55e7f365df
commit e395d89a7f
7 changed files with 50 additions and 23 deletions

View File

@ -3,6 +3,7 @@ from __future__ import unicode_literals
from django.template.loader import render_to_string from django.template.loader import render_to_string
from django.http import JsonResponse from django.http import JsonResponse
from django.shortcuts import get_object_or_404
from django.views.generic import UpdateView, CreateView, DeleteView from django.views.generic import UpdateView, CreateView, DeleteView
@ -75,7 +76,7 @@ class AjaxUpdateView(AjaxView, UpdateView):
def post(self, request, *args, **kwargs): def post(self, request, *args, **kwargs):
if request.is_ajax(): if request.is_ajax():
form = self.form_class(request.POST) form = self.form_class(request.POST, instance=self.get_object())
data = {'form_valid': form.is_valid()} data = {'form_valid': form.is_valid()}
@ -85,16 +86,17 @@ class AjaxUpdateView(AjaxView, UpdateView):
return self.renderJsonResponse(request, form, data) return self.renderJsonResponse(request, form, data)
else: else:
return super(UpdateView, self).post(request, *args, **kwargs) response = super(UpdateView, self).post(request, *args, **kwargs)
return response
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
response = super(CreateView, self).get(request) response = super(UpdateView, self).get(request, *args, **kwargs)
if request.is_ajax(): if request.is_ajax():
form = self.form_class(initial=self.get_initial()) form = self.form_class(instance = self.get_object())
return self.renderJsonResponse(request, form) return self.renderJsonResponse(request, form)
else: else:
return super(UpdateView, self).get(request, *args, **kwargss) return response

View File

@ -63,10 +63,11 @@ class EditBomItemForm(forms.ModelForm):
super(EditBomItemForm, self).__init__(*args, **kwargs) super(EditBomItemForm, self).__init__(*args, **kwargs)
self.helper = FormHelper() self.helper = FormHelper()
self.helper.form_id = 'id-edit-part-form' #self.helper.form_id = 'id-edit-part-form'
self.helper.form_method = 'post' self.helper.form_method = 'post'
self.helper.add_input(Submit('submit', 'Submit')) self.helper.form_tag = False
#self.helper.add_input(Submit('submit', 'Submit'))
class Meta: class Meta:
model = BomItem model = BomItem

View File

@ -1,9 +1,16 @@
{% extends "part/part_base.html" %} {% extends "part/part_base.html" %}
{% load static %}
{% block details %} {% block details %}
{% include 'part/tabs.html' with tab='bom' %} {% include 'part/tabs.html' with tab='bom' %}
<div class='modal fade' id='modal-form'>
<div class='modal-dialog'>
<div class='modal-content'>
</div>
</div>
</div>
<h3>Bill of Materials</h3> <h3>Bill of Materials</h3>
<table class="table table-striped"> <table class="table table-striped">
@ -18,9 +25,9 @@
<tr> <tr>
<td><a href="{% url 'part-detail' sub_part.id %}">{{ sub_part.name }}</a></td> <td><a href="{% url 'part-detail' sub_part.id %}">{{ sub_part.name }}</a></td>
<td>{{ sub_part.description }}</td> <td>{{ sub_part.description }}</td>
<td>{{ bom_item.quantity }}</span></td> <td>{{ bom_item.quantity }}</td>
<td> <td>
<button type='button' class='btn btn-success'>Edit</button> <button type='button' url="{% url 'bom-item-edit' bom_item.id %}" class='btn btn-success edit-row-button'>Edit</button>
</td> </td>
</tr> </tr>
{% endwith %} {% endwith %}
@ -28,9 +35,7 @@
</table> </table>
<div class='container-fluid'> <div class='container-fluid'>
<a href="{% url 'bom-item-create' %}?parent={{ part.id }}"> <button type='button' class='btn btn-success' id='new-bom-item'>Add BOM Item</button>
<button class='btn btn-success'>Add BOM Item</button>
</a>
</div> </div>
{% endblock %} {% endblock %}
@ -39,4 +44,19 @@
<script type='text/javascript' src="{% static 'script/modal_form.js' %}"></script> <script type='text/javascript' src="{% static 'script/modal_form.js' %}"></script>
<script type='text/javascript'>
$(".edit-row-button").click(function () {
var button = $(this);
launchModalForm("#modal-form",
button.attr('url'));
});
$("#new-bom-item").click(function () {
launchModalForm("#modal-form",
"{% url 'bom-item-create' %}/?parent={{ part.id }}");
});
</script>
{% endblock %} {% endblock %}

View File

@ -54,10 +54,12 @@
<script type='text/javascript'> <script type='text/javascript'>
bindModalForm('#modal-cat', $(".js-create-cat").click(function() {
'.js-create-cat', launchModalForm("#modal-cat",
"{% url 'category-create' %}", "{% url 'category-create' %}",
{category: {{ category.id }} }); {category: {{ category.id }} });
});
</script> </script>

View File

@ -40,8 +40,6 @@ $(document).ready(function () {
$(".js-create-cat").click(function() { $(".js-create-cat").click(function() {
launchModalForm("#modal-cat", "{% url 'category-create' %}"); launchModalForm("#modal-cat", "{% url 'category-create' %}");
}); });
//bindModalForm('#modal-cat', '.js-create-cat', "{% url 'category-create' %}");
}); });
</script> </script>

View File

@ -19,7 +19,7 @@ from .forms import EditBomItemForm
from .forms import EditSupplierPartForm from .forms import EditSupplierPartForm
from InvenTree.views import AjaxCreateView from InvenTree.views import AjaxCreateView, AjaxUpdateView
class PartIndex(ListView): class PartIndex(ListView):
model = Part model = Part
@ -168,10 +168,12 @@ class BomItemDetail(DetailView):
template_name = 'part/bom-detail.html' template_name = 'part/bom-detail.html'
class BomItemCreate(CreateView): class BomItemCreate(AjaxCreateView):
model = BomItem model = BomItem
form_class = EditBomItemForm form_class = EditBomItemForm
template_name = 'part/bom-create.html' template_name = 'part/bom-create.html'
ajax_template_name = 'modal_form.html'
ajax_form_title = 'Create BOM item'
def get_initial(self): def get_initial(self):
# Look for initial values # Look for initial values
@ -186,10 +188,12 @@ class BomItemCreate(CreateView):
return initials return initials
class BomItemEdit(UpdateView): class BomItemEdit(AjaxUpdateView):
model = BomItem model = BomItem
form_class = EditBomItemForm form_class = EditBomItemForm
template_name = 'part/bom-edit.html' template_name = 'part/bom-edit.html'
ajax_template_name = 'modal_form.html'
ajax_form_title = 'Edit BOM item'
class BomItemDelete(DeleteView): class BomItemDelete(DeleteView):

View File

@ -26,7 +26,7 @@ function launchModalForm(modal, url, data) {
var form = $(this); var form = $(this);
$.ajax({ $.ajax({
url: form.attr('action'), url: url,
data: form.serialize(), data: form.serialize(),
type: form.attr('method'), type: form.attr('method'),
dataType: 'json', dataType: 'json',