javascript fixes

This commit is contained in:
Oliver 2021-10-14 09:14:31 +11:00
parent 0581885ad5
commit 1cbce5dfbf

View File

@ -192,30 +192,20 @@ function bomSubstitutesDialog(bom_item_id, substitutes, options={}) {
rows += renderSubstituteRow(sub);
});
var html = ``;
if (substitutes.length > 0) {
html += `
<table class='table table-striped table-condensed' id='substitute-table'>
<thead>
<tr>
<th>{% trans "Part" %}</th>
<th>{% trans "Description" %}</th>
<th><!-- Actions --></th>
</tr>
</thead>
<tbody>
${rows}
</tbody>
</table>
`;
} else {
html += `
<div class='alert alert-block alert-info'>
<em>{% trans "There are no substitue parts specified for this BOM line item" %}</em>
</div>
`;
}
var html = `
<table class='table table-striped table-condensed' id='substitute-table'>
<thead>
<tr>
<th>{% trans "Part" %}</th>
<th>{% trans "Description" %}</th>
<th><!-- Actions --></th>
</tr>
</thead>
<tbody>
${rows}
</tbody>
</table>
`;
html += `
<div class='alert alert-success alert-block'>
@ -223,6 +213,30 @@ function bomSubstitutesDialog(bom_item_id, substitutes, options={}) {
</div>
`;
// Add a callback to remove a row from the table
function addRemoveCallback(modal, element) {
$(modal).find(element).click(function() {
var pk = $(this).attr('pk');
var pre = `
<div class='alert alert-block alert-warning'>
{% trans "Are you sure you wish to remove this substitute part link?" %}
</div>
`;
constructForm(`/api/bom/substitute/${pk}/`, {
method: 'DELETE',
title: '{% trans "Remove Substitute Part" %}',
preFormContent: pre,
confirm: true,
onSuccess: function() {
$(modal).find(`#substitute-row-${pk}`).remove();
reloadParentTable();
}
});
});
}
constructForm('{% url "api-bom-substitute-list" %}', {
method: 'POST',
fields: {
@ -239,29 +253,7 @@ function bomSubstitutesDialog(bom_item_id, substitutes, options={}) {
submitText: '{% trans "Add Substitute" %}',
title: '{% trans "Edit BOM Item Substitutes" %}',
afterRender: function(fields, opts) {
// Add a callback to remove individual rows
$(opts.modal).find('.button-row-remove').click(function() {
var pk = $(this).attr('pk');
var pre = `
<div class='alert alert-block alert-warning'>
{% trans "Are you sure you wish to remove this substitute part link?" %}
</div>
`;
constructForm(`/api/bom/substitute/${pk}/`, {
method: 'DELETE',
title: '{% trans "Remove Substitute Part" %}',
preFormContent: pre,
confirm: true,
onSuccess: function() {
$(opts.modal).find(`#substitute-row-${pk}`).remove();
reloadParentTable();
}
});
});
addRemoveCallback(opts.modal, '.button-row-remove');
},
preventClose: true,
onSuccess: function(response, opts) {
@ -277,6 +269,9 @@ function bomSubstitutesDialog(bom_item_id, substitutes, options={}) {
var row = renderSubstituteRow(response);
$(opts.modal).find('#substitute-table > tbody:last-child').append(row);
// Add a callback to the new button
addRemoveCallback(opts.modal, `#button-row-remove-${response.pk}`);
// Re-enable the "submit" button
$(opts.modal).find('#modal-form-submit').prop('disabled', false);