如何从split array函数中乘以数据

[英]How to multiplying data from split array function


i want to add data into a table from splitting a text to array. When i clicked the add button the data appear to table and automatic multiplying price and amount into coloumn total price. Anyone can help me?

我想将数据添加到表中,从而将文本拆分为数组。当我点击添加按钮时,数据显示在表格中,并自动将价格和金额乘以coloumn总价。有人可以帮帮我吗?

enter image description here

This is the html

这是html

    <div class="container">

        <div class="form" style="margin-top: 50px;">

            <div class="form">
                <div class="form-group">
                    <label for="inputEmail3">Input</label>
                    <div class="">
                        <input type="text" class="form-control" id="transaction" placeholder="Input Transaction">
                    </div>
                </div>
                <div class="form-group">
                    <div>
                        <button type="submit" class="btn btn-default" onclick="addTransaction()">Add</button>
                    </div>
                </div>
            </div>
        </div>

        <div class="row">
            <table id="table_trans" class="table table-bordered">
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Price</th>
                        <th>Amount</th>
                        <th>Total Price</th>
                    </tr>
                </thead>
                <tbody>

                </tbody>
                <tfoot>
                    <tr>
                        <td colspan="3">
                            <input type="button" value="Total Price" class="btn btn-success" id="sumTransaction()"/>
                        </td>
                        <td id="area_total"></td>
                    </tr>
                </tfoot>
            </table>
        </div>
    </div>
    <!-- /.container -->

and this is the split function

这是拆分功能

 function addRow(tags){
 var theTable = document.getElementById('table_trans').getElementsByTagName('tbody')[0];
 var newRow = theTable.insertRow(-1);
 var newCell, theText, i;
 for(i=0;i<tags.length;i++){
 newCell = newRow.insertCell(i);

 theText = document.createTextNode(tags[i]);
 newCell.appendChild(theText);
 }
 }

function addTransaction(){
var inputTags = document.getElementById('transaction').value;
addRow(inputTags.split(','));
}

1 个解决方案

#1


1  

As in this codepen, I passed the string to the addRow and split() it there, took the Price and multiplied it by the Amount to get the Total, then append the value of Total to the tags array:

在这个codepen中,我将字符串传递给addRow并将split()传递给那里,获取Price并将其乘以Amount以获得Total,然后将Total的值附加到tags数组:

function addRow(tags) {
  tags = tags.split(',');
  var total = tags[1] * tags[2];
  tags.push(total);
  var theTable = document.getElementById('table_trans').getElementsByTagName('tbody')[0];
  var newRow = theTable.insertRow(-1);
  var newCell, theText, i;
  for (i = 0; i < tags.length; i++) {
    newCell = newRow.insertCell(i);

    theText = document.createTextNode(tags[i]);
    newCell.appendChild(theText);
  }
}

function addTransaction() {
  var inputTags = document.getElementById('transaction').value;
  addRow(inputTags);
}
<div class="container">

  <div class="form" style="margin-top: 50px;">

    <div class="form">
      <div class="form-group">
        <label for="inputEmail3">Input</label>
        <div class="">
          <input type="text" class="form-control" id="transaction" placeholder="Input Transaction">
        </div>
      </div>
      <div class="form-group">
        <div>
          <button type="submit" class="btn btn-default" onclick="addTransaction()">Add</button>
        </div>
      </div>
    </div>
  </div>

  <div class="row">
    <table id="table_trans" class="table table-bordered">
      <thead>
        <tr>
          <th>Name</th>
          <th>Price</th>
          <th>Amount</th>
          <th>Total Price</th>
        </tr>
      </thead>
      <tbody>

      </tbody>
      <tfoot>
        <tr>
          <td colspan="3">
            <input type="button" value="Total Price" class="btn btn-success" id="sumTransaction()" />
          </td>
          <td id="area_total"></td>
        </tr>
      </tfoot>
    </table>
  </div>
</div>
<!-- /.container -->


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2015/11/17/8a850c09b4c34a26f17eab19f364eab7.html



 
© 2014-2018 ITdaan.com 粤ICP备14056181号