0

Ok i have some search results from input box. I used keyup to get results. Then tis results send to AJAX, and i want to append it to table. My problem is because i use append i will get more than one table headers if i have more results, on the other side i cant use html() because script use for loop so i will only get one result. Can someone help me to solve this problem. I try something like this...

$("#search").keyup(function () 
    {
       var value = $(this).val(); // varijabla iz input polja
       // provera za minimalnu duzinu pretrage
       if(value.length > 3)
       {
            $.ajax({
                type: "POST",
                url: "crud/searching/",
                data: { 'var' : value },
                dataType: "json",
                success: function(response)
                {    alert(response);  
                    $('#warning').html(response.msg);;
                    $('#result').html('');

                    for(var i=0; i<response.result.length; i++)  //petlja za pristup json
                    {
                        $('#result').append('<table class="page-list"><thead><tr><th>#</th><th>Naslov</th><th>Autor</th><th>Cena</th><th>Valuta</th></tr><thead><tbody><tr><td>'+ response.result[i].id +'</td><td>'+ response.result[i].naslov +'</td><td>'+ response.result[i].autor +'</td><td>'+ response.result[i].cena +'</td><td>'+ response.result[i].valuta +'</td></tr> </tbody></table> ' );//dodavanje rezultata u div

                    }                                                     
                } 
            })
        }
    });
1
  • 3
    Create the table once, and subsequently append not to #result but to your new table. Commented Oct 3, 2014 at 8:49

2 Answers 2

1

Just create the table once and then append trs in the loop to its tbody

$('#warning').html(response.msg);
if (response.result.length) {
    var $table = $('<table class="page-list"><thead><tr><th>#</th><th>Naslov</th><th>Autor</th><th>Cena</th><th>Valuta</th></tr><thead><tbody></tbody></table>').appendTo($('#result').html(''));
    var $tbody = $table.find('tbody');

    for (var i = 0; i < response.result.length; i++) //petlja za pristup json
    {
        $tbody.append('<tr><td>' + response.result[i].id + '</td><td>' + response.result[i].naslov + '</td><td>' + response.result[i].autor + '</td><td>' + response.result[i].cena + '</td><td>' + response.result[i].valuta + '</td></tr>  '); //dodavanje rezultata u div
    }
} else {
    $('#result').html('')
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you sir, works great. You help me agian! Manny manny thanks, could you take a look at this issue stackoverflow.com/questions/26142101/…
I wouldn't recommend to use jQuery's .append() in a loop ;-)
sorry but i find unwanted behavior in my code thing is if, i dont have any results table header stays.
0

Try this :

$("#search").keyup(function () 
  {
     var value = $(this).val(); // varijabla iz input polja
     // provera za minimalnu duzinu pretrage
     if(value.length > 3)
     {
      $.ajax({
        type: "POST",
        url: "crud/searching/",
        data: { 'var' : value },
        dataType: "json",
        success: function(response)
        {  alert(response);  
          $('#warning').html(response.msg);
          // Store jQuery objects if used more than once
          var $table = $('<table class="page-list">').appendTo($('#result')),
            $thead = $('<thead><tr><th>#</th><th>Naslov</th><th>Autor</th><th>Cena</th><th>Valuta</th></tr><thead>').appendTo($table),
            $tbody = $('<tbody>').appendTo($table);
            innerHTML  = '';

          for(var i=0; i<response.result.length; i++)  //petlja za pristup json
          {
            innerHTML += '<tr><td>'+ response.result[i].id +'</td><td>'+ response.result[i].naslov +'</td><td>'+ response.result[i].autor +'</td><td>'+ response.result[i].cena +'</td><td>'+ response.result[i].valuta +'</td></tr>' );//dodavanje rezultata u div

          }
          // Append to HTML only once, when you have the full HTML to append
          $tbody.append(innerHTML);
        } 
      })
    }
  });

1 Comment

yes thank you mr Arun solve my problem, its similar to your code, so yours is correct to

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.