Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Populate table with jquery/json

I have an asp.net web page and I want to populate the table with jquery, ajax web service call something. But I am not strong on it at all. The html part for the table is:

<tbody id="testBody">
            <tr id="templateEquipment" class="hidden">
                <td>
                </td>
                <td>
                </td>
                <td>
                </td>
                <td>
                </td>
                <td>
                </td>
                <td>
                </td>
                <td class="hidden">
                </td>
            </tr>
        </tbody>

I already defined the columns and the table is empty at the beginning. And in jquery

function SearchEquipment() {
$.ajax({
    type: "POST",
    url: pageName + "SearchEquipment",
    data: "{'oParams':" + JSON.stringify(BeginSearch()) + "}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (response) {
        if (response.d.length > 0) {

            $.each(response.d, function (i, item) {
            <!-- add row to fill the table-->

Thanks for your help. I do not have the resources for that(links are welcomed).


1 Answers

You can do it like this.

var html = $.map(response.d, function (item, i) {
  return "<tr><td>" + item.value1 + "</td><td>" + item.value2 + "</td></tr>";
}).join("");

$("#testbody").append(html);

By creating a big string with all the rows and cells in it you only need to add it once to the DOM which is a lot quicker!

like image 145
Split Your Infinity Avatar answered Sep 14 '26 18:09

Split Your Infinity



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!