如何用jquery计算库存

kuuvgm7e  于 2023-02-18  发布在  jQuery
关注(0)|答案(3)|浏览(116)

我的英语说得不好,我有一个很大的问题,因为我不知道jquery,我想开发一个计算股票这样的:

<table id="tabla">
  <thead>
    <tr>
        <th>producto</th>
        <th>entry</th>
        <th>exit</th>
        <th>stock</th>

    </tr>
  </thead>
  <tbody>
<tr><td class="opening balance">10</td></tr>
    <tr>
        <td class="info">TP031</td>
        <td class="entry">4</td>
        <td class="exit">0</td>

        <td class="stock">14</td>
    </tr>
    <tr>
        <td class="info">SI0352</td>
        <td id="entry">0</td>
        <td class="exit">3</td>

        <td class="stock">11</td>
    </tr>
    <tr>
        <td class="info">MP4012</td>
        <td class="entry">0</td>
        <td class="exit">3</td>
        <td class="stock">9</td>
    </tr>
  </tbody>
</table>

我想用jquery来做这个。我希望它能被理解。来自智利的问候。

pgx2nnw8

pgx2nnw81#

这就是你所需要的:

// First initialize the default variables, with your opening balance
var stockCount = 0;
var updatedStock = Number($(".opening.balance").html()) == "NaN" ? 0 : Number($(".opening.balance").html());
// For each "product", calculates the stock with the previous stock
$("tr.product").each(function(index) {
  stockCount = 0;
  
  // Number is used to convert string to number, so we can calculate
  var entry = Number($("td.entry", $(this)).html());
  var exit = Number($("td.exit", $(this)).html());

  entry = entry == "NaN" ? 0 : entry;
  exit = exit == "NaN" ? 0 : exit;

  var stock = updatedStock + entry - exit;
  updatedStock = stock;

  stockCount += stock;
  $("td.stock", $(this)).html(stock);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<table id="tabla">
  <thead>
    <tr>
        <th>producto</th>
        <th>entry</th>
        <th>exit</th>
        <th>stock</th>

    </tr>
  </thead>
  <tbody>
<tr><td class="opening balance">10</td></tr>
    <tr class="product">
        <td class="info">TP031</td>
        <td class="entry">4</td>
        <td class="exit">0</td>

        <td class="stock"></td>
    </tr>
    <tr class="product">
        <td class="info">SI0352</td>
        <td class="entry">0</td>
        <td class="exit">3</td>

        <td class="stock"></td>
    </tr>
    <tr class="product">
        <td class="info">MP4012</td>
        <td class="entry">0</td>
        <td class="exit">3</td>

        <td class="stock"></td>
    </tr>
  </tbody>
</table>

注意,您已经为第二行设置了一个id。
您可以将单击事件与按钮关联以计算:)
希望我帮上忙了。
http://codepen.io/anon/pen/XXVrNd

1l5u6lss

1l5u6lss2#

<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>

<script>

    $(document).ready(function(){
        var iTotalStock = 0;
        $('#tabla tbody tr').find('td.stock').each(function () {

            iTotalStock = iTotalStock + parseInt($(this).text(), 10);

        });
        $('#total').val(iTotalStock)
    });
</script>
</head>
<body>

   <table id="tabla">
  <thead>
    <tr>
        <th>producto</th>
        <th>entry</th>
        <th>exit</th>
        <th>stock</th>

    </tr>
  </thead>
  <tbody>
<tr><td class="opening balance">10</td></tr>
    <tr>
        <td class="info">TP031</td>
        <td class="entry">4</td>
        <td class="exit">0</td>

        <td class="stock">14</td>
    </tr>
    <tr>
        <td class="info">SI0352</td>
        <td id="entry">0</td>
        <td class="exit">3</td>

        <td class="stock">11</td>
    </tr>
    <tr>
        <td class="info">MP4012</td>
        <td class="entry">0</td>
        <td class="exit">3</td>
        <td class="stock">9</td>
    </tr>
  </tbody>
</table>
    <br /><br />
      Total<input type="text" id="total"/>
</body>
</html>
lyr7nygr

lyr7nygr3#

据我所知,你需要得到股票的总数。
这可以通过添加所有股票来实现,我们需要使用jquery each
下面的代码将给出总库存计数。

var stockTotal=0;

$(function(){
    $('.stock').each(function(){
        stockTotal= stockTotal+ parseInt($(this).text());
    });
    alert(stockTotal);
});

工作Fiddle
希望这个有用。

  • 帮助:)

相关问题