在javascript中拆分函数将数据添加到表中

时间:2015-11-16 10:38:15

标签: javascript html split

我想从split函数中将数据添加到表中。但问题是,我输入的数据显示在行中,并显示为coloumns。任何人都可以帮我解决问题吗?

enter image description here

html正文

<body>
    <div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" rel="home" href="#">Web Test</a>
        </div>

        <div class="collapse navbar-collapse">

            <ul class="nav navbar-nav">
                <li><a href="#">Link</a></li>
                <li><a href="#">Link</a></li>
                <li><a href="#">Link</a></li>
            </ul>

        </div>

    </div>
    </div>

    <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" />
                        </td>
                        <td id="area_total"></td>
                    </tr>
                </tfoot>
            </table>
        </div>
    </div>
    <!-- /.container -->
    <!-- script references -->
    <script src="js/jquery.js"></script>
    <script src="js/bootstrap.min.js"></script>
</body>

这是函数

<script>
function addRow(thetext) {
    var theTable = document.getElementById('table_trans').getElementsByTagName('tbody')[0];
    var newRow = theTable.insertRow(-1); // tr
    var newCell = newRow.insertCell(0); // td

    var theText = document.createTextNode(thetext);
    newCell.appendChild(thetext);
}

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

    for (var i in tags) {
        addRow(tags[i]);
    }
}
</script>

3 个答案:

答案 0 :(得分:2)

这样做:

function addRow(tags)
{
    var theTable = document.getElementById('table_trans');
    var newRow = theTable.insertRow(-1);
    for(var i=0;i<tags.length;i++)newRow.insertCell(i).appendChild(document.createTextNode(tags[i]));
}

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

答案 1 :(得分:0)

没有时间测试它,但从我可以看到你每次都插入一行。 &#34; theTable.insertRow&#34;然后在新行&#34; newRow.insertCell&#34;中插入一个单元格。

这意味着每次创建只有一个单元格的新行时。这意味着垂直显示。

您应该仅为分割数组中的第一个元素插入一行。

这样的事情:

function addRow(thetext,isFirst) {
    var theTable = document.getElementById('table_trans').getElementsByTagName('tbody')[0];
    var newRow = null;
    if(isFirst){
        newRow = theTable.insertRow(-1); // tr
    }else{
        newRow = document.getElementById('table_trans').rows[0];
    }
    var newCell = newRow.insertCell(0); // td

    var theText = document.createTextNode(thetext);
    newCell.appendChild(thetext);
}

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

    for (var i in tags) {
        if i=0{
            addRow(tags[i],true);
        }else{
            addRow(tags[i],false);
        }
    }
}

但仍未经过测试:)

答案 2 :(得分:0)

function addRow(thetextArray) {
    var theTable = document.getElementById('table_trans').getElementsByTagName('tbody')[0];
    var newRow = theTable.insertRow(-1); // create tr
    var newCell, theText, i;
    for(i=0; i<thetextArray.length; i++){
        newCell = newRow.insertCell(); // td
        theText = document.createTextNode(thetext);
        newCell.appendChild(thetext);
    }
}

function addTransaction() {
    var inputTags = document.getElementById('transaction').value;
    addRow(inputTags.split(","));//pass the array
}

您可能需要每input行一行,而cells

中逗号分隔的每个项目多input