jquery - Javascript - delete table row that also recalculates totals -


i have table allows users make selections , enter numbers, used calculate totals automatically. i've added functionality add additional rows table , allow users delete row if required.

i need delete button force recalculation of totals @ same time, whereas @ present it's delete row , leaving original totals incorrect.

here's script delete table row:

$(window).load(function(){ //sum table cell , map   $('#lastyear')     .on('change', 'select', calc)     .on('keyup', 'input', calc);  function calc(){      $('#lastyear tr:has(.risk)').each(function(i,v){          var $cel = $(v.cells);          var $risk = $cel.eq(1).find('option:selected').val();         var $numb = $cel.eq(2).find('input').val();         var $weeks = $cel.eq(3).find('input').val();         var $avg = ($numb * $weeks) / 52;         var $avgrounded = math.round( $avg * 10 ) / 10;          $cel.eq(4).find('input').val($avgrounded);      });         var tot = {high:0,moderate:0};       $('#lastyear tr:has(.risk) option:selected')                 .map(function(i){                     var el = $(this).val();                     var qty = parsefloat( $('#lastyear tr:has(.risk)').eq(i).find('td:last')     .prev().prev() // call prev method twice     .find('input').val() );                      if (!tot.hasownproperty(el)) {                         tot[el] = 0;                                 }                     tot[el] += qty                     return tot;                 }).get();      // console.log(tot);      $('#textfield4').val(tot.moderate.tofixed(1));     $('#textfield5').val(tot.high.tofixed(1));      } });//]]>   

and here's script calculations:

$('#lastyear').on('click', '.delbtn', function () {     $(this).closest('tr').remove() });  var newidsuffix = 2; $('#lastyear').on('click', '.addbtn', function () {     var thisrow = $(this).closest('tr')[0];     $(thisrow).find('.delbtn').show();      var cloned = $(thisrow).clone();     cloned.find('input, select').each(function () {         var id = $(this).attr('id');         id = id.substring(0, id.length - 1) + newidsuffix;         $(this).attr('id', id);     });      cloned.insertafter(thisrow).find('input:text').val('');     cloned.find('.delbtn').hide();     cloned.find("[id^=lastyearselect]")     .autocomplete({         source: availabletags,         select: function (e, ui) {              $(e.target).val(ui.item.value);             setdropdown.call($(e.target));         }     }).change(setdropdown);      $(this).remove();     newidsuffix++; }); 

i've setup working jsfiddle might explain what's going on.

i'm new javascript me looks somehow need combine delete function calculate function somehow?

you need call calc() in .delbtn click handler. calc function , delete button click handler in different contexts @ moment ... if in same scope (move them same file , same onload block), you'd fine.

i've updated jsfiddle demonstrate http://jsfiddle.net/6kpxq/2/


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -