使用jquery进行表计算 - 可以使用一个循环完成

时间:2018-06-07 13:21:15

标签: jquery

我可以使用此$(".weekday-am-spreadcost").each(function() {

的一个循环进行此计算

试过但不和我合作。请提出任何建议



var spreadCost = {
  run: function(flag) {

    /***** week day am rate at customer level ***/

    if (flag == 5) {

      var weekday_am_minsales_total = 0;
      var weekday_am_selling_total = 0;
      var weekday_am_profit_total = 0;

      var weekday_am_target = $(".weekday-am-target-total").val();

      $(".weekday-am-spreadcost").each(function() {

        var asset_profit = $(this).data('profit');
        var weekday_am_rate = $(this).val();
        var weekday_am_cost = $(this).data('cost');

        spreadCost.weekday_am_minsales = (((weekday_am_cost) * (asset_profit)) / 100);
        $(this).closest('tr').find("td:eq(1) .min-sale-price").val(parseFloat(spreadCost.weekday_am_minsales).toFixed(2));

        weekday_am_minsales_total += (spreadCost.weekday_am_minsales);
      });


      console.log({
        "min-sale-total": weekday_am_minsales_total
      });

      spreadCost.customer_weekday_am_difference = (weekday_am_minsales_total - weekday_am_target);

      $(".weekday-am-difference-total").empty().html(parseFloat(spreadCost.customer_weekday_am_difference).toFixed(2));

      $(".weekday-am-spreadcost").each(function() {

        var asset_profit = $(this).data('profit');
        var weekday_am_rate = $(this).val();
        var weekday_am_cost = $(this).data('cost');

        spreadCost.weekday_am_minsales = (((weekday_am_cost) * (asset_profit)) / 100);
        spreadCost.weekday_am_adjust_min_sale = ((spreadCost.weekday_am_minsales / (weekday_am_minsales_total)) * spreadCost.customer_weekday_am_difference);
        spreadCost.weekday_am_selling_price = ((spreadCost.weekday_am_minsales) - (spreadCost.weekday_am_adjust_min_sale));

        $(this).closest('tr').find("td:eq(1) .adjust-min-sales").val(parseFloat(spreadCost.weekday_am_adjust_min_sale).toFixed(2));
        $(this).closest('tr').find("td:eq(1) .selling-price").val(parseFloat(spreadCost.weekday_am_selling_price).toFixed(2));

        weekday_am_profit_total += (spreadCost.weekday_am_adjust_min_sale);
        weekday_am_selling_total += (spreadCost.weekday_am_selling_price);


      });

      console.log({
        "weekday_am_profit_total": weekday_am_profit_total,
        "weekday_am_selling_total": weekday_am_selling_total
      });

      $(".weekday-am-sellingprice-total").empty().html(parseFloat(weekday_am_selling_total).toFixed(2));

    }




  }
}


$(function() {


  $(document).on('keyup', '.weekday-am-target-total', function() {
    spreadCost.run(5);
  });


});

<link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table table-bordered table-stripped TableDataForBundle" id="TableOne" style="width: 100%">
  <thead>
    <tr>
      <th></th>
      <th colspan="1" style="text-align: center">AM</th>
    </tr>
    <tr>
      <th width="30%">Category</th>
      <th width="15%">Weekday</th>
    </tr>
  </thead>
  <tbody class="BundleDataForPrice">
    <tr>
      <td><input type="hidden" readonly="" value="3" name="asset_id[]" class="form-control"><input type="text" readonly="" value="Wheeled Midi Paver" name="asset_name[]" data-profit="125" class="form-control profit"></td>
      <td><input type="text" autocomplete="off" value="225.00" name="weekday_am[]" class="form-control weekday-am-spreadcost" data-cost="175" data-profit="125" style=""><input type="text" class="selling-price" value="" placeholder="Selling price"><input type="text"
          class="min-sale-price" placeholder="Min sale price"><input type="text" class="adjust-min-sales" value="" placeholder="Adjust min sale price"></td>

    </tr>
    <tr>
      <td><input type="hidden" readonly="" value="22" name="asset_id[]" class="form-control"><input type="text" readonly="" value="Paver Driver" name="asset_name[]" data-profit="125" class="form-control profit"></td>
      <td><input type="text" autocomplete="off" value="155.00" name="weekday_am[]" class="form-control weekday-am-spreadcost" data-cost="140" data-profit="125"><input type="text" class="selling-price" value="" placeholder="Selling price"><input type="text"
          class="min-sale-price" placeholder="Min sale price"><input type="text" class="adjust-min-sales" value="" placeholder="Adjust min sale price"></td>
    </tr>
    <tr>
      <td><input type="hidden" readonly="" value="23" name="asset_id[]" class="form-control"><input type="text" readonly="" value="Paver Screwman" name="asset_name[]" data-profit="125" class="form-control profit"></td>
      <td><input type="text" autocomplete="off" value="155.00" name="weekday_am[]" class="form-control weekday-am-spreadcost" data-cost="140" data-profit="125"><input type="text" class="selling-price" value="" placeholder="Selling price"><input type="text"
          class="min-sale-price" placeholder="Min sale price"><input type="text" class="adjust-min-sales" value="" placeholder="Adjust min sale price"></td>
    </tr>
    <tr>
      <td><input type="hidden" readonly="" value="44" name="asset_id[]" class="form-control"><input type="text" readonly="" value="Crewbus" name="asset_name[]" data-profit="125" class="form-control profit"></td>
      <td><input type="text" autocomplete="off" value="65.00" name="weekday_am[]" class="form-control weekday-am-spreadcost" data-cost="50" data-profit="125"><input type="text" class="selling-price" value="" placeholder="Selling price"><input type="text"
          class="min-sale-price" placeholder="Min sale price"><input type="text" class="adjust-min-sales" value="" placeholder="Adjust min sale price"></td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <th width="30%">Selling Price Total</th>
      <th width="15%"><span class="weekday-am-sellingprice-total">600.00</span></th>
    </tr>
    <tr>
      <th width="30%">Target</th>
      <th width="15%"><input type="text" class="form-control weekday-am-target-total" id="" name=""></th>
    </tr>
    <tr>
      <th width="30%">Difference</th>
      <th width="15%"><span class="weekday-am-difference-total"></span></th>
    </tr>
  </tfoot>
</table>
&#13;
&#13;
&#13;

0 个答案:

没有答案