mirror of
https://github.com/inventree/InvenTree.git
synced 2025-04-29 20:16:44 +00:00
Added AjaxUpdateView class
Also cleaned up the modal form javascript
This commit is contained in:
parent
99743c6bd0
commit
55e7f365df
@ -39,7 +39,7 @@ class AjaxView(object):
|
|||||||
|
|
||||||
class AjaxCreateView(AjaxView, CreateView):
|
class AjaxCreateView(AjaxView, CreateView):
|
||||||
|
|
||||||
def post(self, request):
|
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)
|
||||||
@ -55,9 +55,39 @@ class AjaxCreateView(AjaxView, CreateView):
|
|||||||
return self.renderJsonResponse(request, form, data)
|
return self.renderJsonResponse(request, form, data)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
return super(CreateView, self).post(request)
|
return super(CreateView, self).post(request, *args, **kwargs)
|
||||||
|
|
||||||
def get(self, request):
|
def get(self, request, *args, **kwargs):
|
||||||
|
|
||||||
|
response = super(CreateView, self).get(request, *args, **kwargs)
|
||||||
|
|
||||||
|
if request.is_ajax():
|
||||||
|
form = self.form_class(initial=self.get_initial())
|
||||||
|
|
||||||
|
return self.renderJsonResponse(request, form)
|
||||||
|
|
||||||
|
else:
|
||||||
|
return response
|
||||||
|
|
||||||
|
|
||||||
|
class AjaxUpdateView(AjaxView, UpdateView):
|
||||||
|
|
||||||
|
def post(self, request, *args, **kwargs):
|
||||||
|
|
||||||
|
if request.is_ajax():
|
||||||
|
form = self.form_class(request.POST)
|
||||||
|
|
||||||
|
data = {'form_valid': form.is_valid()}
|
||||||
|
|
||||||
|
if form.is_valid():
|
||||||
|
obj = form.save()
|
||||||
|
|
||||||
|
return self.renderJsonResponse(request, form, data)
|
||||||
|
|
||||||
|
else:
|
||||||
|
return super(UpdateView, self).post(request, *args, **kwargs)
|
||||||
|
|
||||||
|
def get(self, request, *args, **kwargs):
|
||||||
|
|
||||||
response = super(CreateView, self).get(request)
|
response = super(CreateView, self).get(request)
|
||||||
|
|
||||||
@ -67,4 +97,4 @@ class AjaxCreateView(AjaxView, CreateView):
|
|||||||
return self.renderJsonResponse(request, form)
|
return self.renderJsonResponse(request, form)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
return response
|
return super(UpdateView, self).get(request, *args, **kwargss)
|
||||||
|
@ -19,7 +19,9 @@
|
|||||||
<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 }}</span></td>
|
||||||
<td><a href="{% url 'bom-item-detail' bom_item.id %}">Edit</a></td>
|
<td>
|
||||||
|
<button type='button' class='btn btn-success'>Edit</button>
|
||||||
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
{% endwith %}
|
{% endwith %}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
@ -31,4 +33,10 @@
|
|||||||
</a>
|
</a>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{% endblock %}
|
||||||
|
|
||||||
|
{% block javascript %}
|
||||||
|
|
||||||
|
<script type='text/javascript' src="{% static 'script/modal_form.js' %}"></script>
|
||||||
|
|
||||||
{% endblock %}
|
{% endblock %}
|
@ -37,7 +37,11 @@
|
|||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
$(document).ready(function () {
|
$(document).ready(function () {
|
||||||
|
|
||||||
bindModalForm('#modal-cat', '.js-create-cat', "{% url 'category-create' %}");
|
$(".js-create-cat").click(function() {
|
||||||
|
launchModalForm("#modal-cat", "{% url 'category-create' %}");
|
||||||
|
});
|
||||||
|
|
||||||
|
//bindModalForm('#modal-cat', '.js-create-cat', "{% url 'category-create' %}");
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
@ -1,28 +1,25 @@
|
|||||||
/* Bind a button to launch a modal form and handle AJAX requests */
|
function launchModalForm(modal, url, data) {
|
||||||
function bindModalForm(modal, button, url, data) {
|
$.ajax({
|
||||||
$(button).click(function () {
|
url: url, // Where to request the data from
|
||||||
$.ajax({
|
type: 'get', // GET request
|
||||||
url: url, // Where to request the data from
|
data: data, // Any additional context data (e.g. initial values)
|
||||||
type: 'get', // GET request
|
dataType: 'json',
|
||||||
data: data, // Any additional context data (e.g. initial values)
|
beforeSend: function() {
|
||||||
dataType: 'json',
|
$(modal).modal('show');
|
||||||
beforeSend: function() {
|
},
|
||||||
$(modal).modal('show');
|
success: function(response) {
|
||||||
},
|
if (response.html_form) {
|
||||||
success: function(response) {
|
var target = modal + ' .modal-content';
|
||||||
if (response.html_form) {
|
$(target).html(response.html_form);
|
||||||
var target = modal + ' .modal-content';
|
} else {
|
||||||
$(target).html(response.html_form);
|
alert('JSON response missing form data');
|
||||||
} else {
|
|
||||||
alert('JSON response missing form data');
|
|
||||||
$(modal).modal('hide');
|
|
||||||
}
|
|
||||||
},
|
|
||||||
error: function (xhr, ajaxOptions, thrownError) {
|
|
||||||
alert('Error requesting form data:\n' + thrownError);
|
|
||||||
$(modal).modal('hide');
|
$(modal).modal('hide');
|
||||||
}
|
}
|
||||||
});
|
},
|
||||||
|
error: function (xhr, ajaxOptions, thrownError) {
|
||||||
|
alert('Error requesting form data:\n' + thrownError);
|
||||||
|
$(modal).modal('hide');
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
$(modal).on('submit', '.js-modal-form', function() {
|
$(modal).on('submit', '.js-modal-form', function() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user