diff --git a/RIGS/static/js/interaction.js b/RIGS/static/js/interaction.js index 0e0fac88..6b7eff6f 100644 --- a/RIGS/static/js/interaction.js +++ b/RIGS/static/js/interaction.js @@ -3,10 +3,20 @@ function setupItemTable(items_json) { $.each(objectitems, function(key, val) { objectitems[key] = JSON.parse(val); }) + newitem = -1 } function updatePrices() { + // individual rows + $('.item_row').each(function() { + var pk = $(this).data('pk'); + var fields = objectitems[pk].fields; + var sub = fields.cost * fields.quantity; + $('#item-'+pk+' .sub-total').html(parseFloat(sub).toFixed(2)).data('subtotal', sub); + }) + var sum = 0; + $('.sub-total').each(function() { sum += Number($(this).data('subtotal')); }); @@ -16,18 +26,6 @@ function updatePrices() { $('#total').text(parseFloat(sum+vat).toFixed(2)); } -function updateItemRow(pk) { - $row = $('#item-'+pk) - url = $row.data('url'); - $.ajax({ - url:url, - success:function(r) { - $row.replaceWith(r); - updatePrices(); - } - }) -} - function addItemRow(url) { $tbody = $('#item-table tbody'); $.ajax({ @@ -54,63 +52,65 @@ $.ajaxSetup({ }); $('#item-table').on('click', '.item-delete', function() { - var row = $('#item-'+$(this).data('pk')); - $.ajax({ - type:"POST", - url:$(this).data('url'), - success:function(r) { - row.remove(); - updatePrices(); - } - }); + delete objectitems[$(this).data('pk')] + $('#item-'+$(this).data('pk')).remove(); + updatePrices(); }); $('#item-table').on('click', '.item-add', function() { - $.ajax({ - url:$(this).data('url'), - success:function(r) { - $('#itemModal .modal-content').html(r); - $('#item-table tbody').sortable('refresh'); - } - }); + $('#item-form').data('pk', newitem--); + + // Set the form values + var fields = objectitems[pk].fields; + $('#item_name').val(''); + $('#item_description').val(''); + $('#item_quantity').val(''); + $('#item_cost').val(''); }); $('#item-table').on('click', '.item-edit', function() { - var url = $(this).data('url'); - $('#itemModal').data('pk', $(this).data('pk')); - $.ajax({ - url:url, - success:function(r) { - $('#itemModal .modal-content').html(r); - } - }) + // set the pk as we will need this later + var pk = $(this).data('pk'); + $('#item-form').data('pk', pk); + + // Set the form values + var fields = objectitems[pk].fields; + $('#item_name').val(fields.name); + $('#item_description').val(fields.description); + $('#item_quantity').val(fields.quantity); + $('#item_cost').val(fields.cost); }); -$('#itemModal').on('hidden.bs.modal', function() { - pk = $(this).data('pk'); - updateItemRow(pk); - $('#itemModal .modal-content').html(''); -}) - -$('body').on('submit','.item-form', function(e) { +$('body').on('submit','#item-form', function(e) { e.preventDefault(); - url = $(this).data('url'); - data = $(this).serialize(); - $.ajax({ - type:'POST', - url:url, - data:data, - success: function(r) { - $('#itemModal .modal-content').html(r) - } - }) -}) + var pk = $(this).data('pk'); + $('#itemModal').modal('hide'); -function addItem(url) { - $.get(url, function(r) { - $('#item-table tbody').append(r); - }); -} + if(pk < 0) { + // @todo: Add new item + } else { + // update data structure + var fields = objectitems[pk].fields; + fields.name = $('#item_name').val() + fields.description = $('#item_description').val(); + fields.cost = $('#item_cost').val(); + fields.quantity = $('#item_quantity').val(); + objectitems[pk].fields = fields; + + // update the table + $row = $('#item-'+pk); + $row.find('.name').html(fields.name); + $row.find('.description').html(fields.description); + $row.find('.cost').html(parseFloat(fields.cost).toFixed(2)); + $row.find('.quantity').html(fields.quantity); + + updatePrices(); + } +}); + +$('body').on('submit', '.itemised_form', function(e) { + $('#id_items_json').val(JSON.stringify(objectitems)); +}); // Return a helper with preserved width of cells var fixHelper = function(e, ui) { @@ -127,7 +127,7 @@ $("#item-table tbody").sortable({ itemorder = new Array(); $.each(info, function(key, value) { pk = $('#'+value).data('pk'); - itemorder[key] = pk; + objectitems[pk].fields.order = key; }); } diff --git a/RIGS/templates/RIGS/event_form.html b/RIGS/templates/RIGS/event_form.html index 532ec8cc..7f532dbb 100644 --- a/RIGS/templates/RIGS/event_form.html +++ b/RIGS/templates/RIGS/event_form.html @@ -62,7 +62,7 @@ {% endif %} }) - $(document).ready(function() { + $(document).ready(function () { setupItemTable($("#{{ form.items_json.id_for_label }}").val()); }); @@ -77,9 +77,10 @@ {% endif %} {% include 'form_errors.html' %} -