0

I have this response output (type JSON)--> pass from controller

[["Subash Nisam",test,"Medix Care","2017.03.02","9.30 am to 10.30 am"],["Subash Nisam",test,"Medix Care","2017.03.02","3.30 to 5.30"]]

I want to append it following table tbody using Javascript-->

<table class="table table-bordered table-hover" id="doctorresultTable">
    <thead>
        <tr>
            <th>Doctor Name</th>
            <th>Speciality</th>
            <th>Hospital</th>
            <th>Date</th>
            <th>Time</th>
        </tr>
    </thead>
    <tbody>

    </tbody>
</table>

How can I do it?

Here is my JS code:

function searchbyNameandDate(name, avadate) {
    var ajaxConfig = {
        type: "GET",
        url: "searchbyNameandDate?name=" + name + "&date=" + avadate,
        async: true,
        dataType: "json"
    };
    $.ajax(ajaxConfig)
            .done(function (response) {

                for (var i = 0; i < response.length; i++) {
                    for (var j = 0; j < 4; j++) {

                        var html = "<tr> \
                <td>" + response[i][j] + "</td> \
                <td>" + response[i][j] + "</td>\
                <td>" + response[i][j] + "</td> \
                <td>" + response[i][j] + "</td> \
                <td>" + response[i][j] + "</td> \
            </tr>";
                        $("#doctorresultTable tbody").append(html);
                    }

                }


            })
            .fail(function (xhr, status, errorMessage) {
                alert("failed to load data");
                console.log("XML HTTP REQUEST : " + xhr);
                console.log("Status : " + status);
                console.log("Error message : " + errorMessage);
            });
    }

}
2
  • can i see your code? what u tried so far Commented Oct 29, 2017 at 13:21
  • I've submitted my js code Commented Oct 29, 2017 at 15:56

2 Answers 2

1

I replace the arrow functions with usual functions. Now it's work properly. -------------------------@Armen Vardanyan ->Thanks for your help.

Here is the code after replace the arrow functions with usual functions.

const data = [["Subash Nisam",'test',"Medix Care","2017.03.02","9.30 am to 10.30 am"],["Subash Nisam",'test',"Medix Care","2017.03.02","3.30 to 5.30"]];

var tableContent = document.querySelector('#doctorresultTable > tbody');
data.map(function(instance) {
 const row = document.createElement('tr');
 tableContent.appendChild(row);
 instance.map(function(info) {
  const cell = document.createElement('td');
  cell.innerText = info;
  row.appendChild(cell);
 });
});
Sign up to request clarification or add additional context in comments.

Comments

0

Using plain javascript (ES6):

const data = [["Subash Nisam",'test',"Medix Care","2017.03.02","9.30 am to 10.30 am"],["Subash Nisam",'test',"Medix Care","2017.03.02","3.30 to 5.30"]];

var tableContent = document.querySelector('#doctorresultTable > tbody');
data.map(instance => {
  const row = document.createElement('tr');
  tableContent.appendChild(row);
  instance.map(info => {
    const cell = document.createElement('td');
    cell.innerText = info;
    row.appendChild(cell);
  });
});
<table class="table table-bordered table-hover" id="doctorresultTable">
            <thead
                <tr>
                    <th>Doctor Name</th>
                    <th>Speciality</th>
                    <th>Hospital</th>
                    <th>Date</th>
                    <th>Time</th>
                </tr>
            </thead>
            <tbody>

            </tbody>
        </table>

Though it would be easier to use some framework like Angular or React for rendering the view (React is preferable if you don't need to build an entire application).

4 Comments

It's worked. can you suggest me a preferred tutorial or reference for improve my js knowledge.
I've problem when run with your code. when it runs console output show this---> Unexpected token > It gives error is here--> " data.map(instance = > {"
You can look into this, as your question was referring to DOM manipulations. And you also can take a look at how jQuery and React are used to manipulate the DOM, there are lots of online tutorials
What browser do you use? By the way, this is ES6, so you're probably going to need a transpiler (like Babel), or just replace the arrow functions with usual functions

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.