Javascript split function to add data to table

I want to add data to a table from a split function. But the problem is that the data I entered was displayed in the rows set in coloumns. Can someone help me solve the problem?

enter image description here

Html body

<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>

this is a function

<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>
+4
source share
3 answers

This will be done:

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(','));
}
+2
source

I did not have time to check it, but from what I see, you insert a line each time. "theTable.insertRow", and then you insert the cell in the new line "newRow.insertCell".

This means that every time you create a new row with only one cell. This means a vertical display.

.

- :

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);
        }
    }
}

:)

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
}

You probably need one line for inputand a few cellsfor elements separated by commas ininput

0
source

Source: https://habr.com/ru/post/1616038/


All Articles