0

I need to add data to a table from a java script. Tha table is in the same .jsp file but no inside the script.

AJAX call

<script type="text/javascript">
        function searchDetails() {

            $.ajax({
                type : "post",
                data : {
                    'accountNo' : $(accNumber).val()
                },
                url : "/banking-internet/account-history/add",
                cache : false,
                success : function(data) {
                    var transactionList = data;
                },
                error : function(e) {
                    alert('Error: ' + e);
                }
            });

        }
    </script>

Table

             <tr>
                <th>Transaction Type</th>
                <th>Amount</th>
                <th class="hide-on-mobile">Description</th>
            </tr>
            <c:forEach var="transaction" items="${transactionList}">
                <tr>
                    <td>${transaction.transactionDate}</td>
                    <td>${transaction.transactionType}</td>                     
                    <td>${transaction.accountNarration}</td>
                </tr>
            </c:forEach>

values passed to the data correctly. But I can;t seem to have getthe value outside from the script and use it.

success : function(data) {
                        var transactionList = data;
                    },

this part get the value from controller correctly.

Please Help

1 Answer 1

1

Add an ID for your table like:

    <table id="test_table">

Then call this:

    success : function(data) {
                    var transactionList = data;
                    addRow ("test_table", transactionList);
    },

Finally replace cellX.innerHtml = data.whatever_member_object;

    <script language="javascript">
    function addRow(tableID, data) {

        var table = document.getElementById(tableID);

        var rowCount = table.rows.length;
        var row = table.insertRow(rowCount);

        var cell1 = row.insertCell(0);
        cell1.innerHtml = data;

        var cell2 = row.insertCell(1);
        cell2.innerHTML = data;

        var cell3 = row.insertCell(2);
        cell3.innerHTML = data;             
    }
    </script>

Hope it works.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.