Search code examples
javascriptphphtmljquery-calculation

How to customize jAutoCalc plugin


I have a simple question regarding jAutoCalc, my question can be answered in two ways:

1> How to customize the jAutoCalc plugin so that we can make it able to be used for input array names instead of just names. my code is here:

<html>
<head>
  <script type="text/javascript" src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
  <script type="text/javascript" src="http://rawgit.com/c17r/jAutoCalc/master/jAutoCalc.js"></script>
  <script type="text/javascript">
    <!--
    $(document).ready(function() {
      function autoCalcSetup() {
        $('form[name=cart]').jAutoCalc('destroy');
        $('form[name=cart] tr[name=line_items]').jAutoCalc({
          keyEventsFire: true,
          decimalPlaces: 2,
          emptyAsZero: true
        });
        $('form[name=cart]').jAutoCalc({
          decimalPlaces: 2
        });
      }
      autoCalcSetup();

      $('button[name=remove]').click(function(e) {
        e.preventDefault();

        var form = $(this).parents('form')
        $(this).parents('tr').remove();
        autoCalcSetup();
      });

      $('button[name=add]').click(function(e) {
        e.preventDefault();

        var $table = $(this).parents('table');
        var $top = $table.find('tr[name=line_items]').first();
        var $new = $top.clone(true);

        $new.jAutoCalc('destroy');
        $new.insertBefore($top);
        $new.find('input[type=text]').val('');
        autoCalcSetup();
      });
    });
  </script>
</head>
<body>
  <form name="cart">
    <table name="cart">
      <tr>
        <th></th>
        <th>Item</th>
        <th>Qty</th>
        <th>Price</th>
        <th>&nbsp;</th>
        <th>Item Total</th>
      </tr>
      <tr name="line_items">
        <td><button name="remove">Remove</button></td>
        <td>Stuff</td>
        <td><input type="text" name="qty" value="1"></td>
        <td><input type="text" name="price" value="9.99"></td>
        <td>&nbsp;</td>
        <td><input type="text" name="item_total" value="" jAutoCalc="{qty} * {price}"></td>
      </tr>
      <tr name="line_items">
        <td><button name="remove">Remove</button></td>
        <td>More Stuff</td>
        <td><input type="text" name="qty" value="2"></td>
        <td><input type="text" name="price" value="12.50"></td>
        <td>&nbsp;</td>
        <td><input type="text" name="item_total" value="" jAutoCalc="{qty} * {price}"></td>
      </tr>
      <tr name="line_items">
        <td><button name="remove">Remove</button></td>
        <td>And More Stuff</td>
        <td><input type="text" name="qty" value="3"></td>
        <td><input type="text" name="price" value="99.99"></td>
        <td>&nbsp;</td>
        <td><input type="text" name="item_total" value="" jAutoCalc="{qty} * {price}"></td>
      </tr>
      <tr>
        <td colspan="3">&nbsp;</td>
        <td>Subtotal</td>
        <td>&nbsp;</td>
        <td><input type="text" name="sub_total" value="" jAutoCalc="SUM({item_total})"></td>
      </tr>
      <tr>
        <td colspan="3">&nbsp;</td>
        <td>
          Tax:
          <select name="tax">
            <option value=".06">CT Tax</option>
            <option selected value=".00">Tax Free</option>
          </select>
        </td>
        <td>&nbsp;</td>
        <td><input type="text" name="tax_total" value="" jAutoCalc="{sub_total} * {tax}"></td>
      </tr>
      <tr>
        <td colspan="3">&nbsp;</td>
        <td>Total</td>
        <td>&nbsp;</td>
        <td><input type="text" name="grand_total" value="" jAutoCalc="{sub_total} + {tax_total}"></td>
      </tr>
      <tr>
        <td colspan="99"><button name="add">Add Row</button></td>
      </tr>
    </table>
  </form>
</body>
</html>

since the names are same for dynamically generated input's i want use input array. The problem is that if i do so then I'm unable to use the plugin features!

2> The question can be answered in second way by providing methods to use plugin while using input arrays


Solution

  • I know its an old post but maybe others may have same issue.

    Try to open jautocalc.js find this function "getFieldSelector(field)" and edit this code.

    return ':input[name="' + field + '"]';

    to

    return ':input[name="' + field + '[]"]';